2016-09-09 26 views
3

我使用iOS 10 SDK在Xcode 8上構建了我的應用程序。 didRegisterForRemoteNotificationsWithDeviceToken不適用於iOS 9.3設備,因此iOS 9.3和波紋管設備無法接收推送通知。 但iOS 10設備可以接收推送。 如果我使用iOS 9.3 SDK在Xcode 7.3上構建相同的應用程序,那麼iOS 9設備可以接收推送。 所以,我確定我的所有設置都應該是正確的。didRegisterForRemoteNotificationsWithDeviceToken未在Xcode 8中調用

這是我實現:

// Push notification 
if(SYSTEM_VERSION_LESS_THAN(@"10.0")) 
{ 
    if([application respondsToSelector:@selector(registerUserNotificationSettings:)]) 
    { 
     UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound) categories:nil]; 
     [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; 
     [[UIApplication sharedApplication] registerForRemoteNotifications]; 

    } 
    else 
    { 
     [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)]; 
    } 


} 
else 
{ 
    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; 
    center.delegate = self; 
    [center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) completionHandler:^(BOOL granted, NSError * _Nullable error) 
    { 
     if(!error) 
     { 
      [[UIApplication sharedApplication] registerForRemoteNotifications]; 
     } 

    }]; 
} 


- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { 

NSString * tokenAsString = [[[deviceToken description] 
          stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]] 
          stringByReplacingOccurrencesOfString:@" " withString:@""]; 

} 



- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { 

DLog(@"app didFailToRegisterForRemoteNotificationsWithError: %@", error.description); 
} 
+0

刪除且設備一旦安裝你的應用,並檢查 –

+0

反正因爲該方法'registerForRemoteNotifications'被稱爲iOS中8和9,以'registerUserNotificationSettings'迴應您的應用程序不會在iOS7註冊推送通知。 – vadian

回答

2

爲什麼你使用的方法已過時? [[UIApplication sharedApplication] registerForRemoteNotificationTypes:

當我刪除它

[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge)categories:nil]]; 

和使用

[[UIApplication sharedApplication] registerForRemoteNotifications]; 

我從APN的得到令牌。

+0

我刪除它,並嘗試。但仍然沒有得到令牌。您是否嘗試過使用xcode 8和ios 9.3設備? – sajaz

+0

是的,我得到了令牌....你有沒有改變常規選項卡中的版本,並試圖?? – Ananth

+0

@ANANTHAKRISHNANKG OP爲iOS7,8/9和10定義了3種情況。「registerForRemoteNotificationTypes」方法僅在實際方法不可用的iOS7中使用。 – vadian

0

您需要從didFinishLaunchingWithOptions中調用registerForNotifications方法。

func registerForNotifications(){ 
     if #available(iOS 10.0, *) { 
      let center = UNUserNotificationCenter.current() 
      center.delegate = self 
      center.requestAuthorization(options:[.alert,.sound,.badge]) { (granted, error) in 
       if granted{ 
        UIApplication.shared.registerForRemoteNotifications() 
       }else{ 
        print("Notification permission denied.") 

       } 
      } 

     } else { 
      // For iOS 9 and Below 
      let type: UIUserNotificationType = [.alert,.sound,.badge]; 
      let setting = UIUserNotificationSettings(types: type, categories: nil); 
      UIApplication.shared.registerUserNotificationSettings(setting); 
      UIApplication.shared.registerForRemoteNotifications() 
     } 
    } 

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { 
     let token = String(format: "%@", deviceToken as CVarArg).trimmingCharacters(in: CharacterSet(charactersIn: "<>")).replacingOccurrences(of: " ", with: "") 
     print(token) 
    } 


extension AppDelegate : UNUserNotificationCenterDelegate{ 
    @available(iOS 10.0, *) 
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (_ options: UNNotificationPresentationOptions) -> Void) { 
     print("Handle push from foreground」) 

     let info = ((notification.request.content.userInfo as NSDictionary).value(forKey: "aps") as! NSDictionary) 
     if let type = info.value(forKey: "type") as? Int{ 
      if type == 0 { // notification arrive 
       handlePush(apsDict: (notification.request.content.userInfo as NSDictionary)) 
      } 
     } 
    } 
    @available(iOS 10.0, *) 
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 
     print("Handle push from background or closed") 

     let info = ((response.notification.request.content.userInfo as NSDictionary).value(forKey: "aps") as! NSDictionary) 
     if let type = info.value(forKey: "type") as? Int{ 
      if type == 0 { // notification arrive 
       handlePush(apsDict: (response.notification.request.content.userInfo as NSDictionary)) 
      } 
     } 
    } 
} 
相關問題