Android 在React Native中收到通知而没有用户交互时如何打开应用程序

Android 在React Native中收到通知而没有用户交互时如何打开应用程序,android,firebase,react-native,firebase-cloud-messaging,Android,Firebase,React Native,Firebase Cloud Messaging,如何在收到通知而无需用户交互时打开应用程序 我正在使用react本机推送通知库进行推送通知。应用程序应该在没有用户交互的情况下从后台自动启动并退出状态,并且应该调用getInitialNotification方法。我想在特定通知类型上调用应用程序 =============================================== File: RNPushNotificationHelper =============================================

如何在收到通知而无需用户交互时打开应用程序

我正在使用react本机推送通知库进行推送通知。应用程序应该在没有用户交互的情况下从后台自动启动并退出状态,并且应该调用getInitialNotification方法。我想在特定通知类型上调用应用程序

===============================================

File: RNPushNotificationHelper

===============================================



public void invokeApp(Bundle bundle) {

    String packageName = context.getPackageName();
    Intent launchIntent = context.getPackageManager().getLaunchIntentForPackage(packageName);
    String className = launchIntent.getComponent().getClassName();

    try {
        Class<?> activityClass = Class.forName(className);
        Intent activityIntent = new Intent(context, activityClass);

        if(bundle != null) {
            activityIntent.putExtra("notification", bundle);
        }

        activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        context.startActivity(activityIntent);
    } catch(Exception e) {
        Log.e(LOG_TAG, "Class not found", e);
        return;
    }
}

===============================================

File: RNReceivedMessageHandler

===============================================


 // If notification ID is not provided by the user for push notification, generate one at random
 if (bundle.getString("id") == null) {
    SecureRandom randomNumberGenerator = new SecureRandom();
    bundle.putString("id", String.valueOf(randomNumberGenerator.nextInt()));
}

Application applicationContext = (Application) context.getApplicationContext();

RNPushNotificationConfig config = new RNPushNotificationConfig(mFirebaseMessagingService.getApplication());  
RNPushNotificationHelper pushNotificationHelper = new RNPushNotificationHelper(applicationContext);

boolean isForeground = pushNotificationHelper.isApplicationInForeground();

RNPushNotificationJsDelivery jsDelivery = new RNPushNotificationJsDelivery(context);
bundle.putBoolean("foreground", isForeground);
bundle.putBoolean("userInteraction", false);
jsDelivery.notifyNotification(bundle);

// If contentAvailable is set to true, then send out a remote fetch event
if (bundle.getString("contentAvailable", "false").equalsIgnoreCase("true")) {
    jsDelivery.notifyRemoteFetch(bundle);
}

Log.v(LOG_TAG, "invokeApp: " + bundle);
pushNotificationHelper.invokeApp(bundle);

if (config.getNotificationForeground() || !isForeground) {
    Log.v(LOG_TAG, "sendNotification: " + bundle);
    pushNotificationHelper.sendToNotificationCentre(bundle);
}
===============================================
文件:RNPushNotificationHelper
===============================================
公共无效调用EAPP(捆绑包){
字符串packageName=context.getPackageName();
Intent launchIntent=context.getPackageManager().getLaunchIntentForPackage(packageName);
字符串className=launchIntent.getComponent().getClassName();
试一试{
类activityClass=Class.forName(类名称);
Intent activityIntent=新的意图(上下文,activityClass);
if(bundle!=null){
activityIntent.putExtra(“通知”,bundle);
}
activityIntent.addFlags(Intent.FLAG\u ACTIVITY\u NEW\u TASK);
上下文。开始触觉(活动意图);
}捕获(例外e){
Log.e(日志标签“未找到类”,e);
返回;
}
}
===============================================
文件:RNReceivedMessageHandler
===============================================
//如果用户没有为推送通知提供通知ID,则随机生成一个
if(bundle.getString(“id”)==null){
SecureRandom randomNumberGenerator=新SecureRandom();
bundle.putString(“id”,String.valueOf(randomNumberGenerator.nextInt());
}
应用程序上下文=(应用程序)上下文。getApplicationContext();
RNPushNotificationConfig=new RNPushNotificationConfig(mFirebaseMessagingService.getApplication());
RNPushNotificationHelper pushNotificationHelper=新的RNPushNotificationHelper(applicationContext);
布尔值isForeground=pushNotificationHelper.isApplicationInForeground();
RNPushNotificationJsDelivery jsDelivery=新的RNPushNotificationJsDelivery(上下文);
bundle.putBoolean(“前景”,即前景);
bundle.putBoolean(“用户交互”,false);
jsDelivery.notifyNotification(bundle);
//如果contentAvailable设置为true,则发送远程获取事件
if(bundle.getString(“contentAvailable”,“false”).equalsIgnoreCase(“true”)){
jsDelivery.notifyRemoteFetch(bundle);
}
Log.v(Log_标签,“invokeApp:+bundle”);
pushNotificationHelper.invokeApp(bundle);
if(config.getNotificationForeground()| |!isForeground){
Log.v(Log_标签,“sendNotification:+bundle”);
pushNotificationHelper.SendToNotificationCenter(捆绑包);
}
但它在后台或退出状态下似乎不起作用。我检查了logcat,它在前台显示日志。

通知操作 我认为这是不可能的,因为这将是一个重大的安全漏洞

在Android文档中,只有点击通知和通知操作被列为后台操作的选项。

绕过限制 Android 10(API级别29)有一个所谓的“从后台启动活动的限制”

然而,这些问题已经解决。 我认为你最感兴趣的是:

  • 应用程序从系统接收到一个挂起通知。在 服务和广播接收器的未决意向,应用程序 在完成挂起的意图后,可以启动活动几秒钟 发送

  • 应用程序已被授予系统警报窗口权限 用户