2017-10-13 144 views
0

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

什麼需要檢查來獲得設備令牌?

回答

1

嘗試這種在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() 
    } 
} 
1

您正在使用UNUserNotificationCenter但如果只工作與iOS 10或10+您的設備。

對於iOS 9,你需要下面的代碼寄存器推送通知

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

您可以添加基於iOS的版本狀態像@Kamalesh答案。