You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MoEngage:应用被杀死时推送通知的深度链接导航失效问题

更优方案:MoEngage React Native SDK 冷启动深度链接修复

针对你在MoEngage React Native SDK 9.1.0中遇到的冷启动深度链接失效问题(iOS已禁用swizzling),以下是更规范、易维护的处理方案,替代你当前的临时修改:


Android 端:利用MoEngage推送回调直接对接Linking系统

避免直接修改Intent,通过MoEngage官方监听接口捕获推送点击事件,将深度链接传递给React Native的Linking模块:

// MainApplication.kt
import com.moengage.core.MoEngage
import com.moengage.core.MoEPushMessageListener
import com.moengage.pushbase.MoEPushMessage
import com.facebook.react.modules.core.DeviceEventManagerModule
import com.facebook.react.ReactNativeHost

class MainApplication : Application(), MoEPushMessageListener {
    override fun onCreate() {
        super.onCreate()
        // 初始化MoEngage并绑定推送监听
        MoEngage.Builder(this, "YOUR_APP_ID")
            .pushMessageListener(this)
            .build()
    }

    // 处理推送点击事件(含冷启动场景)
    override fun onPushClicked(context: Context?, message: MoEPushMessage?) {
        message?.let {
            // 直接使用SDK封装的深度链接属性,避免硬编码key
            val deeplink = it.navigationDeepLink ?: it.deepLink
            deeplink?.let { link ->
                val reactContext = reactNativeHost.reactInstanceManager.currentReactContext
                if (reactContext != null) {
                    // React上下文已初始化,直接发送事件给JS端
                    reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
                        .emit("moengage_deeplink", link)
                } else {
                    // React未初始化,暂存链接到SharedPreferences,JS启动后读取
                    val prefs = getSharedPreferences("MoEngagePrefs", MODE_PRIVATE)
                    prefs.edit().putString("initial_deeplink", link).apply()
                }
            }
        }
    }

    // 可选:处理前台推送接收事件
    override fun onPushReceived(context: Context?, message: MoEPushMessage?) {}
}

JS端对应处理:

import { Linking, NativeEventEmitter, Platform } from 'react-native';
import AsyncStorage from '@react-native-async-storage/async-storage';

// 监听原生发送的深度链接事件
if (Platform.OS === 'android') {
  const eventEmitter = new NativeEventEmitter();
  eventEmitter.addListener('moengage_deeplink', (deeplink) => {
    Linking.openURL(deeplink);
  });
}

// 冷启动时读取暂存的链接
const initDeeplinkHandler = async () => {
  if (Platform.OS === 'android') {
    const storedLink = await AsyncStorage.getItem('initial_deeplink');
    if (storedLink) {
      await AsyncStorage.removeItem('initial_deeplink');
      Linking.openURL(storedLink);
    }
  }
  // 兼容常规初始链接获取
  const initialUrl = await Linking.getInitialURL();
  initialUrl && Linking.openURL(initialUrl);
};

// 在App初始化时调用
initDeeplinkHandler();

iOS 端:通过UNUserNotificationCenter代理处理冷启动推送

禁用swizzling后,手动实现通知代理方法,直接将深度链接传递给React Native的LinkingManager:

// AppDelegate.mm
#import <MoEngageSDK/MoEngageSDK.h>
#import <React/RCTLinkingManager.h>
#import <UserNotifications/UserNotifications.h>

@interface AppDelegate () <UNUserNotificationCenterDelegate>
@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  // 初始化MoEngage并禁用swizzling
  MoEngageSDKConfig* config = [[MoEngageSDKConfig alloc] initWithAppId:@"YOUR_APP_ID"];
  config.disableSwizzling = YES;
  [[MoEngage sharedInstance] initializeSDKWithConfig:config];

  // 设置通知中心代理
  UNUserNotificationCenter.currentNotificationCenter.delegate = self;

  // 其他RN初始化代码...
  return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

// 处理推送点击(含冷启动场景)
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler {
  NSDictionary* userInfo = response.notification.request.content.userInfo;
  if ([[MoEngageSDKMessaging sharedInstance] isPushFromMoEngageWithPayload:userInfo]) {
    NSString* deeplink = userInfo[@"app_extra"][@"moe_deeplink"];
    if (deeplink) {
      // 直接通过LinkingManager打开链接
      [RCTLinkingManager application:nil openURL:[NSURL URLWithString:deeplink] options:@{}];
    }
  }
  completionHandler();
}

// 前台通知展示配置
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
  completionHandler(UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionSound);
}
@end

方案优势

  1. 避免硬编码依赖:直接使用SDK封装的属性或官方约定的字段,降低后续SDK版本更新的兼容性风险。
  2. 逻辑解耦:无需修改Intent或launchOptions,推送处理与RN的Linking系统对接更规范。
  3. 场景全覆盖:同时支持冷启动、热启动、前台/后台的推送点击处理,无需区分应用状态。
  4. 可维护性高:代码符合React Native和MoEngage的官方最佳实践,后续排查问题更清晰。

内容的提问来源于stack exchange,提问作者dwnste

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 18:35:05