Did Register For Remote Notifications With Device Token not working in Swift 5

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        registerForPushNotifications()
        return true
    }
    func registerForPushNotifications() {
 UNUserNotificationCenter.current()
        .requestAuthorization(options: [.alert, .sound, .badge]) {
          [weak self] granted, error in

          print("Permission granted: \(granted)")
          guard granted else { return }
            self!.getNotificationSettings()
      }
      }
    func getNotificationSettings() {
      UNUserNotificationCenter.current().getNotificationSettings { settings in
        print("Notification settings: \(settings)")
        guard settings.authorizationStatus == .authorized else { return }
        DispatchQueue.main.async {
          UIApplication.shared.registerForRemoteNotifications()
        }

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

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

this is the code where I have implemented remote notifications registration methods. this is what I have done so far:

  • I have configuring push notification by Targets -> Signing&Capabilities and correctly entered team and build indentifier.
  • Add the push notification capability by tapping + at the top left.
  • I have enabled the push notification by going into the iPhone setting.
  • wifi is on from the iPhone.

This is what I have tried so far to resolve this:

  • Restart the phone, disable and then enable wifi
  • Tried to change bundle identifier
  • Remove the push notification capability and added it again but nothing helped so far.

App asks for the permission for push notification and even after granting the permission, last two methods are not being called. what have I done wrong? I have seen other similar question and tried everything but still could not figure out what is wrong with the code.

This Post Has One Comment

  1. No Fault

    ou can try this

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.

    var types: UIUserNotificationType = UIUserNotificationType.Badge |
    UIUserNotificationType.Alert |
    UIUserNotificationType.Sound

    var settings: UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: nil)

    application.registerUserNotificationSettings(settings)
    application.registerForRemoteNotifications()

    return true
    }

    func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {

    var characterSet: NSCharacterSet = NSCharacterSet(charactersInString: “”)

    var deviceTokenString: String = (deviceToken.description as NSString)
    .stringByTrimmingCharactersInSet(characterSet)
    .stringByReplacingOccurrencesOfString( ” “, withString: “”) as String

    println(deviceTokenString)

    }
    EDIT: Update for Swift 2.x

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.

    let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)

    application.registerUserNotificationSettings(settings)
    application.registerForRemoteNotifications()

    return true
    }
    EDIT: Update for Swift 3.x

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.

    let settings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)

    application.registerUserNotificationSettings(settings)
    application.registerForRemoteNotifications()

    return true
    }

    func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
    let characterSet = CharacterSet(charactersIn: “”)
    let deviceTokenString = deviceToken.description.trimmingCharacters(in: characterSet).replacingOccurrences(of: ” “, with: “”);
    print(deviceTokenString)
    }

Leave a Reply