Device Token not received when registering for remote notifications in Swift

前端 未结 8 1859
太阳男子
太阳男子 2021-02-04 10:42

I somehow can not receive the device token when registering for remote notifications. I get the modal saying \"Do you want to allow App X to be able to send you notificaitons\",

相关标签:
8条回答
  • 2021-02-04 11:28

    For swift 4.0 please follow some steps :

    1).On push notification in capabilities(TARGET)

    2).Use this code in didFinishLaunchingWithOptions

      let settings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
    
            application.registerUserNotificationSettings(settings)
            application.registerForRemoteNotifications()
    

    3).Add this also

     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)")  
    } 
    
    0 讨论(0)
  • 2021-02-04 11:31

    Do this:

       UIApplication.sharedApplication().registerForRemoteNotifications()
    
    0 讨论(0)
提交回复
热议问题