我在一个应用程序中使用 UILocalNotification,在 iOS 7 或更早版本中工作正常,但在 iOS 8 中 didReceiveLocalNotification 根本没有被调用。 经过一番研究,我发现在 iOS 8 中我们必须使用 handleActionWithIdentifier 来代替,所以我尝试了它,但当 LocalNotification 运行时,这个 void 也没有被调用。 这是我的代码:
我在 didFinishLaunchingWithOptions 中找到了这段代码,它工作正常,第一次启动该应用程序时,我被要求允许发出通知等。
if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
然后我得到:
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
//register to receive notifications
[application registerForRemoteNotifications];
}
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification
completionHandler:(void(^)())completionHandler
{
NSLog(@"handleAction");
}
-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
NSLog(@"didReceive");
}
当 localNotification 运行并且我在屏幕上显示的消息上滑动手指时,没有任何反应...日志根本没有被调用... 我还注意到,如果应用程序处于事件状态并正在运行,则当 localNotification 运行时,它会调用 didReceiveLocalNotification 方法 有人知道我做错了什么吗?感谢您的帮助
最佳答案
将此添加到您的didFinishLaunchingWithOptions:
if ([[UIApplication sharedApplication]respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
{
// iOS 8 Notifications
[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[[UIApplication sharedApplication] registerForRemoteNotifications];
}
else
{
// iOS < 8 Notifications
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
}
您忘记添加这一行:
[[UIApplication共享应用程序]registerForRemoteNotifications];
关于ios - handleActionWithIdentifier 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28305562/