iOS应用点击Clevertap推送通知启动时崩溃(基于MobileFirst V7.1)
iOS冷启动点击Clevertap推送崩溃(MobileFirst V7.1)
我来帮你搞定这个问题——这种仅应用完全关闭时点击推送崩溃,前台/后台正常的场景,我之前在基于MobileFirst的项目里踩过类似的坑,核心问题基本都和冷启动阶段推送回调的执行时机,与MobileFirst、Clevertap SDK的初始化顺序不匹配有关。
核心原因拆解
- MobileFirst初始化滞后:如果你的代码在
didFinishLaunchingWithOptions里先处理了Clevertap的推送回调,甚至直接调用MobileFirst的推送处理方法,但此时MobileFirst的核心组件还没完成初始化,就很容易触发空指针或者未初始化对象的调用崩溃。 - Payload格式不兼容:冷启动时MobileFirst对推送payload的解析逻辑和后台/前台不一样,Clevertap推送的payload如果包含MobileFirst V7.1未预期的字段(比如自定义字段没放在
custom节点下),就会导致解析失败崩溃。 - 冷启动推送参数处理遗漏:iOS冷启动时,推送信息是通过
didFinishLaunchingWithOptions的launchOptions字典传递的,而不是后台/前台的didReceiveRemoteNotification回调,如果代码没区分这两种场景,就会出错。
具体排查&修复步骤
1. 调整didFinishLaunchingWithOptions的代码顺序
一定要保证MobileFirst SDK初始化优先于任何推送相关处理,包括Clevertap的初始化和推送回调。给你个亲测有效的代码顺序示例:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // 第一步:先初始化MobileFirst核心SDK [[WLClient sharedInstance] initializeWithDelegate:self]; // 第二步:再初始化Clevertap SDK [Clevertap autoIntegrate]; // 第三步:冷启动的推送先不着急处理,放到MobileFirst初始化完成的回调里 // 不要在这里直接调用handleRemoteNotification return YES; }
2. 用MobileFirst初始化回调处理冷启动推送
MobileFirst初始化成功后会触发wlInitDidCompleteWithSuccess方法,我们在这里处理冷启动时的推送就安全了:
- (void)wlInitDidCompleteWithSuccess { NSLog(@"MobileFirst初始化完成,可以处理推送了"); // 取出冷启动时的推送参数 NSDictionary *launchOptions = [[UIApplication sharedApplication] launchOptions]; if (launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]) { // 先用MobileFirst的方法处理推送 [[WLClient sharedInstance] handleRemoteNotification:launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]]; // 别忘了通知Clevertap统计推送点击 [Clevertap recordNotificationViewedWithPayload:launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]]; } }
3. 验证Clevertap推送Payload格式
对比后台/前台正常接收的payload和冷启动崩溃时的payload,重点看是否符合MobileFirst V7.1的要求:
- 必须包含
alert字段 - 自定义字段要放在
custom字典下
如果Clevertap的推送payload不符合这个格式,冷启动解析时就会崩溃,可以在崩溃日志里搜NSNull、unrecognized selector这类关键词,定位具体的字段错误。
4. 从崩溃日志抓关键线索
你手里的崩溃前日志一定要重点看这几点:
- 是否有
[null object performSelector:]这类空指针错误 - 是否有
WL开头的类/方法调用失败的信息 - 是否有Clevertap回调触发时的异常记录
临时应急修复
如果暂时没法调整初始化顺序,可以先把冷启动的推送payload暂存到本地,等MobileFirst初始化完成后再处理:
// 在didFinishLaunchingWithOptions里临时存payload if (launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]) { [[NSUserDefaults standardUserDefaults] setObject:launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey] forKey:@"PendingPushPayload"]; [[NSUserDefaults standardUserDefaults] synchronize]; } // 在wlInitDidCompleteWithSuccess里取出处理 NSDictionary *pendingPayload = [[NSUserDefaults standardUserDefaults] objectForKey:@"PendingPushPayload"]; if (pendingPayload) { [[WLClient sharedInstance] handleRemoteNotification:pendingPayload]; [Clevertap recordNotificationViewedWithPayload:pendingPayload]; [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"PendingPushPayload"]; }
内容的提问来源于stack exchange,提问作者user2436743
相关产品推荐
相关产品推荐

