从未调用UILocalNotification iOS8 handleActionWithIdentifier

时间:2014-10-02 11:38:07

标签: ios objective-c xcode ios8 uilocalnotification

我已在此模式下创建本地通知:

午餐时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,则通知时会发生火灾 在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开发,我做错了什么?

3 个答案:

答案 0 :(得分:1)

多德。您似乎正在注册远程通知并为本地通知提供处理程序。请注册本地通知以使听众工作。

答案 1 :(得分:0)

你附加任何dispatch_get_main_queue?

我在主队列和应用程序中执行循环: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];
    }
});

答案 2 :(得分:0)

本地通知只需三个步骤:

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

$_

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

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

3.-在您的应用中,您可以通过以下方式设置本地通知:

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

对我而言,它正在使用ios 8.3和swift 1.2