如何在iOS 8中获取用于远程通知的设备令牌?我所使用的方法didRegisterForRemoteNotificationsWithDeviceToken
中AppDelegate
在IOS <8,并且它返回令牌的设备。但是在iOS 8中却没有。
Answers:
您将知道该怎么做。
第一:
- (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
UIUserNotificationSettings
或UIApplication
谈论iOS 8的要求。它被掩藏在其API差异中。
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
Xcode6 。相反,您应该像zeiteisen所做的那样进行检查。
注册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无需征求许可即可接收静默通知。致电
- (void)registerForRemoteNotifications
。之后 application:didRegisterForRemoteNotificationsWithDeviceToken:
将被称为
注意:仅当应用程序已使用以下功能成功注册了用户通知时,或者启用了“后台应用程序刷新”,才会调用带有令牌的回调。
如果启用了任何通知类型,请为您的应用检查设置。如果没有,您将不会获得设备令牌。
您现在可以通过以下方式获取静默通知
aps {
content-available: 1
}
在通知有效载荷中
但是出现的通知仍需要许可。呼叫
UIUserNotificationType types = UIUserNotificationTypeSound | UIUserNotificationTypeBadge | UIUserNotificationTypeAlert;
UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[application registerUserNotificationSettings:notificationSettings];
此代码应寻求许可。
您现在应该准备好获取推送通知
OS8 can receive silent notifications without asking for permission
。那让我发疯,这是我发现唯一有帮助的地方!
if
声明中,你应该使用UIUserNotificationTypeSound
替代UIRemoteNotificationTypeSound
,因为UIRemoteNotificationType
在iOS的8弃用
就我而言,我进行了必要的更新以请求对iOS 7和iOS 8的推送通知访问,但是当iOS 8用户授予访问权限时,我没有实现新的回调。我需要将此方法添加到我的应用程序委托中。
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
[application registerForRemoteNotifications];
}
如果您使用Xamarin.iOS构建移动应用程序,则可以使用此代码段来请求推送通知注册
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];