代码更新后,启用iOS 8的设备不会收到PUSH通知
我最近升级了我的一个testingiPhone到iOS 8,然后升级了PUSH注册码如下(使用xCode 6)
-(BOOL)hasNotificationsEnabled { NSString *iOSversion = [[UIDevice currentDevice] systemVersion]; NSString *prefix = [[iOSversion componentsSeparatedByString:@"."] firstObject]; float versionVal = [prefix floatValue]; if (versionVal >= 8) { NSLog(@"%@", [[UIApplication sharedApplication] currentUserNotificationSettings]); //The output of this log shows that the app is registered for PUSH so should receive them if ([[UIApplication sharedApplication] currentUserNotificationSettings].types != UIUserNotificationTypeNone) { return YES; } } else { UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; if (types != UIRemoteNotificationTypeNone){ return YES; } } return NO; } -(void)registerForPUSHNotifications { NSString *iOSversion = [[UIDevice currentDevice] systemVersion]; NSString *prefix = [[iOSversion componentsSeparatedByString:@"."] firstObject]; float versionVal = [prefix floatValue]; if (versionVal >= 8) { //for iOS8 UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; [[UIApplication sharedApplication] registerForRemoteNotifications]; } else { [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)]; } }
尽pipe此升级和[[UIApplication sharedApplication] currentUserNotificationSettings]显示设备已启用PUSH的事实,但我没有收到PUSH通知。
我正在使用Parse并按照他们所做的一切来完成本书( https://parse.com/tutorials/ios-push-notifications )。
有没有人遇到同样的问题? 还有什么我可能会失踪?
在iOS 8中,注册推送通知的方式已经更改:以下是iOS 9之前的所有版本的代码:
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) { [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; } else { [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)]; }
如果你想检查是否启用推送通知或不使用下面的代码:
- (BOOL) pushNotificationOnOrOff { if ([UIApplication instancesRespondToSelector:@selector(isRegisteredForRemoteNotifications)]) { return ([[UIApplication sharedApplication] isRegisteredForRemoteNotifications]); } else { UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; return (types & UIRemoteNotificationTypeAlert); } } #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
以上代码只能在Xcode 6+上运行
将这行添加到.m
文件的顶部
#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
1)当您在IOS8
注册推送通知时,您必须注意条件。 在应用程序中添加此代码did finish launch
。
if(IS_IOS_8_OR_LATER) { UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes: (UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound |UIRemoteNotificationTypeAlert) categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:settings]; } else { //register to receive notifications UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound; [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes]; }
2)然后为IOS8
添加这个方法
#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
然后你通知委托方法将被调用。 希望对你有帮助 !!!
运行时和旧的编译器安全选项…如果您运行在旧的xcode(5.0或更早版本)
// changes of API in iOS 8.0 - (void) registerForPushNotification { NSLog(@"registerForPushNotification"); if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) { #if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_8_0 //__IPHONE_8_0 is not defined in old xcode (==0). Then use 80000 NSLog(@"registerForPushNotification: For iOS >= 8.0"); [[UIApplication sharedApplication] registerUserNotificationSettings: [UIUserNotificationSettings settingsForTypes: (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; [[UIApplication sharedApplication] registerForRemoteNotifications]; #endif } else { NSLog(@"registerForPushNotification: For iOS < 8.0"); [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)]; } }
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) { // For iOS 8 [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; [[UIApplication sharedApplication] registerForRemoteNotifications]; } else { // For iOS < 8 [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)]; }
在这之后:
- 从设备中删除没有推送通知的应用程序。
- 完全closures设备并重新打开。
- 进入设置并设置date和时间提前一天
- closures设备并将其closures
- 再次安装应用程序,它会像第一次安装它一样要求通知。
- 如果没有自动设置,请重新设置date/时间。
这将重置通知设置。 重新安装应用程序,所有将正常工作:)
如果你有iOS 8和旧版本,试试这个:
if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) { // For iOS 8 [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; [[UIApplication sharedApplication] registerForRemoteNotifications]; } else { // For iOS < 8 [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)]; }
从苹果的开发网站:
UIUserNotificationSettings
UIUserNotificationSettings对象封装了应用程序可以向用户显示的通知types。 与本地或推送通知结合使用可视或可听警报的应用程序必须注册所使用的警报types。 UIKit将您提供的信息与用户的偏好相关联,以确定允许您的应用程序使用的警报types。
使用此类封装您的初始注册请求并查看请求结果。 创build此类的实例并指定您的首选设置后,请调用UIApplication类的
registerUserNotificationSettings:
方法来注册这些设置。 在根据用户首选项检查您的请求之后,应用程序将结果传递给其应用程序委托的application:didRegisterUserNotificationSettings:
方法。 传递给该方法的对象指定了您的应用程序允许使用的通知types。除了注册您的应用程序的警报types,您还可以使用此类来注册与本地或推送通知一起显示的自定义操作组。 自定义操作代表您的应用可以响应通知而执行的即时任务。 您可以定义一组操作,并将整个组与给定的通知相关联。 当显示相应的警报时,系统会为您指定的每个操作添加button。 当用户点击某个动作的button时,系统会唤醒您的应用程序并调用
application:handleActionWithIdentifier:forRemoteNotification:completionHandler:
或application:handleActionWithIdentifier:forLocalNotification:completionHandler:
其应用程序委托的方法。 使用这些方法来执行请求的操作。
它非常简单:
在xcode6
的项目的didFinishLaunchingWithOptions
中添加以下行
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; [[UIApplication sharedApplication] registerForRemoteNotifications];
The code below resolved: if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) { UIUserNotificationType types; types = [[UIApplication sharedApplication] currentUserNotificationSettings].types; if (types & UIUserNotificationTypeAlert) pushEnabled=YES; else pushEnabled=NO; } else { UIRemoteNotificationType types; types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes]; if (types & UIRemoteNotificationTypeAlert) pushEnabled=YES; else pushEnabled=NO; }
我试图获取设置对象的types信息,发现types属性基本上是一个位掩码。 这里是如何提取信息。
我的例子是在Swift和> = iOS 8.0中。
let settings = UIApplication.sharedApplication().currentUserNotificationSettings() if settings.types.rawValue & UIUserNotificationType.Alert.rawValue == UIUserNotificationType.Alert.rawValue { // can receive alert! } else { // if the user is not even able to receive alerts, show him a hint on how to reenable notifications in system settings } if settings.types.rawValue & UIUserNotificationType.Badge.rawValue == UIUserNotificationType.Badge.rawValue { // can receive badge! } if settings.types.rawValue & UIUserNotificationType.Sound.rawValue == UIUserNotificationType.Sound.rawValue { // can receive sound! }