只能统计 iOS 10 之后版本, iOS 10 之前无效
Notification Service Extension 此扩展为苹果官方在 iOS 10 推出的扩展类, 可以在展示 Push 横幅前截取 Push, 对 Push 进行对其展示, 文案, 等其他的东西进行修改. 但是此扩展也可以进行对 Push 到达率进行统计, 因为之前只能统计点击率.
本人开发环境
MacBook Pro 10.12.6
Xcode 9.0
iPhone 6s 11.2.5
Notification Service Extension 概述
如下图所示 APNs 是将消息首先推送给 ServiceExtension ,ServiceExtension 会处理好数据后再展示给用户一个优美的界面, ServiceExtension 就是一个厨师, 收到原材料, 给用户做成美味的食物一样, so sweat!
2379949-77cd32e4ee3934d7.png
在展示用户推送内容前, UNNotificationServiceExtension 是修改远程推送携带的内容. UNNotificationServiceExtension 类 可以让开发者自定义推送展示的内容, Notification Service app extension 不会自己提供推送界面的, 当将适当类型的推送传递给用户的设备时, 它会按需启动. 你可以用 extension 修改推送内容和下载推送相关的资源. 你可以在 extension 中解密和加密的数据或下载推送相关的图片.
你不需要自己创建 UNNotificationServiceExtension 实例, Xcode 已经帮我们创建好了. 当满足如下两个条件, 应用收到远程推送时, 应用会加载 extension 和调用 didReceiveNotificationRequest:withContentHandler: 方法.
在 didReceiveNotificationRequest:withContentHandler: 里面可以处理远程推送内容, 修改远程推送内容的时间是有限的. 如果修改内容任务没有完成, 系统会调用 serviceExtensionTimeWillExpire 方法, 给你提供最后一次提供修改内容的机会. 如果你没有修改远程推送成功, 系统将会展示远程推送最原始的内容.
创建扩展 Target
1, 在原有项目上 new 一个 target, 如下图所示
屏幕快照 2018-01-30 下午 4.23.56.png
2, 创建 Notification Service Extension, 如下图所示
屏幕快照 2018-01-30 下午 4.25.00.png
3, 自定义名称, 其他的不需要更改, 系统会自动配置
截获 push 前提
1, 需要新增一个字段 mutable-content":"1", 与 alert 同级, 如下图所示
lALPBbCc1WBFE6XNARPNAfI_498_275.png_620x10000q90g.jpg
远程推送的 aps 字典中, mutable-content : 1 The remote notification's aps dictionary includes the mutable-content key with the value set to 1.
2,alert 不能为空
远程通知的配置是展示 Alert The remote notification is configured to display an alert.
3, 两个 target 的 info.plist 均需要配置
不进行配置时会报如下图错误
2379949-b128133f158736d1.png
右键点击 info.plist,Open As -> Source Code 将下面的代码填入最后的
</dict> </plist>
前即可
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
调试
1, 先 run 主 target, 如下图所示
屏幕快照 2018-01-30 下午 4.50.49.png
2, 再 run Notification Service Extension, 如下图所示
屏幕快照 2018-01-30 下午 4.50.59.png
3, 最后, 手动选择你的主 target, 或者 search, 如下图所示
屏幕快照 2018-01-30 下午 4.51.11.png
这样在收到 push 时, 就可以进行打印 Log 或者断点进行调试了
统计 Push 到达率
1, 后台以及杀死 app
1) 操作方法
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler
2) 极光推送统计
具体流程可以参考: https://docs.jiguang.cn/jpush/client/iOS/ios_api/#notification-service-extension
3)server 端推送统计
可以让 server 端在 push 中加一个类似 pushId 的字段
在 NotificationService 中的 didReceiveNotificationRequest 方法中获取 self.bestAttemptContent.userInfo
使用 NSURLSessionTask(建议使用官方网络) 进行将规定的 pushId 进行 post 请求发给 server 端进行统计 push 到达率
2, 前台
集成了极光推送时, 可在下方代码中进行网络请求完成统计
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler
未集成时, 可在下方代码中进行网络请求完成统计
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
参考文章
iOS 10 UNNotificationServiceExtension
来源: http://www.jianshu.com/p/ef65afc95c4a