我使用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);
}
刪除且設備一旦安裝你的應用,並檢查 –
反正因爲該方法'registerForRemoteNotifications'被稱爲iOS中8和9,以'registerUserNotificationSettings'迴應您的應用程序不會在iOS7註冊推送通知。 – vadian