0

I tried much time but not called the didRegisterForRemoteNotificationsWithDeviceToken method. I have spent a lot of time. Please let me know me if anyone has a solution.

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

     let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
}

Below function used for register notification in didFinishLaunchingWithOptions in AppDelegate.

    func registerNotification(application:UIApplication) -> Void {

        if #available(iOS 10.0, *){
            UNUserNotificationCenter.current().delegate = self
        UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .sound, .alert], completionHandler: {(granted, error) in
                if (granted)
                {
                    DispatchQueue.main.async {
                        UIApplication.shared.registerForRemoteNotifications()
                    }
                }
                else{
                    print("Noti Not registered !!")
                    //Do stuff if unsuccessful...
                }
            })
        }
        else { //If user is not on iOS 10 use the old methods we've been using
            let notificationSettings = UIUserNotificationSettings(
                types: [.badge, .sound, .alert], categories: nil)
            application.registerUserNotificationSettings(notificationSettings)
        }
    }

Shiva_iOS
  • 47
  • 1
  • 6
  • Not everything ends with success. [Handle fail too](https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1622962-application) and check what's happening there. – user28434'mstep Jun 18 '20 at 10:05
  • @user28434 I have checked this `didFailToRegisterForRemoteNotificationsWithError` not called and not throw any error there. – Shiva_iOS Jun 18 '20 at 10:11
  • 1
    see this for help : [didRegisterForRemoteNotificationsWithDeviceToken is not getting called Swift 5, Xcode 10.2](https://stackoverflow.com/questions/55823643/didregisterforremotenotificationswithdevicetoken-is-not-getting-called-swift-5) – Anbu.Karthik Jun 18 '20 at 10:16
  • 1
    Try restarting your device, it happens with me as well. This is common among Xcode deployment and Adhoc as well, sometimes you need to wait some time up to 1 hour before it works fine again. – Ali Jun 18 '20 at 10:17

1 Answers1

1

Apply Following Code for Notification Permission

  1. Enable Background Modes in Signing & Capabilities

enter image description here

  1. In AppDelegate
    self.registerForPushNotifications(application: application)
    func registerForPushNotifications(application: UIApplication) {

            if #available(iOS 10.0, *) {
                // For iOS 10 display notification (sent via APNS)
                UNUserNotificationCenter.current().delegate = self

                let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
                UNUserNotificationCenter.current().requestAuthorization(
                    options: authOptions,
                    completionHandler: {_, _ in })
            } else {
                let settings: UIUserNotificationSettings =
                    UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
                application.registerUserNotificationSettings(settings)
            }

            application.registerForRemoteNotifications()

        }

        func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
            let token = deviceToken.description().trimmingCharacters(in: CharacterSet(charactersIn: "<>"))

            strDeviceToken = token.replacingOccurrences(of: " ", with: "")
        }

Shiva_iOS
  • 47
  • 1
  • 6
Parth Patel
  • 915
  • 11
  • 33