远程通知iOS 8

如何在iOS 8中获得远程通知的设备令牌? 我在iOS <8的AppDelegate中使用了方法didRegisterForRemoteNotificationsWithDeviceToken ,并返回了设备令牌。 但在iOS 8中,它不。

阅读UIApplication.h中的代码。

你会知道如何做到这一点。

第一:

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

像这样添加代码

 if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) { #ifdef __IPHONE_8_0 UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound) categories:nil]; [application registerUserNotificationSettings:settings]; #endif } else { UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound; [application registerForRemoteNotificationTypes:myTypes]; } 

如果您不使用Xcode 5和Xcode 6 ,请尝试使用此代码

 if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) { UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound |UIRemoteNotificationTypeAlert) categories:nil]; [application registerUserNotificationSettings:settings]; } else { UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound; [application registerForRemoteNotificationTypes:myTypes]; } 

(感谢@zeiteisen @dmur的提醒)


第二:

添加这个函数

 #ifdef __IPHONE_8_0 - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings { //register to receive notifications [application registerForRemoteNotifications]; } - (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forRemoteNotification:(NSDictionary *)userInfo completionHandler:(void(^)())completionHandler { //handle the actions if ([identifier isEqualToString:@"declineAction"]){ } else if ([identifier isEqualToString:@"answerAction"]){ } } #endif 

你可以得到deviceToken

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

如果它仍然不起作用,使用这个函数和NSLog的错误

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

注册iOS 8并继续支持旧版本的方式

 UIApplication *application = [UIApplication sharedApplication]; if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) { UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge |UIUserNotificationTypeSound |UIUserNotificationTypeAlert) categories:nil]; [application registerUserNotificationSettings:settings]; } else { UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound; [application registerForRemoteNotificationTypes:myTypes]; } 

并在应用程序委托中添加

 - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings { [application registerForRemoteNotifications]; } 

iOS8可以收到沉默notificaions没有要求许可。 调用- (void)registerForRemoteNotifications 。 在这个application:didRegisterForRemoteNotificationsWithDeviceToken:将被调用

注意:只有在应用程序成功注册了具有以下function的用户通知或启用了“后台应用程序刷新”时,才会调用带有令牌的callback。

如果启用了任何通知types,请检查设置为您的应用程序。 如果没有,你将不会得到一个设备令牌。

您现在可以通过以下方式获取安静通知

 aps { content-available: 1 } 

在通知有效载荷中

但是出现的通知仍然需要权限。 呼叫

 UIUserNotificationType types = UIUserNotificationTypeSound | UIUserNotificationTypeBadge | UIUserNotificationTypeAlert; UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:types categories:nil]; [application registerUserNotificationSettings:notificationSettings]; 

这个代码应该请求权限。

您现在应该准备好推送通知

在我的情况下,我已经做了必要的更新来请求iOS 7和iOS 8的推送通知访问,但是当iOS 8用户授予访问权限时,我还没有实现新的callback。 我需要将此方法添加到我的应用程序委托。

 - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings { [application registerForRemoteNotifications]; } 

如果您使用Xamarin.iOS来构build您的移动应用程序,则可以使用此代码片段来请求推送通知注册

 if (UIDevice.CurrentDevice.CheckSystemVersion(8,0)) { UIUserNotificationType userNotificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound; UIUserNotificationSettings settings = UIUserNotificationSettings.GetSettingsForTypes(userNotificationTypes, null); UIApplication.SharedApplication.RegisterUserNotificationSettings(settings); } else { UIRemoteNotificationType notificationTypes = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound; UIApplication.SharedApplication.RegisterForRemoteNotificationTypes(notificationTypes); } 

您还需要重写DidRegisterUserNotificationSettings方法以获取从APNS服务器返回的设备令牌:

 public override void DidRegisterUserNotificationSettings(UIApplication application, UIUserNotificationSettings notificationSettings) { application.RegisterForRemoteNotifications(); } 

Madao的答案( https://stackoverflow.com/a/24488562/859742 )是正确的,但….

 UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIRemoteNotificationTypeBadge 

应该更“正确”

 UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil]; 

这些标志具有相同的位掩码值,这就是为什么这两个工作,但UIUserNotificationSettings需要UIUserNotificationType不是UIRemoteNotificationType

除此之外,我会打电话

 [application registerUserNotificationSettings:settings]; 

AppDelegate方法中(取决于授予的权限),

 - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings 

我更好的方法来保持向后兼容性,我们可以用这种方法,这是我的工作希望为你工作。 也很容易理解。

 if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) { [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; [[UIApplication sharedApplication] registerForRemoteNotifications]; } else { [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)]; } 
 UIUserNotificationType types = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert; UIUserNotificationSettings *mySettings = [UIUserNotificationSettings settingsForTypes:types categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:mySettings]; [application registerForRemoteNotifications];