Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cant get Extras from intent when application is in background

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();
            }
        });
    }
like image 945
Rifki Maulana Avatar asked Dec 19 '18 04:12

Rifki Maulana


3 Answers

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.

like image 200
Amad Yus Avatar answered Oct 18 '22 05:10

Amad Yus


these payloads will be delivered to the activity you are specifying in the pending intent. so when the user clicks on your notification, HomePageTabActivity launches and you can get the intent by calling getIntent() anywhere in activity lifecycle. but because you are setting singleTop flag on activity if HomePageTabActivity is already launched, Android will not launch it again and will pass the new Intent (provided in notification) to onNewIntent() instead. you can consume it there or even call the getIntent() to get the new value from there on.

like image 36
seyed Jafari Avatar answered Oct 18 '22 07:10

seyed Jafari


Receive messages in an Android app. Messages with both notification and data payload, both background and foreground. In this case the data payload is delivered to extras of the intent of your launcher activity. If you want to get it on some other activity, you have to define click_action on the data payload. So get the intent extra in your launcher activity.

like image 33
aslamconsole Avatar answered Oct 18 '22 07:10

aslamconsole