本地通知覆盖iOS 9中的远程通知(可操作通知)设置?

时间:2016-06-22 05:07:05

标签: ios objective-c push-notification uilocalnotification

我使用以下代码进行远程通知

 UIMutableUserNotificationAction *action1;
    action1 = [[UIMutableUserNotificationAction alloc] init];
    [action1 setActivationMode:UIUserNotificationActivationModeBackground];
    [action1 setTitle:@"REJECT"];
    [action1 setIdentifier:NotificationActionOneIdent];
    [action1 setDestructive:NO];
    [action1 setAuthenticationRequired:NO];

    UIMutableUserNotificationAction *action2;
    action2 = [[UIMutableUserNotificationAction alloc] init];
    [action2 setActivationMode:UIUserNotificationActivationModeBackground];////UIUserNotificationActivationModeBackground
    [action2 setTitle:@"ACCEPT"];
    [action2 setIdentifier:NotificationActionTwoIdent];
    [action2 setDestructive:NO];
    [action2 setAuthenticationRequired:NO];

    UIMutableUserNotificationCategory *actionCategory;
    actionCategory = [[UIMutableUserNotificationCategory alloc] init];
    [actionCategory setIdentifier:NotificationCategoryIdent];
    [actionCategory setActions:@[action1, action2]
                    forContext:UIUserNotificationActionContextDefault];

    NSSet *categories = [NSSet setWithObject:actionCategory];

    //Right, that is the point
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:
                                             UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:categories];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    //register to receive notifications
    [UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];

这可以很好地显示正确的(接受/拒绝按钮)。对于某些情况,我想将应用程序唤醒到Foreground,所以我在

中使用以下本地通知代码
  
      
  • (void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier   forRemoteNotification:(NSDictionary *)userInfo completionHandler :( void   (^)())completionHandler方法。
  •   
  UIMutableUserNotificationAction *action1;
    action1 = [[UIMutableUserNotificationAction alloc] init];
    [action1 setActivationMode:UIUserNotificationActivationModeForeground];
    [action1 setTitle:@"LAUNCH"];
    [action1 setIdentifier:@"OPEN_ACTION"];
    [action1 setDestructive:NO];
    [action1 setAuthenticationRequired:NO];

    UIMutableUserNotificationCategory *actionCategory;
    actionCategory = [[UIMutableUserNotificationCategory alloc] init];
    [actionCategory setIdentifier:@"LOCAL_NOTIFICATIOn"];
    [actionCategory setActions:@[action1]
                    forContext:UIUserNotificationActionContextDefault];
    [actionCategory setActions:@[action1]
                    forContext:UIUserNotificationActionContextMinimal];

    NSSet *categories1 = [NSSet setWithObject:actionCategory];

    UIUserNotificationSettings *settings2 = [UIUserNotificationSettings settingsForTypes:
                                            UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:categories1];

    [[UIApplication sharedApplication] registerUserNotificationSettings:settings2];

    UILocalNotification* localNotification = [[UILocalNotification alloc] init];
    localNotification.fireDate = [NSDate date];
    localNotification.alertTitle= @"Security settings enabled,";
    localNotification.alertBody = @"tap the Launch button to start the application";
    localNotification.category = @"LOCAL_NOTIFICATIOn";
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

问题:首次远程通知显示正确接受/拒绝按钮但注册本地通知后远程通知不显示操作按钮(接受/拒绝)。我无法在警报中看到按钮?

1 个答案:

答案 0 :(得分:3)

您的远程通知设置被本地通知设置覆盖。

// Registering UIUserNotificationSettings more than once results in previous settings being overwritten.

- (void)registerUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings NS_AVAILABLE_IOS(8_0) __TVOS_PROHIBITED;

评论此代码:

 [[UIApplication sharedApplication] registerUserNotificationSettings:settings2];