安排 UILocalNotification
Schedule UILocalNotification
我想要在收到通知时可操作的按钮。
为了处理远程推送通知,我添加了以下代码 -
(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];
本地通知已安排,我收到了通知。但我没有看到任何可操作的按钮。
我阅读了 Apple 文档
- (void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification;
好像有用。但这是在单击通知时触发的。
我提到的其他帖子:
https://www.shinobicontrols.com/blog/ios8-day-by-day-day-25-notification-actions
谁能给我指出正确的方向?
kRemindLaterCategory 值和 kMTNRemindLaterCategory 应该是相同的,然后你也可以看到带有本地通知的可操作按钮,如果两者不相同,可操作按钮不可见,因为尚未注册具有本地通知的指定类别。
我想要在收到通知时可操作的按钮。 为了处理远程推送通知,我添加了以下代码 -
(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];
本地通知已安排,我收到了通知。但我没有看到任何可操作的按钮。
我阅读了 Apple 文档
- (void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification;
好像有用。但这是在单击通知时触发的。
我提到的其他帖子:
https://www.shinobicontrols.com/blog/ios8-day-by-day-day-25-notification-actions
谁能给我指出正确的方向?
kRemindLaterCategory 值和 kMTNRemindLaterCategory 应该是相同的,然后你也可以看到带有本地通知的可操作按钮,如果两者不相同,可操作按钮不可见,因为尚未注册具有本地通知的指定类别。