【发布时间】:2021-03-26 23:58:55
【问题描述】:
在没有用户交互的情况下收到通知时如何打开应用程序
我正在使用 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);
}
但它似乎在后台或退出状态下不起作用。我检查了 logcat,它在前台显示日志。
【问题讨论】:
标签: android firebase react-native firebase-cloud-messaging