开发者社区> 问答> 正文

iOS10通知如何适配?(2)



4.3 Action和Category

  • iOS 8以上支持,此处仅讲述iOS 10系统的实现方式。
  • 通知支持设置Action点击动作,即在通知上添加按钮,点击按钮可触发回调以此做出不同的逻辑处理;
  • 通知支持Category分类,可将Action和Category进行关联,Category和第6节(通知详情自定义UI)相关。
  • 下面代码自定义id为action1和action2的通知动作,创建id为test_category的通知类别后,将两个Action关联到该category,最后注册category到通知中心。
  • 使用OpenAPI推送通知时,调用setiOSNotificationCategory()接口,可指定通知的类别;创建的test_category类别的通知弹出时如下图所示,test1和test2按钮分别对应id为action1和action2的通知Action;
  • 【注意】Category注册到通知中心需要在推送前完成。

  1. [backcolor=transparent]/**
  2. [backcolor=transparent] *  创建并注册通知category(iOS 10+)
  3. [backcolor=transparent] */
  4. [backcolor=transparent]-[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]void[backcolor=transparent])[backcolor=transparent]createCustomNotificationCategory [backcolor=transparent]{
  5. [backcolor=transparent]    [backcolor=transparent]// 自定义`action1`和`action2`
  6. [backcolor=transparent]    [backcolor=transparent]UNNotificationAction[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]action1 [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent][[backcolor=transparent]UNNotificationAction[backcolor=transparent] actionWithIdentifier[backcolor=transparent]:@[backcolor=transparent]"action1"[backcolor=transparent] title[backcolor=transparent]:@[backcolor=transparent]"test1"[backcolor=transparent] options[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]UNNotificationActionOptionNone[backcolor=transparent]];
  7. [backcolor=transparent]    [backcolor=transparent]UNNotificationAction[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]action2 [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent][[backcolor=transparent]UNNotificationAction[backcolor=transparent] actionWithIdentifier[backcolor=transparent]:@[backcolor=transparent]"action2"[backcolor=transparent] title[backcolor=transparent]:@[backcolor=transparent]"test2"[backcolor=transparent] options[backcolor=transparent]:[backcolor=transparent] [backcolor=transparent]UNNotificationActionOptionNone[backcolor=transparent]];
  8. [backcolor=transparent]    [backcolor=transparent]// 创建id为`test_category`的category,并注册两个action到category
  9. [backcolor=transparent]    [backcolor=transparent]// UNNotificationCategoryOptionCustomDismissAction表明可以触发通知的dismiss回调
  10. [backcolor=transparent]    [backcolor=transparent]UNNotificationCategory[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]category [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent][[backcolor=transparent]UNNotificationCategory[backcolor=transparent] categoryWithIdentifier[backcolor=transparent]:@[backcolor=transparent]"test_category"[backcolor=transparent] actions[backcolor=transparent]:@[[backcolor=transparent]action1[backcolor=transparent],[backcolor=transparent] action2[backcolor=transparent]][backcolor=transparent] intentIdentifiers[backcolor=transparent]:@[][backcolor=transparent] options[backcolor=transparent]:
  11. [backcolor=transparent]                                        [backcolor=transparent]UNNotificationCategoryOptionCustomDismissAction[backcolor=transparent]];
  12. [backcolor=transparent]    [backcolor=transparent]// 注册category到通知中心
  13. [backcolor=transparent]    [backcolor=transparent][[backcolor=transparent]center setNotificationCategories[backcolor=transparent]:[[backcolor=transparent]NSSet[backcolor=transparent] setWithObjects[backcolor=transparent]:[backcolor=transparent]category[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]nil[backcolor=transparent]]];
  14. [backcolor=transparent]}


4.4 通知回调

  • UNUserNotificationCenterDelegate协议定义了通知相关的回调;


4.4.1 设置代理

  • 若要处理通知相关回调,需要实现并指定通知中心对象UNUserNotificationCenter的代理UNUserNotificationCenterDelegate,一般是在AppDelegate中实现,如下所示:
  1. [backcolor=transparent]@interface[backcolor=transparent] [backcolor=transparent]AppDelegate[backcolor=transparent] [backcolor=transparent]()[backcolor=transparent] [backcolor=transparent]<[backcolor=transparent]UNUserNotificationCenterDelegate[backcolor=transparent]>
  2. [backcolor=transparent]@end
  3. [backcolor=transparent]@implementation[backcolor=transparent] [backcolor=transparent]AppDelegate
  4. [backcolor=transparent]...
  5. [backcolor=transparent]UNUserNotificationCenter[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]center [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent][[backcolor=transparent]UNUserNotificationCenter[backcolor=transparent] currentNotificationCenter[backcolor=transparent]];
  6. [backcolor=transparent]center[backcolor=transparent].[backcolor=transparent]delegate[backcolor=transparent] [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]self[backcolor=transparent];
  7. [backcolor=transparent]...
  8. [backcolor=transparent]@end


