推送通知在带有 iOS 8.0 的 iPhone 6 中不起作用
Posted
技术标签:
【中文标题】推送通知在带有 iOS 8.0 的 iPhone 6 中不起作用【英文标题】:Push notification is not working in iPhone 6 with iOS 8.0 【发布时间】:2014-12-29 11:22:18 【问题描述】:我正在使用推送通知开发 iPhone 应用程序。我遇到了通知问题,我在 ios 6、7、8.1、8.1.2 中成功获取了设备令牌 ID,但对于我的 iPhone 6 和 iOS 8.0。
所以在我的推送通知方法“didRegisterForRemoteNotificationsWithDeviceToken”中,令牌 ID 返回 nil。所以,我无法获得推送通知。我不知道是什么问题。
这是我的代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)])
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
else
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
.
.
.
.
[self.window makeKeyAndVisible];
return YES;
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken
NSString *newToken = [deviceToken description];
newToken = [newToken stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
newToken = [newToken stringByReplacingOccurrencesOfString:@" " withString:@""];
[GlobalVariables proTrain].deviceToken = newToken;
NSLog(@"device token id = %@", newToken);
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
[application registerForRemoteNotifications];
【问题讨论】:
【参考方案1】:#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
if (IS_OS_8_OR_LATER)
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
else
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
【讨论】:
【参考方案2】:也许不是这样:
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
试试这个:
[application registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
【讨论】:
感谢您的回复,但是我在iOS8.0中遇到了问题,所以替换上述代码后应该不起作用。【参考方案3】:/*--- Setup Push Notification ---*/
//For iOS 8
if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)] && [UIApplication instancesRespondToSelector:@selector(registerForRemoteNotifications)])
UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
//For iOS 7 & less
else if ([UIApplication instancesRespondToSelector:@selector(registerForRemoteNotificationTypes:)])
UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
通知方式如下:
#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"])
NSLog(@"didReceiveRemoteNotification ios 8");// push some specific view when notification received
#endif
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
self.strDeviceToken = [[[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]] stringByReplacingOccurrencesOfString:@" " withString:@""];
NSLog(@"Device Token ==== %@", self.strDeviceToken);
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
NSLog(@"error : %@",error.localizedDescription);
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
NSLog(@"didReceiveRemoteNotification ios 7");// push some specific view when notification received
【讨论】:
【参考方案4】: 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:
(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
【讨论】:
以上是关于推送通知在带有 iOS 8.0 的 iPhone 6 中不起作用的主要内容,如果未能解决你的问题,请参考以下文章
推送通知 - 适配器 - MFP 从 6.3 迁移到 8.0
在 iPhone 上处理时如何使 iPad 上的 iOS 推送通知无效?
iPhone - 注册推送通知锁定 iPhone 在发布模式下运行 iOS3