小编Rif*_*ana的帖子

当应用程序在后台时无法从意图中获取额外内容

单击通知时,我尝试在 MainActivity 中执行一个函数。该函数需要一个我放入意图附加项的数据。

问题是当我在应用程序运行时单击通知时执行函数,但是当我在应用程序在后台时单击通知时,函数没有执行。我已经检查过了,这是因为当应用程序在后台时,我放入 Intent Extras 的数据是空的。

我怎么解决这个问题?谢谢!

这是我收到的回复:

{
    "to":"blablabla",
    "notification": {
        "body":"Sentiment Negative from customer",
        "title":"Mokita"
    },
    "data" : {
        "room_id":1516333
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的通知代码:

public void onMessageReceived(RemoteMessage message) {
    super.onMessageReceived(message);
    Log.d("msg", "onMessageReceived: " + message.getData().get("room_id"));
    String roomId = message.getData().get("room_id");

    Intent intent = new Intent(this, HomePageTabActivity.class);
    intent.putExtra("fromNotification", true);
    intent.putExtra("roomId", roomId);
    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    String channelId = "Default";
    NotificationCompat.Builder builder = new  NotificationCompat.Builder(this, channelId)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle(message.getNotification().getTitle())
            .setContentText(message.getNotification().getBody())
            .setAutoCancel(true)
            .setContentIntent(pendingIntent);
    NotificationManager manager …
Run Code Online (Sandbox Code Playgroud)

java android android-intent android-notifications android-pendingintent

5
推荐指数
1
解决办法
2976
查看次数