【问题标题】:UILocalNotification iOS8 handleActionWithIdentifier never calledUILocalNotification iOS8 handleActionWithIdentifier 从未调用过
【发布时间】:2014-10-02 11:38:06
【问题描述】:

我已经在这种模式下创建了一个本地通知:

午餐中的 AppDelegate.m

if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
        // Setup each action thar will appear in the notification
        UIMutableUserNotificationAction *acceptAction = [[UIMutableUserNotificationAction alloc] init];
        acceptAction.identifier = @"ACCEPT_ACTION"; // Identifier is returned in handleActionWithIdentifier: after the user taps on an action
        acceptAction.title = @"ACCEPT_TITLE";
        acceptAction.activationMode = UIUserNotificationActivationModeForeground; //Brings the app into the foreground when action tapped

        UIMutableUserNotificationCategory *not_cat = [[UIMutableUserNotificationCategory alloc] init];
        not_cat.identifier = @"testCategory";

        [not_cat setActions:@[acceptAction] forContext:UIUserNotificationActionContextDefault];

        NSSet *categories = [NSSet setWithObjects:not_cat, nil];

        UIUserNotificationSettings* notificationSettings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:categories];
        [[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];
        [[UIApplication sharedApplication] registerForRemoteNotifications];
    } else {
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes: (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
    }

然后我创建一个本地通知:

+ (void)sendLocalNotification:(NotificaObject*)n
{
    UILocalNotification *localNotif = [[UILocalNotification alloc] init];

    // Set the category to the category that contains our action
    localNotif.category = @"testCategory";
    localNotif.alertBody = @"First Test mex, no action";//n.titolo;
    localNotif.soundName = UILocalNotificationDefaultSoundName;
    localNotif.applicationIconBadgeNumber = 1;
    localNotif.userInfo = [NSDictionary dictionaryWithObject:@"0" forKey:@"id"];
    localNotif.fireDate = [NSDate dateWithTimeIntervalSinceNow:5]; //5 seconds
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
    NSLog(@"Sending Local notification");
}

当应用程序 (iOS8) 为 FOREGROUND 时通知 FIRE 在 AppDelegate 中被调用

 - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification

永远不要打电话

- (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler

如果应用程序 (iOS8) 在后台仅调用 didReceiveLocalNotification

我不明白为什么 handleActionWithIdentifier 从不调用

我使用 xcode6.01 并为 ios7/8 开发,我做错了什么?

【问题讨论】:

标签: ios objective-c xcode ios8 uilocalnotification


【解决方案1】:

老兄。看来您正在注册远程通知并为本地通知放置处理程序。请注册本地通知以使侦听器正常工作。

【讨论】:

    【解决方案2】:

    您是否附加任何 dispatch_get_main_queue?

    我在主队列中循环,并且 application:handleActionWithIdentifier:forLocalNotification:completionHandler: 方法永远不会被调用

    dispatch_async(dispatch_get_main_queue(), ^{
        while (YES) {
            [NSThread sleepForTimeInterval:1.0];
        }
    });
    

    解决方案是使用 dispatch_get_global_queue

    dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        while (YES) {
            [NSThread sleepForTimeInterval:1.0];
        }
    });
    

    【讨论】:

      【解决方案3】:

      本地通知只需三个步骤:

      1.- 在 AppDelegate 中,didFinishLaunchingWithOptions 告诉你应用正在处理通知:

      if(UIApplication.instancesRespondToSelector(Selector("registerUserNotificationSettings:")))
          {
              application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: .Sound | .Alert | .Badge, categories: nil))
          }
      

      2.- 为通知编写一个处理程序(本地通知发生时会发生什么):

      func application(application: UIApplication, handleActionWithIdentifier identifier: String?, forLocalNotification notification: UILocalNotification, completionHandler: () -> Void) {
          println("notification is here. Open alert window or whatever")
          // Must be called when finished
          completionHandler()
      }
      

      3.- 在您的应用中,您可以这样设置本地通知:

       var localNotification: UILocalNotification = UILocalNotification()
          localNotification.alertAction = "Your Alarm!"
          localNotification.alertBody = "Your description"
          localNotification.fireDate = NSDate(timeIntervalSinceNow: 10) // your time
          localNotification.soundName = UILocalNotificationDefaultSoundName 
          // there are more and different options
          UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
      

      对我来说,它适用于 ios 8.3 和 swift 1.2

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-03
        相关资源
        最近更新 更多