未收到设备令牌

问题描述:

iPhone设备未从我的应用程序接收设备令牌。该未收到设备令牌

didRegisterForRemoteNotificationsWithDeviceToken 

方法不获取调用虽然我在

didFinishLaunch 

方法下面的代码。

didFinishLaunch方法:

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

注:

  1. notificationSettings方法获取调用。但不是didRegisterdidFail方法需要远程通知。
  2. 我使用的是iPhone 6S与iOS 9

什么需要检查来获得设备令牌?

尝试这种在didFinishLaunch

if #available(iOS 10.0, *) { 
     UNUserNotificationCenter.current().delegate = self 
    } else {   
     // Fallback on earlier versions 
    } 
    // ios 10 
    if #available(iOS 10.0, *) { 
     let center = UNUserNotificationCenter.current() 
     center.requestAuthorization(options: [.alert, .sound,.badge]) { (granted, error) in 
      // actions based on whether notifications were authorized or not 
     } 

     UIApplication.shared.registerForRemoteNotifications() 
    } 
    // iOS 9 support 
    else if #available(iOS 9, *) { 
     UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil)) 
     UIApplication.shared.registerForRemoteNotifications() 
    } 
    else 
    { 
     let settings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil) 
     UIApplication.shared.registerUserNotificationSettings(settings) 
     UIApplication.shared.registerForRemoteNotifications() 
    } 
} 

您正在使用UNUserNotificationCenter但如果只工作与iOS 10或10+您的设备。

对于iOS 9,你需要下面的代码寄存器推送通知

UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.badge, .sound, .alert], categories: nil)) 
UIApplication.shared.registerForRemoteNotifications() 

您可以添加基于iOS的版本状态像@Kamalesh答案。