本文内容
a.注册普通本地通知
b.自定义操作的本地通知
c.自定义可快捷回复的本地通知
注册普通本地通知
- 创建一个本地通知
UILocalNotification *localNotification = [[UILocalNotification alloc]init];
//本地通知出现的时间
localNotification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
localNotification.timeZone = [NSTimeZone defaultTimeZone];
//锁屏界面中通知出现时 "滑动以xxx",alertAction中就是xxx
localNotification.alertAction = @"休息一下";
//本地通知的主体内容
localNotification.alertBody = @"中饭时间";
//本地通知的提醒铃声,可以用mainBundle中的音乐文件
localNotification.soundName = UILocalNotificationDefaultSoundName;
//本地通知的重复周期
localNotification.repeatInterval = NSCalendarUnitWeekday;
//设置userInfo,便于标志出该条通知
NSDictionary *infoDic = [NSDictionary dictionaryWithObject:@"lunchNotification" forKey:@"name"];
localNotification.userInfo = infoDic;
//注册该通知
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
- 在AppDelegate.m中application:didFinishLaunchingWithOptions:添加
if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
-
运行结果如下
自定义操作的本地通知
需要用到
UIMutableUserNotificationAction和UIMutableUserNotificationCategory
- 定义快捷操作
UIMutableUserNotificationAction *acceptAction = [[UIMutableUserNotificationAction alloc] init];
acceptAction.identifier = @"accept";
acceptAction.title = @"好的";
//是否取消提醒
acceptAction.destructive = NO;
//是否需要权限,例如锁屏的时候,执行操作是否需要解锁再执行
acceptAction.authenticationRequired = NO;
//启动app还是后台执行
acceptAction.activationMode = UIUserNotificationActivationModeForeground;
UIMutableUserNotificationAction *rejectAction = [[UIMutableUserNotificationAction alloc] init];
rejectAction.identifier = @"reject";
rejectAction.title = @"忽略";
rejectAction.destructive = NO;
rejectAction.authenticationRequired = NO;
rejectAction.activationMode = UIUserNotificationActivationModeBackground;
- 定义category
UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc]init];
//!!!这个identifier的值,在定义本地通知的时候需要
category.identifier = @"actionCategory";
[category setActions:@[acceptAction,rejectAction] forContext:UIUserNotificationActionContextDefault];
[category setActions:@[acceptAction,rejectAction] forContext:UIUserNotificationActionContextMinimal];
- 修改application:didFinishLaunchingWithOptions:
//最后的categories不是nil,而是上面的category形成的集合类
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:[NSSet setWithObjects:category, nil]]];
- 修改本地通知定义
//需要增加赋予category上述定义的identifier值
localNotification.category = @"actionCategory";
- 点击快捷操作之后
在AppDelegate.m添加方法
application: handleActionWithIdentifier:forLocalNotification:completionHandler:
-(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler {
NSLog(@"按下的是%@",identifier);
//.....
completionHandler();
}
-
运行结果如下
自定义可快捷回复的本地通知
与上述自定义操作类似,只是在定义操作的时候,加上赋值method属性
- 定义快捷回复操作
UIMutableUserNotificationAction *replyAction = [[UIMutableUserNotificationAction alloc]init];
replyAction.identifier = @"replyAction";
replyAction.title = @"回复";
replyAction.authenticationRequired = NO;
replyAction.activationMode = UIUserNotificationActivationModeBackground;
replyAction.behavior = UIUserNotificationActionBehaviorTextInput;
- 定义category
UIMutableUserNotificationCategory *replyCategory = [[UIMutableUserNotificationCategory alloc]init];
replyCategory.identifier = @"replyCategory";
[replyCategory setActions:@[replyAction] forContext:UIUserNotificationActionContextDefault];
不要忘了修改registerUserNotificationSettings中category的值
- 获取文本框中的内容
在AppDelegate.m添加方法
application: handleActionWithIdentifier:forLocalNotification:withResponseInfo:completionHandler:
-(void)application:(UIApplication *)application handleActionWithIdentifier:(nullable NSString *)identifier forLocalNotification:(UILocalNotification *)notification withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void(^)())completionHandler{
NSLog(@"按下的是%@",identifier);
if (responseInfo[UIUserNotificationActionResponseTypedTextKey] && [responseInfo[UIUserNotificationActionResponseTypedTextKey] isKindOfClass:[NSString class]] ) {
NSString *response = responseInfo[UIUserNotificationActionResponseTypedTextKey];
NSLog(@"回复的是:%@",response);
}
completionHandler();
}
-
运行结果如下