2016-09-19 130 views
4

我有2周的iPhone:1 - 與IOS 10和2 - 與IOS 9didRegisterForRemoteNotificationsWithDeviceToken不叫上ios10

雖然試圖將第一iphone:

didRegisterForRemoteNotificationsWithDeviceToken方法不被調用時用戶點擊「允許「在警報 雖然,方法didRegisterUserNotificationSettings被稱爲。 在這種情況下,設備不會收到推送通知。

雖然試圖第二iphone:

這兩種方法都在這裏調用。並且設備會收到推送通知。

然後我上模擬器IOS 8

檢查在這種情況下一樣在第一。只有一種方法被調用。

我檢查了一些類似問題的答案,但他們沒有幫助我。 我懷疑這個問題是在推送通知設置的某個地方,因爲ios 9工作正常。 所以這個問題是某處的ios 10

內的非常的問題是:

  1. 我如何可以調用方法didRegisterForRemoteNotificationsWithDeviceToken
  2. 要不我怎麼能得到設備標識,因爲它是我們的目標

期待您的幫助!

+0

您是如何註冊通知的? 'UIApplication'使用'-registerUserNotificationSettings:'和'-registerForRemoteNotifications'註冊了嗎?後者需要調用'didRegisterForRemoteNotificationsWithDeviceToken'。請參閱https://developer.apple.com/reference/uikit/uiapplication/1623078-registerforremotenotifications。 – Roy

+0

也許你可以從這裏獲得幫助。 https://www.sitepoint.com/developing-push-notifications-for-ios-10/ –

+0

你可以按照上面的步驟來獲取工作推送通知在iOS 10 http://stackoverflow.com/questions/39572897/ IOS-10推送通知-問題,解決 –

回答

9

對於使用xCode 8 GM的iOS 10。

此問題通過以下步驟解決。 要求: - Xcode 8轉基因種子。 MAC操作系統: - 船長EL 10.11.6

不要刪除您的IOS 9或以下版本的代碼。

步驟1: - 進入 - >目標設置在Xcode - >功能 - >啓用PushNotifications。

步驟2: - 添加UserNotifications框架 - >構建階段 - >鏈接庫

步驟3: -

#import <UserNotifications/UserNotifications.h> 
@interface AppDelegate : UIResponder <UIApplicationDelegate,UNUserNotificationCenterDelegate> 

@end 

步驟4: - 在方法didFinishLaunchingWithOptions註冊UIUserNotificationSettings。

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ 

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

    return YES; 
    } 

第5步: -實施UNUserNotificationCenterDelegate的2種委託方法。

-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{ 

//Do Your Code.................Enjoy!!!! 
    } 

-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler{ 

} 
0

你應該調用此方法內didFinishLaunchingWithOptions

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.") 
        if !(SharedPrefs.sharedInstance!.isLoggedIn){ 
         Defaults.sharedPref.removeNotificationToken() 
        }else{ 
         UIApplication.shared.unregisterForRemoteNotifications() 
         print("We will delete the token at the time of logout") 
        } 
       } 
      } 

     } 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() 
     } 
    } 
0

您可能要檢查的項目設置。選擇你的項目。轉至第二個選項卡 - >功能 - >選擇推送通知開啓。

相關問題