====== APNs 送达统计 ====== 此文档是建立在已经集成了环信推送服务的基础上,为您提供统计苹果远程推送(APNs)送达的功能。\\ 您需要在您的应用里创建推送服务扩展(推送扩展服务仅支持iOS10及以上版本),在推送扩展里导入环信扩展服务SDK,调用对应的api即可。\\ 注意:APNs的点击已在sdk内部实现。\\ ===== 1、推送服务扩展介绍 ===== iOS10 苹果增加了推送通知服务应用扩展(Notification Service Extension),用户可以在扩展基础上对远程的推送内容进行修改。 \\ 前提是推送时候在payload中必须设置mutable-content值为1,否则推送扩展将不会被调用。\\ 需要注意的是,一般系统会自动给你生成扩展应用的''%%Bundle Identifier%%'',不做修改即可。如果修改了的话必须在主应用的Bundle Identifier基础上做添加,否则将会有错误。比如主应用的Bundle Identifier为com.xx.targetname,那么扩展应用的Bundle Identifier应该为com.xx.targetname.yy。 \\ 在创建推送服务扩展后系统默认给你创建了两个回调方法,使用didReceiveNotificationRequest:withContentHandler:方法修改推送内容,系统会给你大约30s的时间处理这个推送,如果超过这个时间,系统将调serviceExtensionTimeWillExpire方法,此时您必须立即向系统返回您所能返回的任何内容。如果从任何一个方法中调用完成处理程序失败,系统将显示通知的原始内容。 \\ ===== 2、为应用创建推送服务扩展 ===== === 点击xcode,选择 File > New > Target === {{:playground:push:apppush:1.png|}} === 在iOS > Application Extension下选中 Notification Service Extension,点击next === {{:playground:push:apppush:2.png|}} === 为您的应用程序扩展指定名称和其他配置细节,点击完成。 === {{:playground:push:apppush:3.png|插入图片3}} === 创建完后在工程里会自动生成三个文件(NotificationService.h、NotificationService.m、info.plist)和扩展应用的target(工程名就是创建时候填的Product Name) === {{:playground:push:apppush:4.png|插入图片4}} ===== 3、推送服务扩展导入SDK ===== 推送扩展SDK支持手动和pod导入,您可以选择任意一种方式进行导入。 ==== 通过pod导入 ==== 在集成环信推送服务时候,您为主工程已创建了Podfile文件,在此文件基础上,您可以为扩展添加pod管理,在Podfile文件里指明对应的target,再添加需要导入的SDK名称就可以。 target 'EMPushServerExt' do pod 'EMPushExtension' end 然后在Podfile目录下,执行更新命令就可以。 pod install --repo-update ==== 手动导入 ==== APNs的送达统计sdk下载地址:[[https://downloadsdk.easemob.com/downloads/EMPushExtension0_1_0.zip|下载推送扩展SDK]] \\ 下载下来之后,将EMPushExtension.framework加入到应用扩展目录下即可。 {{:playground:push:apppush:5.png|插入图片5}} ===== 4、SDK使用 ===== 在NotificationService.m里引入头文件 #import 在系统提供处理推送的方法里调用EMPushServiceExt的两个方法,这两个方法必须都调用,且设置appkey的方法需要先调用。 - (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler { self.contentHandler = contentHandler; self.bestAttemptContent = [request.content mutableCopy]; // Modify the notification content here... [EMPushServiceExt setAppkey:@"appkey from easemob"];//appkey必须和主应用中的appkey一致 [EMPushServiceExt receiveRemoteNotificationRequest:request completion:^(NSError * _Nonnull error) { NSLog(@"EMPushServiceExt complete APNs delivery"); self.contentHandler(self.bestAttemptContent); }]; self.contentHandler(self.bestAttemptContent); } - (void)serviceExtensionTimeWillExpire { // Called just before the extension will be terminated by the system. // Use this as an opportunity to deliver your "best attempt" at modified content, otherwise the original push payload will be used. self.contentHandler(self.bestAttemptContent); }