【问题标题】:FCM Notification onclick does not open desired activityFCM 通知 onclick 未打开所需的活动
【发布时间】:2023-03-17 16:52:01
【问题描述】:

我几乎尝试了此处发布的所有解决方案以及每个标志的组合,但它不起作用。

以下是我遇到问题的用例。

1) 当我在应用程序中时,FCM 通知会打开我想要的活动。 数据在主要活动中传递给onNewIntent。当应用程序在前台时,它可以正常工作。

2) 在后台模式下(按主页按钮)单击通知后,它会启动我的应用程序的新实例,即使我已在清单文件中为我的主要活动指定 android:launchMode="singleTop" 但我不知道这里发生了什么.我想在此处打开的所需活动是 RateEventActivity

这里发生的奇怪事情是 NotificationActivity 和 ProfileActivity 即使在应用程序处于后台模式时也能正常工作,但 RateEventActivity 正在产生我上面描述的所有问题。大多数应用程序代码是由我正在开发RateEevent 模块的其他人编写的,所以我不知道我在这里缺少什么?

当我在 RateEventActivity 场景中从后台模式单击通知时,意图在某处丢失(未在创建时传递给 mainActivty)。

我也尝试将唯一 ID 传递给我的待处理意图,但没有运气。我花了很多时间和精力来解决这个问题,但每次都失败了

这是我的 FCM 代码

  private void createNotification(RemoteMessage remoteMessage) {
    RemoteMessage.Notification notification = remoteMessage.getNotification();
    NotificationManager mNotificationManager = (NotificationManager)
            this.getSystemService(Context.NOTIFICATION_SERVICE);

    Log.v("Notification Title", remoteMessage.getNotification().getTitle());

    Intent intent = new Intent(this, MainActivity.class);
    //intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    if (remoteMessage.getData().containsKey("notificationUser")) {
        intent.putExtra("notificationUser", remoteMessage.getData().get("notificationUser"));
    } else if (remoteMessage.getData().containsKey("notificationId")) {
        intent.putExtra("notificationId", remoteMessage.getData().get("notificationId"));
    } else if (remoteMessage.getData().containsKey("event")) {
        if (remoteMessage.getNotification().getTitle().equalsIgnoreCase("Event Feedback")) {


            Log.v("Notification Event", remoteMessage.getData().get("event"));
            intent.putExtra("eventId", remoteMessage.getData().get("event"));
        }


    }


    //PendingIntent.FLAG_UPDATE_CURRENT
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0 , intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_notification)
                    .setLargeIcon(largeIcon)
                    .setContentTitle(notification.getTitle())
                    .setStyle(new NotificationCompat.BigTextStyle()
                            .bigText(notification.getBody()))
                    .setContentText(notification.getBody())
                    .setAutoCancel(true);

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}

主要活动代码onCreate方法

   if (getIntent().hasExtra("notificationId")) {
        Intent intent = new Intent(this, NotificationActivity.class);
        intent.putExtra("notificationId", getIntent().getStringExtra("notificationId"));
        startActivity(intent);
    } else if (getIntent().hasExtra("user")) {
        Intent intent = new Intent(this, ProfileActivity.class);
        intent.putExtra("userId", getIntent().getStringExtra("user"));
        startActivity(intent);
    } else if (getIntent().hasExtra("eventId")) {
        Intent intent = new Intent(this, RateEventActivity.class);
        intent.putExtra("eventId", getIntent().getStringExtra("eventId"));
        startActivity(intent);
    }

我的清单文件

 <activity
        android:name=".activities.MainActivity"
        android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:launchMode="singleTop"
        android:screenOrientation="portrait"
        android:theme="@style/HomeTheme" />

<activity
        android:name=".activities.ProfileActivity"
        android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:label="Profile"
        android:screenOrientation="portrait" />

 <activity
        android:name=".activities.NotificationActivity"
        android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:label="Notifications"
        android:screenOrientation="portrait" />

<activity
        android:name=".activities.RateEventActivity"
        android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:label="Rate Users"
        android:screenOrientation="portrait"
       />

【问题讨论】:

  • 您是否收到此日志`Log.v("Notification Event", remoteMessage.getData().get("event"));`??
  • 是的,我正在从推送通知中获取数据..

标签: android android-intent firebase-cloud-messaging


【解决方案1】:

如果您想打开您的应用并执行特定操作 [在后台运行时],请在通知负载中设置 click_action 并将其映射到您要启动的 Activity 中的意图过滤器。例如,将 click_action 设置为 OPEN_ACTIVITY_1 以触发如下意图过滤器:

按照 react-native-fcm 文档中的建议,要求后端以这样的形式发送 json 数据,

 {

    "to":"some_device_token",

    "content_available": true, 
    "notification": {
    "title": "hello",
    "body": "yo",
    "click_action": "OPEN_ACTIVITY_1" // for intent filter in your activity
    },

"data": {
"extra":"juice"
}
}

并在您的 mainfest 文件中为您的活动添加意图过滤器,如下所示

<intent-filter>
  <action android:name="OPEN_ACTIVITY_1" />
  <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

当您单击通知时,它将打开应用程序并直接转到您在 click_action 中定义的活动,在本例中为“OPEN_ACTIVTY_1”。在该活动中,您可以通过以下方式获取数据:

Bundle b = getIntent().getExtras();// add these lines of code to get data from notification
String someData = b.getString("someData");

查看以下链接以获得更多帮助:

Firebase FCM notifications click_action payload

Firebase onMessageReceived not called when app in background

Firebase console: How to specify click_action for notifications

【讨论】:

  • +1 用于解释使用动作和类别设置意图过滤器以工作 click_action。这对我帮助很大。
【解决方案2】:

我很久以前就遇到过这个问题。让我检查一下我的项目代码。

啊,它在这里。

尝试设置标志

Intent intent = new Intent(this,    MainActivity.class));       
// set intent so it does not start a new activity
         intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
         Intent.FLAG_ACTIVITY_SINGLE_TOP);

并更改您的PendingIntent,如下所示

PendingIntent contentIntent = PendingIntent.getActivity(this, ,
                intent, 0);

同时删除

android:launchMode="singleTop"

来自 Manifest 文件中的 MainActivity

试试这个,看看效果如何。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-30
    相关资源
    最近更新 更多