iOS10通知(四)--带事件通知的发送和处理

来源:互联网 发布:nba01年总决赛数据 编辑:程序博客网 时间:2024/06/05 04:30

所谓的带事件通知只是我的一个理解,原意是可交互的通知

1、在NotificationHandle类中增加交互通知的实现方法

//注册通知中的action事件-(void)registerNotificationCategory{    //带评论的通知事件注册    UNTextInputNotificationAction *inputAction = [UNTextInputNotificationAction actionWithIdentifier:@"input" title:@"评论" options:UNNotificationActionOptionForeground textInputButtonTitle:@"发送" textInputPlaceholder:@"请输入评论内容.."];    UNNotificationAction *openAction = [UNNotificationAction actionWithIdentifier:@"open" title:@"打开" options:UNNotificationActionOptionForeground];    UNNotificationAction *cancleAction = [UNNotificationAction actionWithIdentifier:@"cancle" title:@"取消" options:UNNotificationActionOptionDestructive];        UNNotificationCategory *tapCategory = [UNNotificationCategory categoryWithIdentifier:@"comment" actions:@[inputAction,openAction,cancleAction] intentIdentifiers:@[] options:UNNotificationCategoryOptionCustomDismissAction];        //自定义UI通知的时间注册    UNNotificationAction *nextAction = [UNNotificationAction actionWithIdentifier:@"switch" title:@"切换" options:UNNotificationActionOptionAuthenticationRequired];    UNNotificationCategory *customCategory = [UNNotificationCategory categoryWithIdentifier:@"customUI" actions:@[nextAction,openAction,cancleAction] intentIdentifiers:@[] options:UNNotificationCategoryOptionNone];        //    NSSet *set = [[NSSet alloc]initWithObjects:tapCategory, customCategory, nil];    [[UNUserNotificationCenter currentNotificationCenter] setNotificationCategories:set];}
2、修改代理事件中的方法,增加交互处理,红色标注的为新增

-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler{    //收到推送的请求    UNNotificationRequest *request = response.notification.request;        //收到的内容    UNNotificationContent *content = request.content;        //收到用户的基本信息    NSDictionary *userInfo = content.userInfo;        //收到消息的角标    NSNumber *badge = content.badge;        //收到消息的body    NSString *body = content.body;        //收到消息的声音    UNNotificationSound *sound = content.sound;        //推送消息的副标题    NSString *subtitle = content.subtitle;        //推送消息的标题    NSString *title = content.title;        if ([response.notification.request.trigger isKindOfClass:[UNNotificationTrigger class]]) {        NSLog(@"点击了通知:%@\n",userInfo);    }    else{        NSLog(@"通知:{\nbody:%@,\ntitle:%@,\nsubtitle:%@,\nbadge:%@,\nsound:%@,\nuserInfo:%@}",body,title,subtitle,badge,sound,userInfo);    }        //处理消息的事件    NSString *category = content.categoryIdentifier;    if ([category isEqualToString:@"comment"]) {        [self handCommnet:response];    }        completionHandler();}-(void)handCommnet:(UNNotificationResponse *)response{    NSString *actionType = response.actionIdentifier;    NSString *textStr = @"";        if ([actionType isEqualToString:@"input"]) {        UNTextInputNotificationResponse *temp = (UNTextInputNotificationResponse *)response;        textStr = temp.userText;    }    else if ([actionType isEqualToString:@"open"])    {        textStr = @"open";    }    else if ([actionType isEqualToString:@"cancle"])    {        textStr = @"";    }        NSLog(@"你刚输入的内容是:%@",textStr);}
3、在AppDelegate中注册交互事件,红色标注为新增

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    // Override point for customization after application launch.        _window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];    RootViewController *rootVC = [[RootViewController alloc]init];    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:rootVC];        _window.rootViewController = nav;    [_window makeKeyWindow];        [UNUserNotificationCenter currentNotificationCenter].delegate = [NotificationHandle shareInstance];        [[NotificationHandle shareInstance] authorizationPushNotificaton:application];    [[NotificationHandle shareInstance] registerNotificationCategory];        return YES;}
4、发送带交互事件的通知

-(void)btnClicked{    UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc]init];    content.body = @"这是一个带交互事件的通知";    content.title = @"交互事件通知";
//此处的唯一标识和NotificationHandle中的一一对应
    content.categoryIdentifier = @"comment";        UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:3 repeats:NO];    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:@"actionable" content:content trigger:trigger];    [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {        //    }];}

5、远程推送的payload内容

{  "aps":{    "alert":{      "title":"事件通知",      "body":"这是一个带事件的通知"    },    "category":"conment"  //标识这是一个交互通知,用于代理事件中获取后处理  }}

6、收到消息后,通知上使用3D Touch即可看到效果,效果图如下






0 0
原创粉丝点击