我在一个应用程序中使用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 谁知道我做错了什么?谢谢你的帮助
答案 0 :(得分:2)
将此添加到您的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 sharedApplication] registerForRemoteNotifications];
答案 1 :(得分:2)
根据window
的Apple文档:
此方法的实现应执行与指定标识符关联的操作,并在完成后立即在completionHandler参数中执行该块。在实施结束时未能执行完成处理程序块将导致您的应用程序被终止。
上面的实现缺少完成处理程序块。
要使其有效,请将application:handleActionWithIdentifier:forLocalNotification:completionHandler:
修改为如下:
application:handleActionWithIdentifier:forLocalNotification:completionHandler:
答案 2 :(得分:1)
当用户在警报中点击远程或本地通知的自定义操作时,此方法会调用。
- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandle
{
//some action
}
当您在屏幕上滑动手指时称为此方法
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
}
答案 3 :(得分:0)
虽然我看到你正在处理“forRemoteNotification:
”案件,
我不认为这是在原始代码块中定义的:
application:handleActionWithIdentifier:forLocalNotification:completionHandler: