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

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

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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值