【问题标题】:handleActionWithIdentifier doesn't workhandleActionWithIdentifier 不起作用
【发布时间】:2015-04-03 00:59:34
【问题描述】:

我在一个应用程序中使用 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 有人知道我在做什么错吗?感谢您的帮助

【问题讨论】:

    标签: ios iphone ios8 uilocalnotification


    【解决方案1】:

    将此添加到您的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];

    【讨论】:

    • 我也面临同样的问题
    【解决方案2】:

    当我看到您正在处理“forRemoteNotification:”案件时, 我没有在您的原始代码块中看到这一点:

    application:handleActionWithIdentifier:forLocalNotification:completionHandler:

    【讨论】:

    • 好的,我将代码更改为: - (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)() )completionHandler 但仍然没有任何反应...
    【解决方案3】:

    根据application:handleActionWithIdentifier:forLocalNotification:completionHandler:的Apple文档:

    此方法的实现应执行与指定标识符相关的操作,并在完成后立即执行 completionHandler 参数中的块。在实现结束时未能执行完成处理程序块将导致您的应用被终止。

    您上面的实现缺少完成处理程序块。

    要使其正常工作,请将您的 application:handleActionWithIdentifier:forLocalNotification:completionHandler: 修改为如下:

    - (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void(^)())completionHandler
    {
         NSLog(@"handleAction");
         if (completionHandler) {
             completionHandler();
         }
    }
    

    【讨论】:

      【解决方案4】:

      当用户点击远程或本地通知的警报中的自定义操作时调用此方法。

       - (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
      {
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-13
        • 1970-01-01
        • 1970-01-01
        • 2016-09-12
        • 1970-01-01
        相关资源
        最近更新 更多