Skip to content
This repository has been archived by the owner on Feb 27, 2019. It is now read-only.

onAuthChange always return Unauthorized for Notifications #125

Closed
ed-mejia opened this issue Nov 9, 2015 · 31 comments
Closed

onAuthChange always return Unauthorized for Notifications #125

ed-mejia opened this issue Nov 9, 2015 · 31 comments
Labels
Milestone

Comments

@ed-mejia
Copy link

ed-mejia commented Nov 9, 2015

Hi, I'm trying a very simple setup... but for some reason I always get _Unauthorized_ response to the notifications request, I'm running on Xcode 7.1, swift 2.1 Simulator iPhone 6, iOS 9.1

Code:

import UIKit
import PermissionScope

class ViewController: UIViewController {
    let singlePscope = PermissionScope()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        singlePscope.viewControllerForAlerts = self
        singlePscope.addPermission(NotificationsPermission(notificationCategories: nil),
            message: "We use this to send you\r\nspam and love notes")

        singlePscope.onAuthChange = { (finished, results) in
            print("got results \(results)")
        }

    }

    @IBAction func singlePerm() {
        singlePscope.requestNotifications()
    }

}

Console:

got results [Notifications Unauthorized]

@bre7 bre7 added the bug label Nov 9, 2015
@bre7
Copy link
Collaborator

bre7 commented Nov 9, 2015

Could you try it with a real device ?

@ed-mejia
Copy link
Author

ed-mejia commented Nov 9, 2015

Hi!...

Yes tested on real device, iPhone 6 iOS 9.1, same result.

@bre7
Copy link
Collaborator

bre7 commented Nov 9, 2015

Hi ed, Some additional questions:

  1. Where you prompted by PScope to allow the permission ?
  2. Could you post a screenshot of Settings -> AppName ?

@nickoneill
Copy link
Owner

@ed-mejia You should be using the singlePscope.show() method, not requestNotifications()... unless you don't care about the UI.

@bre7
Copy link
Collaborator

bre7 commented Nov 9, 2015

😓 yep, didn't even realize you were calling the request method directly...

Still, I think there's a problem with the request methods (when the intent is to call them directly): case .Authorized: break implies no return so onAuthChange won't be triggered

@ed-mejia
Copy link
Author

ed-mejia commented Nov 9, 2015

Hi @nickoneill , I don't want to display the default UI, I want to use my own, so that's the reason to use requestNotifications() directly, in fact thats a feature according to the docs 🎉

@bre7 That's weird despite the fact that initially returned Unauthorized it seems that was Authorized because notifications access was granted..

@bre7
Copy link
Collaborator

bre7 commented Nov 9, 2015

Request methods, currently don't trigger the onAuthChange closure...Only show() uses it. So, in order to return that info, we need to know whether request..() was called from show() or not and call onAuthChange if necessary

@ed-mejia
Copy link
Author

ed-mejia commented Nov 9, 2015

@bre7 I don't understand, onAuthChange is being called, and even I tap OK to allow permission the closure is called indicating that I did't granted.

@bre7
Copy link
Collaborator

bre7 commented Nov 9, 2015

Yeah, ignore that last comment. Call statusNotifications() instead of requestNotifications() (after granting access) and post the result

@ed-mejia
Copy link
Author

singlePscope.onAuthChange = { (finished, results) in
    print("got results \(results)")
    print("statusNotifications \(self.singlePscope.statusNotifications())")
}

Console:

got results [Notifications Unauthorized]
statusNotifications Authorized

Should I use this hack?

@bre7
Copy link
Collaborator

bre7 commented Nov 10, 2015

Got it.

Cloudkit's getResultsForConfig + owaves fix fixed it. Check the new branch

PENDING: show() isn't working

bre7 added a commit to bre7/PermissionScope that referenced this issue Nov 10, 2015
@ed-mejia
Copy link
Author

@bre7 Hey I tested the new branch and seems to be working correctly now for direct requests.

Im using cocoapods, this fix is going to be merge into master? Im kind of lost how to update my project to include any update since the last release 1.0.1 is 26 commits ahead to master

@bre7
Copy link
Collaborator

bre7 commented Nov 10, 2015

Once we get this sorted out, nick will probably push a new version. As I said before, this fix still needs some work since show() isn't working

@ed-mejia
Copy link
Author

Great, Thanks!!!! 👍

@nickoneill
Copy link
Owner

Huh. I thought I had pushed a 1.0.2 version at some point... I'll figure out what to include for a new release today.

@nickoneill
Copy link
Owner

@ed-mejia I couldn't reproduce a problem where notifications were reporting the incorrect status, though I did encounter an issue where onAuthChange won't be called if notifications are already authorized.

Can you test with :head and tell me if this is reproducible for you still?

@ed-mejia
Copy link
Author

Hi @nickoneill, I was able to recreate the issue again,

Update pod from master, my pod config is just like:

pod 'PermissionScope', :git => 'https://github.com/nickoneill/PermissionScope.git'

On Simulator: Reset content and settings

My Controller code:

class PermissionsViewController: UIViewController {

    //MARK: Properties
    let pscope = PermissionScope()

    override func viewDidLoad() {
        super.viewDidLoad()
        setupPermissions()

    }

