UILocalNotification

本文内容
a.注册普通本地通知
b.自定义操作的本地通知
c.自定义可快捷回复的本地通知

注册普通本地通知

  1. 创建一个本地通知
    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];
  1. 在AppDelegate.m中application:didFinishLaunchingWithOptions:添加
if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]){
        [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
    }
  1. 运行结果如下


    UILocalNotification_第1张图片
    本地通知

自定义操作的本地通知

需要用到
UIMutableUserNotificationAction和UIMutableUserNotificationCategory

  1. 定义快捷操作
    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;
  1. 定义category
    UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategory alloc]init];
//!!!这个identifier的值,在定义本地通知的时候需要
    category.identifier = @"actionCategory";
    [category setActions:@[acceptAction,rejectAction] forContext:UIUserNotificationActionContextDefault];
    [category setActions:@[acceptAction,rejectAction] forContext:UIUserNotificationActionContextMinimal];
  1. 修改application:didFinishLaunchingWithOptions:
//最后的categories不是nil,而是上面的category形成的集合类
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:[NSSet setWithObjects:category, nil]]];
  1. 修改本地通知定义
//需要增加赋予category上述定义的identifier值
localNotification.category = @"actionCategory";
  1. 点击快捷操作之后
    在AppDelegate.m添加方法
    application: handleActionWithIdentifier:forLocalNotification:completionHandler:
-(void)application:(UIApplication *)application handleActionWithIdentifier:(NSString *)identifier forLocalNotification:(UILocalNotification *)notification completionHandler:(void (^)())completionHandler  {
      NSLog(@"按下的是%@",identifier);
      //.....
      completionHandler();
}
  1. 运行结果如下


    UILocalNotification_第2张图片
    快捷操作的本地通知锁屏

    UILocalNotification_第3张图片
    快捷操作的本地通知后台

自定义可快捷回复的本地通知

与上述自定义操作类似,只是在定义操作的时候,加上赋值method属性

  1. 定义快捷回复操作
    UIMutableUserNotificationAction *replyAction = [[UIMutableUserNotificationAction alloc]init];
    replyAction.identifier = @"replyAction";
    replyAction.title = @"回复";
    replyAction.authenticationRequired = NO;
    replyAction.activationMode = UIUserNotificationActivationModeBackground;
    replyAction.behavior = UIUserNotificationActionBehaviorTextInput;
  1. 定义category
    UIMutableUserNotificationCategory *replyCategory = [[UIMutableUserNotificationCategory alloc]init];
    replyCategory.identifier = @"replyCategory";
    [replyCategory setActions:@[replyAction] forContext:UIUserNotificationActionContextDefault];

不要忘了修改registerUserNotificationSettings中category的值

  1. 获取文本框中的内容
    在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();
}
  1. 运行结果如下


    UILocalNotification_第4张图片
    快捷回复的本地通知后台

    点击发送结果

参考资料

注册通知官方文档说明

你可能感兴趣的:(UILocalNotification)