4.4.2 回调1:App在前台收到通知

  • 当App处于前台,收到通知会触发userNotificationCenter:willPresentNotification:withCompletionHandler:回调;
  • 在回调中可以处理通知相关的字段信息,回调处理结束前需要调用completionHandler(UNNotificationPresentationOptions),UNNotificationPresentationOptions的参数含义如下:UNNotificationPresentationOptionNone,通知不提醒;
  • UNNotificationPresentationOptionSound,通知声音提醒;
  • UNNotificationPresentationOptionAlert,通知内容提醒;
  • UNNotificationPresentationOptionBadge,通知角标提醒。
基于此,App在前台时也可以将通知弹出。
  1. [backcolor=transparent]/**
  2. [backcolor=transparent] *  处理iOS 10通知(iOS 10+)
  3. [backcolor=transparent] */
  4. [backcolor=transparent]-[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]void[backcolor=transparent])[backcolor=transparent]handleiOS10Notification[backcolor=transparent]:([backcolor=transparent]UNNotification[backcolor=transparent] [backcolor=transparent]*)[backcolor=transparent]notification [backcolor=transparent]{
  5. [backcolor=transparent]    [backcolor=transparent]UNNotificationRequest[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]request [backcolor=transparent]=[backcolor=transparent] notification[backcolor=transparent].[backcolor=transparent]request[backcolor=transparent];
  6. [backcolor=transparent]    [backcolor=transparent]UNNotificationContent[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]content [backcolor=transparent]=[backcolor=transparent] request[backcolor=transparent].[backcolor=transparent]content[backcolor=transparent];
  7. [backcolor=transparent]    [backcolor=transparent]NSDictionary[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]userInfo [backcolor=transparent]=[backcolor=transparent] content[backcolor=transparent].[backcolor=transparent]userInfo[backcolor=transparent];
  8. [backcolor=transparent]    [backcolor=transparent]// 通知时间
  9. [backcolor=transparent]    [backcolor=transparent]NSDate[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]noticeDate [backcolor=transparent]=[backcolor=transparent] notification[backcolor=transparent].[backcolor=transparent]date[backcolor=transparent];
  10. [backcolor=transparent]    [backcolor=transparent]// 标题
  11. [backcolor=transparent]    [backcolor=transparent]NSString[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]title [backcolor=transparent]=[backcolor=transparent] content[backcolor=transparent].[backcolor=transparent]title[backcolor=transparent];
  12. [backcolor=transparent]    [backcolor=transparent]// 副标题
  13. [backcolor=transparent]    [backcolor=transparent]NSString[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]subtitle [backcolor=transparent]=[backcolor=transparent] content[backcolor=transparent].[backcolor=transparent]subtitle[backcolor=transparent];
  14. [backcolor=transparent]    [backcolor=transparent]// 内容
  15. [backcolor=transparent]    [backcolor=transparent]NSString[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]body [backcolor=transparent]=[backcolor=transparent] content[backcolor=transparent].[backcolor=transparent]body[backcolor=transparent];
  16. [backcolor=transparent]    [backcolor=transparent]// 角标
  17. [backcolor=transparent]    [backcolor=transparent]int[backcolor=transparent] badge [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent][[backcolor=transparent]content[backcolor=transparent].[backcolor=transparent]badge intValue[backcolor=transparent]];
  18. [backcolor=transparent]    [backcolor=transparent]// 取得通知自定义字段内容,例:获取key为"Extras"的内容
  19. [backcolor=transparent]    [backcolor=transparent]NSString[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]extras [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent][[backcolor=transparent]userInfo valueForKey[backcolor=transparent]:@[backcolor=transparent]"Extras"[backcolor=transparent]];
  20. [backcolor=transparent]    [backcolor=transparent]// 通知打开回执上报
  21. [backcolor=transparent]    [backcolor=transparent][[backcolor=transparent]CloudPushSDK[backcolor=transparent] handleReceiveRemoteNotification[backcolor=transparent]:[backcolor=transparent]userInfo[backcolor=transparent]];
  22. [backcolor=transparent]    [backcolor=transparent]NSLog[backcolor=transparent](@[backcolor=transparent]"Notification, date: %@, title: %@, subtitle: %@, body: %@, badge: %d, extras: %@."[backcolor=transparent],[backcolor=transparent] noticeDate[backcolor=transparent],[backcolor=transparent] title[backcolor=transparent],[backcolor=transparent] subtitle[backcolor=transparent],[backcolor=transparent] body[backcolor=transparent],[backcolor=transparent] badge[backcolor=transparent],[backcolor=transparent] extras[backcolor=transparent]);
  23. [backcolor=transparent]}
  24. [backcolor=transparent]/**
  25. [backcolor=transparent] *  App处于前台时收到通知(iOS 10+)
  26. [backcolor=transparent] */
  27. [backcolor=transparent]-[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]void[backcolor=transparent])[backcolor=transparent]userNotificationCenter[backcolor=transparent]:([backcolor=transparent]UNUserNotificationCenter[backcolor=transparent] [backcolor=transparent]*)[backcolor=transparent]center willPresentNotification[backcolor=transparent]:([backcolor=transparent]UNNotification[backcolor=transparent] [backcolor=transparent]*)[backcolor=transparent]notification withCompletionHandler[backcolor=transparent]:([backcolor=transparent]void[backcolor=transparent] [backcolor=transparent](^)([backcolor=transparent]UNNotificationPresentationOptions[backcolor=transparent]))[backcolor=transparent]completionHandler [backcolor=transparent]{
  28. [backcolor=transparent]    [backcolor=transparent]NSLog[backcolor=transparent](@[backcolor=transparent]"Receive a notification in foregound."[backcolor=transparent]);
  29. [backcolor=transparent]    [backcolor=transparent]// 处理iOS 10通知相关字段信息
  30. [backcolor=transparent]    [backcolor=transparent][[backcolor=transparent]self[backcolor=transparent] handleiOS10Notification[backcolor=transparent]:[backcolor=transparent]notification[backcolor=transparent]];
  31. [backcolor=transparent]    [backcolor=transparent]// 通知不弹出
  32. [backcolor=transparent]    [backcolor=transparent]//completionHandler(UNNotificationPresentationOptionNone);
  33. [backcolor=transparent]    [backcolor=transparent]// 通知弹出,且带有声音、内容和角标(App处于前台时不建议弹出通知)
  34. [backcolor=transparent]    completionHandler[backcolor=transparent]([backcolor=transparent]UNNotificationPresentationOptionSound[backcolor=transparent] [backcolor=transparent]|[backcolor=transparent] [backcolor=transparent]UNNotificationPresentationOptionAlert[backcolor=transparent] [backcolor=transparent]|[backcolor=transparent] [backcolor=transparent]UNNotificationPresentationOptionBadge[backcolor=transparent]);
  35. [backcolor=transparent]}


