Aman
Aman

Reputation: 458

How to get FCM onMessageReceived event in activity without PendingIntent

I want to implement pub/sub in my app, for that server send notification in specific event, I will do some modification on data which is display in my activity

Upvotes: 0

Views: 2918

Answers (1)

teck wei
teck wei

Reputation: 1385

onMessageReceived() doesn't need PendingIntent to be call. It will always call if you have correct setup. This link provide the data type you should sent to FCM server

With FCM, you can send two types of messages to clients:

  1. Notification messages, sometimes thought of as "display messages."

  2. Data messages, which are handled by the client app.

If you would like to always trigger use just data message so it will always trigger the onMessageReceived().If you try to use data-message and notification-message together the onMessageReceived() will not get trigger when your app is in background.

Just do anything you would like to do such as save to database, sharedPeference etc inside your onMessageReceived()

So how you sent to the activity?

Use Broadcast Receiver here is how you sent a broadcast receiver in your case you will like to put it inside your onMessageReceived() so anytime you received a new notification this code will help you sent the data to the specific activity.

Intent intent = new Intent("Use anything you like");
intent.putExtra("data","The data you receive");
sendBroadcast(intent);
             

In your activity register it in your onStart()

registerReceiver(broadCastReceiver,new IntentFilter("must match the intent filter parameter"));

Here is how you handle your data

class broadCastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {

        Log.d("Your data",intent.getData());
    }
}

Note: your intent filter parameter must match the intent parameter you set in your onMessageReceived()

If your app never received data from the FCM this answer will be useless since your question still unclear this is the best I can do for you.

Upvotes: 3

Related Questions