开发者社区> 问答> 正文

iOS API是什么?(3)



2.3 通知打开监听



iOS 10+

  • 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] sendNotificationAck[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]}
  • 触发通知动作时回调,比如点击、删除通知和点击自定义action(iOS 10+);
  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]}



iOS 10以下版本

  • App处于关闭状态时,点击打开通知;
  1. [backcolor=transparent]-[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]BOOL[backcolor=transparent])[backcolor=transparent]application[backcolor=transparent]:([backcolor=transparent]UIApplication[backcolor=transparent] [backcolor=transparent]*)[backcolor=transparent]application didFinishLaunchingWithOptions[backcolor=transparent]:([backcolor=transparent]NSDictionary[backcolor=transparent] [backcolor=transparent]*)[backcolor=transparent]launchOptions [backcolor=transparent]{
  2. [backcolor=transparent]    [backcolor=transparent]// 点击通知将App从关闭状态启动时,将通知打开回执上报
  3. [backcolor=transparent]    [backcolor=transparent]// [CloudPushSDK handleLaunching:launchOptions];(Deprecated from v1.8.1)
  4. [backcolor=transparent]    [backcolor=transparent][[backcolor=transparent]CloudPushSDK[backcolor=transparent] sendNotificationAck[backcolor=transparent]:[backcolor=transparent]launchOptions[backcolor=transparent]];
  5. [backcolor=transparent]    [backcolor=transparent]return[backcolor=transparent] YES[backcolor=transparent];
  6. [backcolor=transparent]}
  • App处于打开状态时,点击打开通知;
  1. [backcolor=transparent]-[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]void[backcolor=transparent])[backcolor=transparent]application[backcolor=transparent]:([backcolor=transparent]UIApplication[backcolor=transparent]*)[backcolor=transparent]application didReceiveRemoteNotification[backcolor=transparent]:([backcolor=transparent]NSDictionary[backcolor=transparent]*)[backcolor=transparent]userInfo [backcolor=transparent]{
  2. [backcolor=transparent]    [backcolor=transparent]NSLog[backcolor=transparent](@[backcolor=transparent]"Receive one notification."[backcolor=transparent]);
  3. [backcolor=transparent]    [backcolor=transparent]// 取得APNS通知内容
  4. [backcolor=transparent]    [backcolor=transparent]NSDictionary[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]aps [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent][[backcolor=transparent]userInfo valueForKey[backcolor=transparent]:@[backcolor=transparent]"aps"[backcolor=transparent]];
  5. [backcolor=transparent]    [backcolor=transparent]// 内容
  6. [backcolor=transparent]    [backcolor=transparent]NSString[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]content [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent][[backcolor=transparent]aps valueForKey[backcolor=transparent]:@[backcolor=transparent]"alert"[backcolor=transparent]];
  7. [backcolor=transparent]    [backcolor=transparent]// badge数量
  8. [backcolor=transparent]    [backcolor=transparent]NSInteger[backcolor=transparent] badge [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent][[[backcolor=transparent]aps valueForKey[backcolor=transparent]:@[backcolor=transparent]"badge"[backcolor=transparent]][backcolor=transparent] integerValue[backcolor=transparent]];
  9. [backcolor=transparent]    [backcolor=transparent]// 播放声音
  10. [backcolor=transparent]    [backcolor=transparent]NSString[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]sound [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent][[backcolor=transparent]aps valueForKey[backcolor=transparent]:@[backcolor=transparent]"sound"[backcolor=transparent]];
  11. [backcolor=transparent]    [backcolor=transparent]// 取得Extras字段内容
  12. [backcolor=transparent]    [backcolor=transparent]NSString[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]Extras[backcolor=transparent] [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent][[backcolor=transparent]userInfo valueForKey[backcolor=transparent]:@[backcolor=transparent]"Extras"[backcolor=transparent]];[backcolor=transparent] [backcolor=transparent]//服务端中Extras字段,key是自己定义的
  13. [backcolor=transparent]    [backcolor=transparent]NSLog[backcolor=transparent](@[backcolor=transparent]"content = [%@], badge = [%ld], sound = [%@], Extras = [%@]"[backcolor=transparent],[backcolor=transparent] content[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]long[backcolor=transparent])[backcolor=transparent]badge[backcolor=transparent],[backcolor=transparent] sound[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]Extras[backcolor=transparent]);
  14. [backcolor=transparent]    [backcolor=transparent]// iOS badge 清0
  15. [backcolor=transparent]    application[backcolor=transparent].[backcolor=transparent]applicationIconBadgeNumber [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]0[backcolor=transparent];
  16. [backcolor=transparent]    [backcolor=transparent]// 通知打开回执上报
  17. [backcolor=transparent]    [backcolor=transparent]// [CloudPushSDK handleReceiveRemoteNotification:userInfo];(Deprecated from v1.8.1)
  18. [backcolor=transparent]    [backcolor=transparent][[backcolor=transparent]CloudPushSDK[backcolor=transparent] sendNotificationAck[backcolor=transparent]:[backcolor=transparent]userInfo[backcolor=transparent]];
  19. [backcolor=transparent]}

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

相关电子书

更多
Spring Boot2.0实战Redis分布式缓存 立即下载
CUDA MATH API 立即下载
API PLAYBOOK 立即下载