4.4.3 回调2:点击/清除通知

  • 点击/清除通知时, 可在userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:回调里捕获到这些动作,根据UNNotificationResponse.actionIdentifier可对这些动作进行区分:点击通知打开App,对应UNNotificationDefaultActionIdentifier;
  • 左滑删除通知,对应UNNotificationDismissActionIdentifier,注册Category时,需传入UNNotificationCategoryOptionCustomDismissAction才可以捕获到该动作,具体见4.3节的Category创建和注册;
  • 点击自定义Action,如点击4.3节创建的id为action1和action2的Action,自定义Action点击动作的捕获的好处在于,即使不进入App同样可完成某些逻辑处理。
【注意】两个通知回调是不冲突的,当App处于前台时,收到通知先触发userNotificationCenter:willPresentNotification:withCompletionHandler:回调;之后若有点击通知动作,再触发userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:回调。
  1. [backcolor=transparent]/**
  2. [backcolor=transparent] *  触发通知动作时回调,比如点击、删除通知和点击自定义action(iOS 10+)
  3. [backcolor=transparent] */
  4. [backcolor=transparent]-[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]void[backcolor=transparent])[backcolor=transparent]userNotificationCenter[backcolor=transparent]:([backcolor=transparent]UNUserNotificationCenter[backcolor=transparent] [backcolor=transparent]*)[backcolor=transparent]center didReceiveNotificationResponse[backcolor=transparent]:([backcolor=transparent]UNNotificationResponse[backcolor=transparent] [backcolor=transparent]*)[backcolor=transparent]response withCompletionHandler[backcolor=transparent]:([backcolor=transparent]void[backcolor=transparent] [backcolor=transparent](^)())[backcolor=transparent]completionHandler [backcolor=transparent]{
  5. [backcolor=transparent]    [backcolor=transparent]NSString[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]userAction [backcolor=transparent]=[backcolor=transparent] response[backcolor=transparent].[backcolor=transparent]actionIdentifier[backcolor=transparent];
  6. [backcolor=transparent]    [backcolor=transparent]// 点击通知打开
  7. [backcolor=transparent]    [backcolor=transparent]if[backcolor=transparent] [backcolor=transparent]([[backcolor=transparent]userAction isEqualToString[backcolor=transparent]:[backcolor=transparent]UNNotificationDefaultActionIdentifier[backcolor=transparent]])[backcolor=transparent] [backcolor=transparent]{
  8. [backcolor=transparent]        [backcolor=transparent]NSLog[backcolor=transparent](@[backcolor=transparent]"User opened the notification."[backcolor=transparent]);
  9. [backcolor=transparent]        [backcolor=transparent]// 处理iOS 10通知,并上报通知打开回执
  10. [backcolor=transparent]        [backcolor=transparent][[backcolor=transparent]self[backcolor=transparent] handleiOS10Notification[backcolor=transparent]:[backcolor=transparent]response[backcolor=transparent].[backcolor=transparent]notification[backcolor=transparent]];
  11. [backcolor=transparent]    [backcolor=transparent]}
  12. [backcolor=transparent]    [backcolor=transparent]// 通知dismiss,category创建时传入UNNotificationCategoryOptionCustomDismissAction才可以触发
  13. [backcolor=transparent]    [backcolor=transparent]if[backcolor=transparent] [backcolor=transparent]([[backcolor=transparent]userAction isEqualToString[backcolor=transparent]:[backcolor=transparent]UNNotificationDismissActionIdentifier[backcolor=transparent]])[backcolor=transparent] [backcolor=transparent]{
  14. [backcolor=transparent]        [backcolor=transparent]NSLog[backcolor=transparent](@[backcolor=transparent]"User dismissed the notification."[backcolor=transparent]);
  15. [backcolor=transparent]    [backcolor=transparent]}
  16. [backcolor=transparent]    [backcolor=transparent]NSString[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]customAction1 [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]@[backcolor=transparent]"action1"[backcolor=transparent];
  17. [backcolor=transparent]    [backcolor=transparent]NSString[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]customAction2 [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]@[backcolor=transparent]"action2"[backcolor=transparent];
  18. [backcolor=transparent]    [backcolor=transparent]// 点击用户自定义Action1
  19. [backcolor=transparent]    [backcolor=transparent]if[backcolor=transparent] [backcolor=transparent]([[backcolor=transparent]userAction isEqualToString[backcolor=transparent]:[backcolor=transparent]customAction1[backcolor=transparent]])[backcolor=transparent] [backcolor=transparent]{
  20. [backcolor=transparent]        [backcolor=transparent]NSLog[backcolor=transparent](@[backcolor=transparent]"User custom action1."[backcolor=transparent]);
  21. [backcolor=transparent]    [backcolor=transparent]}
  22. [backcolor=transparent]    [backcolor=transparent]// 点击用户自定义Action2
  23. [backcolor=transparent]    [backcolor=transparent]if[backcolor=transparent] [backcolor=transparent]([[backcolor=transparent]userAction isEqualToString[backcolor=transparent]:[backcolor=transparent]customAction2[backcolor=transparent]])[backcolor=transparent] [backcolor=transparent]{
  24. [backcolor=transparent]        [backcolor=transparent]NSLog[backcolor=transparent](@[backcolor=transparent]"User custom action2."[backcolor=transparent]);
  25. [backcolor=transparent]    [backcolor=transparent]}
  26. [backcolor=transparent]    completionHandler[backcolor=transparent]();
  27. [backcolor=transparent]}

展开
收起
猫饭先生 2017-10-23 11:10:06 1328 0
0 条回答
写回答
取消 提交回答
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
手淘iOS性能优化探索 立即下载
From Java/Android to Swift iOS 立即下载
深入剖析iOS性能优化 立即下载