安排 UILocalNotification
Posted
技术标签:
【中文标题】安排 UILocalNotification【英文标题】:Schedule UILocalNotification 【发布时间】:2016-11-26 08:53:06 【问题描述】:我想要在收到通知时可操作的按钮。 为了处理远程推送通知,我添加了以下代码 -
(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】:kRemindLaterCategory 值和 kMTNRemindLaterCategory 值应该相同,然后您也可以看到带有本地通知的可操作按钮,如果两者不相同,则可操作按钮不可见因为带有本地通知的指定类别尚未注册。
【讨论】:
你是最棒的!非常感谢:D以上是关于安排 UILocalNotification的主要内容,如果未能解决你的问题,请参考以下文章