安排UILocalNotification

时间:2016-11-26 08:53:06

标签: objective-c ios9 action uilocalnotification localnotification

收到通知后我想要可操作的按钮。 为了处理远程推送通知,我添加了以下代码 -

(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UIApplication *application = [UIApplication sharedApplication];

if( SYSTEM_VERSION_LESS_THAN( @"10.0" ) ) {
    UIMutableUserNotificationAction *action1 = [[UIMutableUserNotificationAction alloc] init];
    [action1 setActivationMode:UIUserNotificationActivationModeForeground];
    [action1 setTitle:@"action1Text"];
    [action1 setIdentifier:@"action1id"];
    [action1 setDestructive:NO];
    [action1 setAuthenticationRequired:NO];

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

    NSSet *categories = [NSSet setWithObject:actionCategory];
    UIUserNotificationType types = (UIUserNotificationTypeAlert|
                                    UIUserNotificationTypeSound|
                                    UIUserNotificationTypeBadge);

    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:types categories:categories];

    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];

}
}

完美无缺。我能够看到按钮并处理动作。现在,在单击按钮并基于更多逻辑时,我想使用与上面定义的相同的可操作按钮来安排本地通知。我添加了代码(不在AppDelegate文件中):

NSDate *date = [NSDate dateWithTimeIntervalSinceNow:10];
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = date;
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.alertBody = @"title 4";
localNotification.hasAction = YES;
localNotification.alertAction = @"action";
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.category = kRemindLaterCategory;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

安排了本地通知,我收到了通知。但我没有看到任何可操作的按钮。

我读了苹果文档

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

似乎很有用。但是在单击通知时会触发此操作。 我提到的其他帖子: https://www.shinobicontrols.com/blog/ios8-day-by-day-day-25-notification-actions actions and categories don't show in UILocalNotification in iOS How can I add action buttons/actions to UILocalNotification alert?

任何人都可以指出我正确的方向吗?

1 个答案:

答案 0 :(得分:1)

kRemindLaterCategory 值和 kMTNRemindLaterCategory 应该相同,然后您也可以看到带有本地通知的可操作按钮,如果两者不相同,则无法看到可操作按钮因为已分配的带有本地通知的类别尚未注册。