    /// Private func to setup permission for this screen
    private func setupPermissions() {
        pscope.viewControllerForAlerts = self
        pscope.addPermission(NotificationsPermission(notificationCategories: nil),
            message: "We use this to send you\r\nspam and love notes")

        pscope.onAuthChange = { (finished, results) in
            print("got results \(results)")
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func enablePushNotifications(sender: RoundedButton) {
        pscope.requestNotifications()
    }
}

the console output after granting the permission:

got results [Notifications Unauthorized]

@nickoneill
Copy link
Owner

Thanks for the excellent repro steps @ed-mejia, I can see the issue you're having now.

Can you try again with the most updated pscope code? Use this in your podfile:

pod 'PermissionScope', :head

And pod install again

I tested with a fresh project and the issue appears with our 1.0.1 version and fixed in the upcoming 1.0.2. Please let me know if you see anything different.

@ed-mejia
Copy link
Author

@nickoneill I don't think the fixed code is in the repository or I'm doing something wrong 😕

I just create a fresh new project to be sure:

Xcode 7.2, Apple Swift version 2.1.1

My pod file:

# Uncomment this line to define a global platform for your project
 platform :ios, '8.0'
# Uncomment this line if you're using Swift
 use_frameworks!

target 'PerTest' do

 pod 'PermissionScope', :head

end

run pod install, console output:

 pod install
Updating local specs repositories
Analyzing dependencies
Downloading dependencies
Installing PermissionScope (HEAD based on 1.0.1)
Generating Pods project
Integrating client project

Opened the project run and test again same console output after permission being granted:

got results [Notifications Unauthorized]

I attached the project here:
PermTest.zip

@bre7
Copy link
Collaborator

bre7 commented Dec 23, 2015

Try

pscope.onAuthChange = { (finished, results) in
    print("got results \(results)")
    print("statusNotifications \(self.pscope.statusNotifications())")
}

Is it still Unauthorized and Authorized ? #126 fixes the issue related with direct calls iirc but show() won't work.

@ed-mejia
Copy link
Author

@bre7 Yes the results are:

got results [Notifications Unauthorized]
statusNotifications Authorized

@nickoneill
Copy link
Owner

Here's what I'm seeing with your project @ed-mejia:
• First time run on a fresh sim: Unauthorized
• Subsequent runs on sim with same bundle id: Authorized
• First time run on sim with new bundle id (existing sim): Authorized
• First time run on iOS 8 device: Authorized
• First time run on iOS 9 device: Authorized

I'm nearly ready to chalk this up to simulator weirdness (not the first time we've seen sim issues with permissions). It seems to work fine in plenty of other places. Care to run some of these and report your findings?

@ed-mejia
Copy link
Author

@nickoneill I just ran the project on an iPhone 6 iOS 9.2 for the first time and the results were not successful just after calling requestNotifications for the first time:

got results [Notifications Unauthorized]
statusNotifications Authorized

So it's not a simulator issue, if you call requestNotifications again the results are ok, but its wrong from the beginning.

@ed-mejia
Copy link
Author

I just tried to run the same project again with different bundle ids on the same device and the results were ok despite the fact the first time were wrong....

I took another iPhone 6 and I ran the project again for the very first time on that device and the results were:

got results [Notifications Unauthorized]
statusNotifications Authorized

So at the very very first time the app runs on the device it returns a wrong value, considering that the request of permission is a one shoot in the very first life time of the app this issue really matters because there is only one chance to display the actual status of that request.

@nickoneill
Copy link
Owner

Alright, back to the drawing board then. I'll run through the code again and see if I can spot a race condition.

@nickoneill
Copy link
Owner

@ed-mejia We were going through the detectAndCallback cycle (requesting status from each of the permissions) in the notification observer callback which apparently isn't long enough after accepting permissions for that change to be reflected in currentUserNotificationSettings.

I've moved the detectAndCallback cycle into an async block, similar to how we call it when you request via the UI. This resolved the issue that I was able to reproduce when I reset a simulator and installed fresh.

Let me know if this fixes the issue for you. I'll be releasing a 1.0.2 beta shortly, or you can continue using :head to test.

@ed-mejia
Copy link
Author

@nickoneill I used the test project that I attached here PermTest.zip .. updated Pod to head, run and still 😭

got results [Notifications Unauthorized]

I did simulator reset, change bundle identifier, no luck

PD: Sorry I closed the issue by mistake.

@ed-mejia ed-mejia reopened this Jan 16, 2016
@nickoneill
Copy link
Owner

Shit. Same deal with the test project and 1.0.2beta, I'll try again. At least we know where the issue is at this point.

@nickoneill
Copy link
Owner

@ed-mejia

Opted for a slight different approach, now we just straight up delay 0.1s after finishedShowingNotificationPermission comes back from the permissions dialog. Please test and let me know what you find, it worked using your PermTest project and erasing simulators for me. I didn't update the tag so be careful when updating the pod, I couldn't get it to update to the newest commit (d232175) right away.

@ed-mejia
Copy link
Author

Hey @nickoneill I can confirm that the onAuthChange callback is now returning the right value after authorising:

got results [Notifications Authorized]

🎉 😃

@nickoneill
Copy link
Owner

Awesome :) Releasing here soon...

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants