简体   繁体   中英

Trouble receiving push notifications in iOS10 and iOS9

Trying to set up push notification for iOS10. had it working previously for versions below 10.
Read through some guides my setup is like so:

// Register for the defined notifications

        if #available(iOS 10.0, *) {

            let center = UNUserNotificationCenter.current()
            center.delegate = UIApplication.shared.delegate as! AppDelegate
            center.requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in

                if error == nil {
                    UIApplication.shared.registerForRemoteNotifications()
                }
            }

        } else {

            // Fallback on earlier versions

            let notificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: [autoCancelCategory])
            UIApplication.shared.registerUserNotificationSettings(notificationSettings)
            UIApplication.shared.registerForRemoteNotifications()

        }

This is called on login ^ in one of my view controllers.

And now in AppDelegate , it conforms to UNUserNotificationCenterDelegate , I have these methods:

@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    print("Got a push!")
}


@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    print("Got a push!")
}

And I also have:

 func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {

    let settings = UserDefaults.standard
    settings.setValue(deviceToken, forKey: "deviceToken")
}

The device token is uploaded to the server as Data , and this has worked fine for previous versions.
I have checked that the token on the server matches the one on the phone.
I have enabled push notifications for all targets in Capabilities , I have also ticked Add the push Notifications entitlement to your entitlements file .
Not receiving anything at all though on the push.
Any ideas what I might be doing wrong here? Any pointers would be really appreciated!

Thanks!

EDIT: I have also noticed that the push notifications are not working in iOS9.

Have you checked that Build Settings > Code Signing Entitlements has a reference to entitlements file? [AppName].entitlements.

And that the entitlements file contains the correct info:

(key: APS Environment, value: development)

Other than that, you can try regenerating push certificates. We had to create new certificates to get it to work in production after IOS 10 upgrade (but not in sandbox environment).

Just noticed that you're registering the delegate later than application(_:willFinishLaunchingWithOptions:) or application(_:didFinishLaunchingWithOptions:) methods are called.

Perhaps this is why your delegate methods are not being hit?

See the docs :

Important

You must assign your delegate object to the UNUserNotificationCenter object no later before your app finishes launching. For example, in an iOS app, you must assign it in the application( :willFinishLaunchingWithOptions:) or application( :didFinishLaunchingWithOptions:) method.

Going to answer this, maybe it will be useful for other. My token was wrong. The encoding has changed with recent updates. I did actually search all over stack overflow for this and it was to my understanding that I had to convert the Data to a String using the base64string . However I noticed this string had a few odd characters in it such as \\ . I eventually ended up making a Data extension:

extension Data {

    func hexString() -> String {
        return self.reduce("") { string, byte in
            string + String(format: "%02X", byte)
        }
    }

}

This converts the Data to the correct format. So if this is how you do your conversion, plus all the entitlements are set, you remember to add the new methods:

@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    print("Got a push!")
}


@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    print("Got a push!")
}

and set the delegate object in didFinishLaunchingWithOptions everything should be good to go.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM