Device Token not received when registering for remote notifications in Swift

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

    When didRegisterForRemoteNotificationsWithDeviceToken doesn't get called, you should always first reconnect to another Wi-Fi network, or toggle Flight modus twice, and restart your app, before you try anything else. Otherwise restarting your device may also help.

    0 讨论(0)
  • 2021-02-04 11:07

    For Swift 3.0 you can use:

    let deviceTokenString = message.reduce("", {$0 + String(format: "%02X", $1)})
    
    0 讨论(0)
  • 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)
    
    }
    
    0 讨论(0)
  • 2021-02-04 11:13

    Swift 5

    For Swift 5, I have used like this,

        let settings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
        application.registerUserNotificationSettings(settings)
    
        UIApplication.shared.registerForRemoteNotifications()
    
    0 讨论(0)
  • 2021-02-04 11:15

    If you get this message, then you have already registered and the method will get called only when the application gets launched or the token gets changed. It has slight chance that the token will be updated.

    Probably, you can try to delete the application from you test device and try to clear it from Xcode. And do it over again.

    0 讨论(0)
  • 2021-02-04 11:22

    You 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)
    }
    
    0 讨论(0)
提交回复
热议问题