iOS Interactive UILocalNotifications

时间:2015-07-06 09:21:55

标签: ios objective-c xcode uilocalnotification

我正在尝试实施互动式UILocalNotifications。

以下是我的代码。我无法获得3个接收通知的动作按钮。

UIMutableUserNotificationAction *nAction1 = [[UIMutableUserNotificationAction alloc] init];
nAction1.identifier = @"Present";
nAction1.title = @"Present";
nAction1.activationMode = UIUserNotificationActivationModeBackground;
nAction1.destructive = NO;
nAction1.authenticationRequired = YES;

UIMutableUserNotificationAction *nAction2 = [[UIMutableUserNotificationAction alloc] init];
nAction2.identifier = @"Late";
nAction2.title = @"Late";
nAction2.activationMode = UIUserNotificationActivationModeBackground;
nAction2.destructive = NO;
nAction2.authenticationRequired = YES;

UIMutableUserNotificationAction *nAction3 = [[UIMutableUserNotificationAction alloc] init];
nAction3.identifier = @"Absent";
nAction3.title = @"Absent";
nAction3.activationMode = UIUserNotificationActivationModeBackground;
nAction3.destructive = YES;
nAction3.authenticationRequired = YES;

UIMutableUserNotificationCategory *notificationCategory = [[UIMutableUserNotificationCategory alloc] init];
notificationCategory.identifier = @"Attendance";
[notificationCategory setActions:@[nAction3, nAction2, nAction1] forContext:UIUserNotificationActionContextDefault];
[notificationCategory setActions:@[nAction3, nAction1] forContext:UIUserNotificationActionContextDefault];

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

UIUserNotificationType notificationType = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
UIUserNotificationSettings *notificationSettings = [UIUserNotificationSettings settingsForTypes:notificationType categories:categories];

[[UIApplication sharedApplication] registerUserNotificationSettings:notificationSettings];

UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];
localNotification.alertBody = @"Testing";
localNotification.category = @"Attendance";
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

1 个答案:

答案 0 :(得分:1)

[notificationCategory setActions:@[nAction3, nAction2, nAction1] forContext:UIUserNotificationActionContextDefault];
[notificationCategory setActions:@[nAction3, nAction1] forContext:UIUserNotificationActionContextDefault];

您似乎只用2个按钮覆盖相同的上下文我猜应该是正确的:

[notificationCategory setActions:@[nAction3, nAction2, nAction1] forContext:UIUserNotificationActionContextDefault];
[notificationCategory setActions:@[nAction3, nAction1] forContext:UIUserNotificationActionContextMinimal];