android firebase 通知没有导航到准确的页面

android firebase notification not navigate to the exact page

我已经完成了 firebase 通知。我可以在通知栏中获得多个通知。当我按下通知栏中的一个通知图标时,它会重定向到上次收到的屏幕。然后,如果我按下通知栏中的待定图标,则什么也不会发生。这是我的代码

 private void sendNotification(Map<String, String> data) {
    Intent intent = null;
    String messageBody = data.get("Message");
    String referenceKey = data.get("ReferenceKey");
    String referenceValueFromFCM = data.get("ReferenceValue");

    if (LocalRepository.getInstance().isAuthenticated()) {
     PromotionData promotionData = new PromotionData();

            if (!TextUtils.isEmpty(referenceValue) && TextUtils.isDigitsOnly(referenceValue)) {
                promotionData.promotionId = Integer.parseInt(referenceValue);

            }
    intent.putExtra("key", referenceKey);
     intent.putExtra("data", promotionData);
     intent.putExtra("value", referenceValue);
intent = new Intent(this, DashboardActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    long[] pattern = {250, 250, 250, 250, 250};
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_notification)
            .setContentTitle(getString(R.string.app_name))
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setVibrate(pattern)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(AppUtil.getNotificationId() /* ID of notification */, notificationBuilder.build());

这是我的清单

 <activity
        android:name=".views.dashboard.DashboardActivity"
        android:screenOrientation="portrait"
        android:exported="true"
        android:theme="@style/AppTheme.NoActionBar"
        android:windowSoftInputMode="stateAlwaysHidden" />

这是仪表板导航activity

 String key = getIntent().getStringExtra("key");
            String promotionID = getIntent().getStringExtra("value");
            PromotionData promotionData = getIntent().getParcelableExtra("data");

            if (!TextUtils.isEmpty(promotionID) && (!TextUtils.isEmpty(key))) {
                Intent intent = null;
                switch (key) {
                    case Repository.ModuleCode.WHATS_NEWS:
                         if (hasPromotionId) {
                        whatsNew();
                        intent = new Intent(this, WhatsNewDetailActivity.class);
                        intent.putExtra("data", promotionData);
                        intent.putExtra("pushID", pushID);
                        startActivity(intent);
                         }else{
                          whatsNew(); // this is fragment
                         }
                        break;

                    case Repository.ModuleCode.PROMOTION:
                         if (hasPromotionId) {
                        promotion();
                        intent = new Intent(this, PromotionDetailActivity.class);
                        intent.putExtra("data", promotionData);
                        intent.putExtra("pushID", pushID);
                        startActivity(intent);
                         }else{
                           promotion(); // this is fragment
                         }
                        break;

你能告诉我吗 提前致谢。

您可以使用此答案了解如何使用 NotificationActivity 上传递值。 Here's a link which might be helpful

只是你必须像这样用 Intent 传递值,

Intent intent = new Intent(this, yourActivityClass.class);
intent.putExtra("msg",messageBody);

我已经找到问题的答案了。 当我用 Intent 传递值时,应该使用

intent.setData

然后它正在处理多个通知。

这是我用于导航确切屏幕的更新代码。

Intent intent = null;
    String messageBody = data.get("Message");
    String referenceKey = data.get("ReferenceKey");
    String referenceValue = data.get("ReferenceValue");
    String pushID = data.get("PushId");


    PromotionData promotionData = new PromotionData();//item id
    if (!TextUtils.isEmpty(referenceKey)) {
        if (!TextUtils.isEmpty(referenceValue) && TextUtils.isDigitsOnly(referenceValue)) {
            promotionData.promotionId = Integer.parseInt(referenceValue);

        }
    }

    if (LocalRepository.getInstance().isAuthenticated()) {
        intent = new Intent(this, DashboardActivity.class);
        intent.setData(new Uri.Builder().scheme(referenceKey).build());
        intent.putExtra("pushID", pushID);
        intent.putExtra("data", promotionData);
        intent.putExtra("key", referenceKey);
        intent.putExtra("value", referenceValue);
    } else {
        intent = new Intent(this, LoginActivity.class);
        intent.setData(new Uri.Builder().scheme(referenceKey).build());
        intent.putExtra("pushID", pushID);
        intent.putExtra("data", promotionData);
        intent.putExtra("key", referenceKey);
        intent.putExtra("value", referenceValue);
    }

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    long[] pattern = {250, 250, 250, 250, 250};
    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_notification)
            .setContentTitle(getString(R.string.app_name))
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setVibrate(pattern)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(AppUtil.getNotificationId() /* ID of notification */, notificationBuilder.build());
}

此处应使用此行进行准确的页面导航。这是火力地堡错误。 intent.setData(new Uri.Builder().scheme(referenceKey).build()); 我们可以传递任何字符串而不是 referenceKey。