首页 > 解决方案 > 从通知的后台打开后,Android PendingIntent 未启动活动

问题描述

我正在使用 Google FCM 向我的应用发送通知。我目前正在发送数据通知并让我的 android 应用程序构建推送通知。我希望用户能够单击通知并将其路由到适当的片段/活动。

除了一个流程外,它工作得很好。用户终止应用程序->接收通知->点击通知->应用程序启动和数据从通知正确传递到启动活动(我正在使用通知活动来广播/启动意图)->用户在应用程序打开时收到另一个通知-> 用户单击通知 -> 之前正确启动的通知活动不会启动。

我正在挠头,试图弄清楚为什么会这样。我的猜测与上下文错误有关,因为应用程序是从通知中启动的,但我尝试同时使用“this”和“getApplicationContext()”,但都没有解决问题。

以下是我构建通知和待处理意图的方式。

Intent notificationIntent = new Intent(this, NotificationActivity.class);
notificationIntent.putExtra(Variables.INTENT_NOTIFICATION_DATA, "dummy_data");
notificationIntent.setAction(Variables.NOTIFICATION_CLICK);
PendingIntent contentIntent = PendingIntent.getActivity(this,
        0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(this, Variables.CHANNEL)
        .setContentTitle("New Notification")
        .setContentText("Dummy text")
        .setSmallIcon(R.drawable.notification_icon)
        .setContentIntent(contentIntent)
        .setAutoCancel(true)
        .setOngoing(false)
        .build();
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (mNotificationManager != null) {
    mNotificationManager.notify(0, notification);
}

标签: javaandroidfirebasefirebase-cloud-messaging

解决方案


推荐阅读