(目前用的個推+神策,程序最低兼容iOS10)
收到push時加埋點事件pushShow和點擊push時加埋點事件pushClick需求,分四種情況
A.APP退到后台時,點擊push
B.APP運行時,點擊push
C.APP運行時,收到push
D.APP退到后台時,收到push
E.APP殺死,收到push
F.APP殺死,點擊push
針對A、B、F,會執行以下方法,加埋點pushClick即可
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler{} |
針對C,會執行個推的透傳消息,在下面的方法里寫個本地通知
- (void)GeTuiSdkDidReceivePayloadData:(NSData *)payloadData andTaskId:(NSString *)taskId andMsgId:(NSString *)msgId andOffLine:(BOOL)offLine fromGtAppId:(NSString *)appId{} |
然后執行以下方法,加埋點pushShow即可
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(nonnull UNNotification *)notification withCompletionHandler:(nonnull void (^)(UNNotificationPresentationOptions))completionHandler{} |
針對D、E,挺麻煩的,慢慢看吧
1)提醒這個方法不會走
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{} |
2)提醒這個方法不會走
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(nonnull UNNotification *)notification withCompletionHandler:(nonnull void (^)(UNNotificationPresentationOptions))completionHandler{} |
3)提醒這個方法不會走
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler |
只能另想辦法了,走的蘋果的apns。
第一步,添加 NotificationService (切記推送內容格式里含"mutable-content": 1, 才能走通知擴展,后台必須給返回這個字段),添加方式請看:
個推文檔 http://docs.getui.com/getui/mobile/ios/xcode/
https://www.jianshu.com/p/26b96b991eaf
最容易忽視的點,遇到問題卡半天,請看:
https://www.jianshu.com/p/12aa81d1f859
第二步,想要走以下的方法,需要進行以下操作才能調試:使用Xcode “Debug”下的“Attach to Process By PID or NAME”再真機調試
或者在以下方法里新增本地通知,便於測試是否收到push,有時候不會走里面的斷點。
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler{} |
測試本地通知的代碼如下:
#pragma mark todo 以下本地推送為測試效果代碼 斷點不會走。 更換為埋點代碼 UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init]; content.body = @"自定義查找刪除"; content.userInfo = @{@"hello":@"word"}; content.sound = [UNNotificationSound defaultSound]; content.badge = @(100); content.title = @"自定義處理"; UNNotificationRequest *aNewRequest = [UNNotificationRequest requestWithIdentifier:@"Notif" content:content trigger:nil]; [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:aNewRequest withCompletionHandler:^(NSError * _Nullable error) { }]; #pragma mark todo 以上本地推送為測試效果代碼 斷點不會走。 更換為埋點代碼 |
第三步,在以上方法里新增神策埋點代碼,加埋點pushShow即可,需要按照以下文檔
https://cf.sensorsdata.cn/pages/viewpage.action?pageId=7547239
第四步,開發者證書需要支持App Groups服務,應用和擴展的Groups ID需要與證書、神策存取的一致。
切記:上傳App Store限制,避免審核被拒絕
使用了 Notification Service Extension 的項目在制作待上傳至 AppStore 的 IPA 包時,編譯設備需要選擇 Generic iOS Device
,然后再進行 Product -> Archive
操作。只有選擇 Generic iOS Device
才能打包編譯出帶有 Notification Service Extension 且適配全機型設備的 IPA 包
總結:
點擊push的埋點,APP不區分殺死、退到后台、運行中,都在didReceiveNotificationResponse中埋點pushClick;
收到push的埋點,APP進程殺死、退到后台,走的蘋果apns,都在NotificationService的didReceiveNotificationRequest中埋點pushShow;
收到push的埋點,APP運行中,走的個推的透傳,在willPresentNotification中埋點pushShow;
收到push、點擊push,神策不會立即上傳,會保存到本地,下次打開APP才會上傳。然后再等幾分鍾 ,去后台查看是否埋點成功,神策的機制是這樣的。