IM 即时通讯 (438)
Android (207)
- 初始化&链接 (8)
- 事件&监听处理 (7)
- 用户信息 (13)
- 会话列表 (15)
- 聊天会话 (26)
- 消息处理 (35)
- 自定义消息 (6)
- 音视频 (2)
- 推送&通知 (33)
- 扩展功能 (4)
- 第三方地图 (3)
- 依赖&配置 (9)
- 升级说明 (3)
- 其他 (43)
iOS (166)
- SDK 导入 (8)
- 连接 (8)
- 事件处理 (2)
- 用户信息 (3)
- 会话列表 (14)
- 聊天会话 (44)
- 消息处理 (25)
- 自定义消息 (8)
- 推送&通知 (19)
- 扩展功能 (5)
- 国际化 (3)
- 音视频 (4)
- 其他 (23)
Web (36)
Server (29)
用户如何实现接收推送撤回通知栏指定通知?
IM SDK 2.9.9 版本增加了撤回通知功能,即当用户接收到撤回消息 (RCRecallCommandMessage) 时,将通知栏指定通知删除。
当 APP 在后台活跃时,由 SDK 提供撤回通知的功能,当 APP 不活跃时,用户按照如下方式实现撤回通知功能。
发布时间: 2019-02-18 15:20
回答:
实现方式请参照 SealTalk 中的 ServiceExtension。
具体步骤如下:
给工程添加一个 target :Notification Service Extension。
主工程开启 Push Notifications 和 Background Modes 功能。
Service Extension target 开启 Push Notifications 功能。
NotificationService.m 中实现
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler
:
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler { UNNotificationContent *content1 = request.content; NSDictionary *userInfo = content1.userInfo; NSDictionary *aps = [userInfo objectForKey:@"aps"]; NSString *category = [aps objectForKey:@"category"]; if (aps && category && [category isEqualToString:@"RC:RcCmd"]) { NSString *appData = [userInfo objectForKey:@"appData"]; NSDictionary *appDataDic = [NSJSONSerialization JSONObjectWithData:[appData dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:nil]; NSString *idString = [appDataDic objectForKey:@"rc-dlt-identifier"]; if (idString) { [[UNUserNotificationCenter currentNotificationCenter] getDeliveredNotificationsWithCompletionHandler:^(NSArray<UNNotification *> * _Nonnull notifications) { if (notifications.count == 0) { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ self.contentHandler = contentHandler; self.bestAttemptContent = [request.content mutableCopy]; self.contentHandler(self.bestAttemptContent); }); return; } for (UNNotification *notice in notifications) { UNNotificationRequest *request = notice.request; UNNotificationContent *content = request.content; NSDictionary *userInfo1 = content.userInfo; NSString *identifier = userInfo1[@"rc"][@"id"]; if ([idString isEqualToString:identifier]) { [[UNUserNotificationCenter currentNotificationCenter] removeDeliveredNotificationsWithIdentifiers:@[request.identifier]]; } } dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ self.contentHandler = contentHandler; self.bestAttemptContent = [request.content mutableCopy]; self.contentHandler(self.bestAttemptContent); }); }]; } } else { self.contentHandler = contentHandler; self.bestAttemptContent = [request.content mutableCopy]; self.contentHandler(self.bestAttemptContent); } }
撤回消息的推送内容示例:
{ appData = "{\"rc-dlt-identifier\":\"B7EE-8T9S-OI44-8NSS\"}"; aps = { alert = { body = "\U4f60\U6536\U5230\U4e86\U4e00\U6761\U6d88\U606f"; }; badge = 2; category = "RC:RcCmd"; "mutable-content" = 1; sound = "aaaaaa.caf"; }; rc = { cType = PR; fId = 1xTlsmAn9; id = "B7EE-918C-GI64-8NSS"; oName = "RC:RcCmd"; sourceType = 0; tId = 1xTlsmAn9; }; richMediaUri = ""; }
其中通过 aps
中的 category
判断是不是撤回消息,如果是撤回消息,appData
中的 rc-dlt-identifier
表示要撤回消息的 id
。
注:
只有公有云支持该功能;
该功能只在 iOS 10 以上有效。