qua*_*ang 76 objective-c apple-push-notifications ios8
如何在iOS 8中获取设备令牌以进行远程通知?我所使用的方法didRegisterForRemoteNotificationsWithDeviceToken中AppDelegate在IOS <8,并且它返回的设备令牌.但在iOS 8中,它没有.
Mad*_*dao 180
你会知道怎么做.
第一:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
Run Code Online (Sandbox Code Playgroud)
像这样添加代码
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];
}
Run Code Online (Sandbox Code Playgroud)
如果您不同时使用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];
}
Run Code Online (Sandbox Code Playgroud)
(感谢@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
Run Code Online (Sandbox Code Playgroud)
你可以获得deviceToken
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
Run Code Online (Sandbox Code Playgroud)
如果它仍然不起作用,使用此功能和NSLog 错误
-(void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
Run Code Online (Sandbox Code Playgroud)
zei*_*sen 75
注册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];
}
Run Code Online (Sandbox Code Playgroud)
并在应用程序委托添加
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
[application registerForRemoteNotifications];
}
Run Code Online (Sandbox Code Playgroud)
iOS8可以在未经许可的情况下收到静默通知.打电话
- (void)registerForRemoteNotifications.在此之后 application:didRegisterForRemoteNotificationsWithDeviceToken:将被调用
注意:仅当应用程序已成功注册具有以下功能的用户通知或启用了后台应用程序刷新时,才会调用带有令牌的回调.
如果启用了任何通知类型,请检查您的应用程序设置.如果没有,您将无法获得设备令牌.
您现在可以使用获取静默通知
aps {
content-available: 1
}
Run Code Online (Sandbox Code Playgroud)
在通知有效负载中
但出现的通知仍需要许可.呼叫
UIUserNotificationType types = UIUserNotificationTypeSound | UIUserNotificationTypeBadge | UIUserNotificationTypeAlert;
UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:types categories:nil];
[application registerUserNotificationSettings:notificationSettings];
Run Code Online (Sandbox Code Playgroud)
此代码应该请求许可.
您现在应该准备好获取推送通知了
Kyl*_*egg 15
在我的情况下,我已经进行了必要的更新以请求iOS 7和iOS 8的推送通知访问,但是当iOS 8用户授予访问权限时,我没有实现新的回调.我需要将此方法添加到我的app委托.
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings {
[application registerForRemoteNotifications];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
84657 次 |
| 最近记录: |