Kex Kex - 2 months ago 36
iOS Question

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.

Kex Kex
Answer

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.