1

Haven't done iOS dev for long time (iOS 8) so had to read up and started on a new app. Problem is that I don't get a device token callback. Certificates and profiles are setup correctly

import UIKit
import CoreLocation
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate,CLLocationManagerDelegate, UNUserNotificationCenterDelegate {

   let locationManager = CLLocationManager()
   var window: UIWindow?


    static var sharedDelegate: AppDelegate {
    return UIApplication.shared.delegate as! AppDelegate
    }

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        UNUserNotificationCenter.current().delegate = self
        UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
            (granted, error) in
            print("Permission granted: \(granted)")
            guard granted else { return }
            DispatchQueue.main.async{
                application.registerForRemoteNotifications()
            }
        }

          return true
    }
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        let tokenParts = deviceToken.map { data -> String in
            return String(format: "%02.2hhx", data)
        }
        let token = tokenParts.joined()
        print("Device Token: \(token)")
    }

    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        print("Failed to register for remote notifications with error: \(error)")
    }




}

Problem is that didRegisterForRemoteNotificationsWithDeviceToken or didFailToRegisterForRemoteNotificationsWithError is not called whatever I try. note: entitlement is correct, Push Notification ability added, even background mode Remote notification is added.

Can someone provide a working app example that prompts for permission and register and prints the deviceToken

4
  • do you try it on a device or simulator? PN's are not supported on simulator Jun 4, 2020 at 15:25
  • It was on iPhone and iPad both no success Jun 4, 2020 at 15:28
  • What happens if you just call registerForRemoteNotifications? Authorization is only needed to show them to the user so you can test without it. Jun 4, 2020 at 16:02
  • I tried your code in a sample project and it worked fine. do you have anything in the logs when you run the app? Jun 5, 2020 at 7:57

1 Answer 1

2

I'm doing it this way. (Sorry, had to post it here as an answer instead of as a comment because of the length restriction and for better formatting)

let center = UNUserNotificationCenter.current()
center.delegate = self
    // check for user permission first
    center.requestAuthorization(options: [.alert, .sound, .badge]) { (granted, _) in
        NSLog("PUSH NOTIFICATION PERMISSION GRANTED: \(granted)")
        guard granted else { return }
        DispatchQueue.main.async {
            UIApplication.shared.registerForRemoteNotifications()
            }
        }

Some more notes:

Maybe a deciding difference could be that I'm using a let constant for the current() object and storing it rather than assigning the delegate to the current()? I think I remember struggling like you do because of that... By the way, I'm calling this function after enabling a Switch for receiving Notifications, so double-check your function for registering is even called! Maybe another hind could be to call it on UIApplication.shared instead of the application parameter in AppDelegate, but I think that shouldn't make a difference...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.