【问题标题】:Cant get Extras from intent when application is in background应用程序在后台时无法从意图中获取附加信息
【发布时间】:2019-05-19 13:27:39
【问题描述】:

单击通知时,我尝试在 MainActivity 中执行一个函数。 该函数需要我放入 Intent Extras 的数据。

问题是当我在应用程序运行时单击通知时执行该功能,但是当我在应用程序处于后台时单击通知时,该功能未执行。我已经检查过了,这是因为当应用程序在后台时,我放入 Intent extras 的数据是空的。

我该如何解决这个问题?谢谢!

这是我收到的回复:

{
    "to":"blablabla",
    "notification": {
        "body":"Sentiment Negative from customer",
        "title":"Mokita"
    },
    "data" : {
        "room_id":1516333
    }
}

这是我的通知代码:

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 = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel(channelId, "Default channel", NotificationManager.IMPORTANCE_DEFAULT);
        manager.createNotificationChannel(channel);
    }

    manager.notify(0, builder.build());
}

}

这是 函数 以及我如何在 MainActivity 中执行它:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_drawer);
    onNewIntent(getIntent());
}

@Override
    public void onNewIntent(Intent intent){
        Bundle extras = intent.getExtras();
        if(extras != null){
            if(extras.containsKey("fromNotification") || extras.containsKey("roomId")) {
                openChatRoom(Long.valueOf(extras.getString("roomId")));
            }else if(extras.containsKey("fromNotification") && extras.containsKey("roomId")){
                openChatRoom(Long.valueOf(extras.getString("roomId")));
            }else{
                Log.e("EXTRAS room",""+extras.getString("roomId"));
                Log.e("EXTRAS STATUS",""+extras.getBoolean("fromNotification"));
            }
        }else{
            Toast.makeText(HomePageTabActivity.this,"Empty",Toast.LENGTH_SHORT).show();
        }
    }


public void openChatRoom(long roomId){
        Log.d("LONG ROOM",""+roomId);
        QiscusRxExecutor.execute(QiscusApi.getInstance().getChatRoom(roomId),
        new QiscusRxExecutor.Listener<QiscusChatRoom>() {
            @Override
            public void onSuccess(QiscusChatRoom qiscusChatRoom) {
                startActivity(GroupRoomActivity.
                        generateIntent(HomePageTabActivity.this, qiscusChatRoom));
            }
            @Override
            public void onError(Throwable throwable) {
                throwable.printStackTrace();
            }
        });
    }

【问题讨论】:

  • 1) onNewIntent(Intent intent)里面必须有'this.setIntent(intent)。 2)在Manifest中为HomePageTabActivity添加android:launchMode="singleTask"。
  • 当我在后台点击通知时仍然无法工作
  • 我将回复改为:{ "to":"blabalasd", "data":{ "title":"Your title", "body":"Your message", "room_id" :1516333 } } 现在正在运行

标签: java android android-intent android-notifications android-pendingintent


【解决方案1】:

Firebase 有两种类型的消息:通知消息和数据消息。如果您希望 FCM SDK 自己处理消息,您需要使用notification。当应用处于非活动状态时,FCM 将使用notification body 来显示消息。在这种状态下,onMessageReceived 也不会被触发。如果您希望应用程序处理消息,您需要使用data。您可能需要从

更改推送通知有效负载
{
   "message":{
   "token":"xxxxx:...",
   "notification":{
          "title":"Your title",
          "body":"Your message"
      }
   }
}

{
   "message":{
   "token":"xxxxx:...",
   "data":{
          "title":"Your title",
          "body":"Your message",
          "fromNotification":"true",
          "roomId":"123"
      }
   }
}

您还需要相应地处理onMessageReceived(RemoteMessage remoteMessage) 中的消息。您可以在Notifications and data messages 中阅读有关通知行为的信息。

【讨论】:

  • 我已经编辑了问题并添加了我从 FCM 获得的回复,以及我的通知的完整代码。你能看看我哪里做错了吗?
  • 当应用程序处于后台时,在LauncherActivity 中未收到getExtras 有帮助吗?
【解决方案2】:

这些有效负载将被传递到您在待定意图中指定的活动。因此,当用户单击您的通知时,HomePageTabActivity 会启动,您可以通过在活动生命周期中的任何位置调用 getIntent() 来获取意图。但是因为如果HomePageTabActivity 已经启动,您在活动上设置singleTop 标志,Android 将不会再次启动它,而是将新的意图(在通知中提供)传递给onNewIntent()。您可以在那里使用它,甚至可以调用getIntent() 从那里获取新值。

【讨论】:

  • 我已经编辑了问题并添加了我从 FCM 获得的回复,以及我的通知的完整代码。你能看看我哪里做错了吗?
【解决方案3】:

Receive messages in an Android app。具有通知和数据有效负载的消息,包括后台和前台。在这种情况下,数据有效负载将传递给您的启动器活动意图的附加部分。如果您想在其他活动中获取它,则必须在数据有效负载上定义 click_action。因此,在您的启动器活动中获得额外的意图。

【讨论】:

  • 我已经编辑了问题并添加了我从 FCM 获得的回复,以及我的通知的完整代码。你能看看我哪里做错了吗?
  • 是 HomePageTabActivity.class 您的启动器活动吗?您只能从启动器活动中获得额外内容。因此,如果您想从启动器活动之外的后台获取消息,则将附加信息发送到您的启动器活动中。从启动器获得附加功能后,您可以从启动器活动中向其发送您想要的活动。注意:您只能从启动器活动中获得额外信息,因为数据保留在系统 blob 中,其他活动无法访问。可以阅读示例github.com/firebase/quickstart-android
  • 启动器活动是指打开应用程序时首先运行的活动吗?如果我有一个 SplashScreen 作为我的启动器活动,我应该将 SplashScreen.java 放在待处理的意图中并将附加内容放在那里吗?
  • 是的,您必须在启动器活动 (SplashScreen) 中添加额外内容。
  • 它现在可以工作了,我改变了对这个 { "to":"blalalal", "data":{ "title":"Your title", "body":"Your message", "room_id":1516333 } } 感谢您的宝贵时间 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多