ios - handleActionWithIdentifier 不起作用

标签 ios iphone ios8 uilocalnotification

我在一个应用程序中使用 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/

相关文章:

iOS 8.3 打破自动单元格高度

swift - UISplitViewController displayModeButtonItem()

ios - 用几秒钟显示 iPhone 锁屏(自己使用)

iphone - 这些真的是我最终为 iPhone 编程需要采取的步骤吗?

iphone - 本地化 - 美国居民的码数或英尺数?

iphone - Apple AppStore 是否接受私有(private)或受密码保护的应用程序?

ios - animatewithduration在iOS 8中不起作用

ios - 按钮标签自动更改

ios - Facebook 登录 Swift 项目

ios - UIKeyboardWillChangeFrameNotification 并不总是被调用