Device Token not received when registering for remote notifications in Swift

前端 未结 8 1856
太阳男子
太阳男子 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:08

    Please make sure your certificates are valid and app is registered successfully,Do some Googling and u will sure find right way.This code worked for me.

    (in AppDelegate.swift didFinishLaunchingWithOptions method )

    var notify : UIUserNotificationSettings = UIUserNotificationSettings(forTypes:UIUserNotificationType.Alert|UIUserNotificationType.Sound, categories: nil)
        UIApplication.sharedApplication().registerUserNotificationSettings(notify)
        UIApplication.sharedApplication().registerForRemoteNotifications()
    

    // Add Delegate methods of UIApplication

       func application(application: UIApplication,didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData){
     //send this device token to server
    
        println("\(deviceToken)")
    }
    

    //Called if unable to register for APNS.

    func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
    
        println(error)
    
    }
    

提交回复
热议问题