Cant get Extras from intent when application is in background

Rifki Maulana :

I try to execute a function in my MainActivity when a notification is clicked. The function needs a data that I put in intent extras.

The problem is when I click the notification when the application is running the function is executed, but when I click the notification when the application is in the background, the function isn't executed. I've checked it and it's because the data that I put in intent extras is empty when the application is in the background.

How can I solve this problem? Thanks!

This is the response i receive :

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

This is my notification code :

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());
}

}

And this is the function and how i executed it in 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();
            }
        });
    }
Amad Yus :

Firebase has two types of messages: notification messages and data messages. If you want FCM SDK to handle the messages by its own, you need to use notification. When the app is inactive, FCM will use notification body to display the messages. In this state, onMessageReceived also will not be triggered. If you want app to process the messages, you need to use data. You might need to change push notification payload from

{
   "message":{
   "token":"xxxxx:...",
   "notification":{
          "title":"Your title",
          "body":"Your message"
      }
   }
}

to

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

You also need to process the messages in onMessageReceived(RemoteMessage remoteMessage) accordingly. You can read about notification behaviour in Notifications and data messages.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=74725&siteId=1
Recommended