IOS 8 推送通知检查返回错误的 UIRemoteNotificationType
Posted
技术标签:
【中文标题】IOS 8 推送通知检查返回错误的 UIRemoteNotificationType【英文标题】:IOS 8 push notification check returns wrong UIRemoteNotificationType 【发布时间】:2014-10-15 09:56:09 【问题描述】:我正在尝试检查我的应用程序的 PushNotifications 是否已启用。
在 AppDelegate.m 中,我为远程通知注册了应用程序,并在 iPhone (ios 8) 上的设置中启用了此应用程序的推送通知。
我已经用谷歌搜索了方法:
UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
BOOL check = [[UIApplication sharedApplication] isRegisteredForRemoteNotifications];
结果,types = UIRemoteNotificationTypeNone 和 check = NO。
我正在使用这个code sample 注册推送通知应用程序:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
//-- Set Notification
if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
// iOS 8 Notifications
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[application registerForRemoteNotifications];
else
// iOS < 8 Notifications
[application registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];
//--- your custom code
return YES;
这里有什么问题?
【问题讨论】:
【参考方案1】:我认为您已经找到了解决方案,但以防万一:在 iOS 8 中,已弃用 enabledRemoteNoficationTypes 您应该使用:
if ([[UIApplication sharedApplication] respondsToSelector:@selector(currentUserNotificationSettings)])
UIUserNotificationSettings *currentSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];
if (currentSettings.types == UIUserNotificationTypeNone)
最好的
【讨论】:
谢谢它解决了我的问题。它会检测用户是否关闭通知。【参考方案2】:尝试使用以下代码检查您的状态:
UIRemoteNotificationType allOnType = UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeBadge ;
UIRemoteNotificationType offType = UIRemoteNotificationTypeNone ;
UIRemoteNotificationType currentTypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (currentTypes == allOnType)
// all are on
else if (currentTypes == offType)
// all are off
else
// some are on, some are off
编辑
另外,尝试实现推送通知回调:
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
NSLog(@"Token%@",deviceToken);
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)err
NSLog(@"err:%@",err);
-(void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
//for iOS8
【讨论】:
此代码进入“全部关闭”状态。显然是因为 [[UIApplication sharedApplication] enabledRemoteNotificationTypes];返回 UIRemoteNotificationTypeNone 正如我在问题中提到的那样。 如何注册远程通知。我很确定你没有得到错误的回应。您的应用根本没有正确配置推送 嗯,我假设如果 iPhone 设置菜单中的应用程序通知设置正确,则应用程序配置正确。为问题添加了注册码 必须与 iOS 8 兼容,这在 iOS8 中已被弃用以上是关于IOS 8 推送通知检查返回错误的 UIRemoteNotificationType的主要内容,如果未能解决你的问题,请参考以下文章
在 iOS 8 中注册推送通知时如何访问 deviceToken?