启用 iOS 8 的设备在代码更新后未收到推送通知 [英] iOS 8 enabled device not receiving PUSH notifications after code update
问题描述
我最近将我的一台测试 iphone 升级到 iOS 8,然后升级了 PUSH 注册码如下(使用 xCode 6)
I recently upgraded one of my test iphones to iOS 8 and then upgraded the PUSH registration code as below (using 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)];
}
}
尽管进行了此升级并且 [[UIApplication sharedApplication] currentUserNotificationSettings] 显示设备已启用推送,但我没有收到推送通知.
Despite this upgrade and the fact that [[UIApplication sharedApplication] currentUserNotificationSettings] shows PUSH is enabled for the device, I am not receiving PUSH notifications.
就他们而言,我正在使用 Parse 并按本书做所有事情(https://parse.com/tutorials/ios-push-notifications) .
I am using Parse and doing everything by the book as far as they are concerned ( https://parse.com/tutorials/ios-push-notifications) .
有人遇到同样的问题吗?还有什么我可能遗漏的吗?
Is anyone experiencing the same issue? Is there something else that I may be missing ?
推荐答案
iOS 8 更改了注册推送通知的方式:以下是iOS 9之前所有版本的代码:
The way to register for push notifications has been changed in iOS 8: Below is the code for all versions till 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)];
}
如果您想检查是否启用推送通知,请使用以下代码:
In case you want to check whether push notifications are enabled or not use below code:
- (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+ 上运行...
Above code will run on Xcode 6+ only...
这篇关于启用 iOS 8 的设备在代码更新后未收到推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!