简体   繁体   中英

How to get data from firebase-message while Android app is in background

I'm sending firebase-messages using the firebase-console. The messages shall contain additional data like shown below with the purpose to open a specific URL within a webview in my app:

在此处输入图像描述

I set up my manifest and firebase class to get the messages. Within my firebase class I try to get the data:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    if(remoteMessage.getData().containsKey("key1")) {
        intent.putExtra("destination", remoteMessage.getData().get("key1"));
    }
    PendingIntent pendingIntent = PendingIntent.getActivity
    (this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    String channelId = "default";
    NotificationCompat.Builder builder;
    if (remoteMessage.getNotification() != null ) {
        if (remoteMessage.getNotification().getTitle() != null) {
            builder = new NotificationCompat.Builder(this, channelId)
                    .setSmallIcon(R.drawable.ic_stat_onesignal_default)
                    .setContentTitle(remoteMessage.getNotification().getTitle())
                    .setStyle(new NotificationCompat.BigTextStyle().bigText(remoteMessage.getNotification().getBody()))
                    .setAutoCancel(true)
                    .setContentIntent(pendingIntent);
        } else {
            builder = new NotificationCompat.Builder(this, channelId)
                    .setSmallIcon(R.drawable.ic_stat_onesignal_default)
                    .setStyle(new NotificationCompat.BigTextStyle().bigText(remoteMessage.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", NotificationManager.IMPORTANCE_DEFAULT);
            manager.createNotificationChannel(channel);
        }

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

Within my MainActivity class I try to get the data. When the app is in the foreground, the following works (no matter what activity is opened, it will jump to the MainActivity and execute the following):

@Override
protected void onNewIntent(Intent intent) {
    if (intent.getExtras() != null) {

        Bundle extras = intent.getExtras();

        if(extras.containsKey("destination")) {
            Log.e("FIREBASE_CONTAINS", (String) extras.get("destination"));
        }
    }
}

But the event wont trigger if the app started from the background. I tried to get the intent and check for the key within the onResume() event of the activity, but it does not work (Same in inCreate() and onStart()).

Can anybody help me?

------------EDIT-----------------

As described in one of the comments, the problem seems to be that Notification-Messages won't reach the onMessageReceived() event. Apparently the firebase console can't send data-notifications (which would reach the event) so I tried using POSTMAN. I've read that I have to leave the notification tag out of the message body and put all my information in the data section. But if I do so, the messages won't reach my app (they do, when I add the notification section again, but of course they are not reaching the onMessageReceived() event in that case).

There are 3 types of push messages

  • notification
  • data
  • and both

A push messages is basically a json payload:

payload:{
    notificacion...
    data
}

Rules for each type of push messages are differente. In your case you are using the Firebase web console and adding custom data, which mean your payload will have notification and data.

For the combined type the behaviour in backgroun is to use a default notificacion (NotificationCompat, the visual kind) and open the default activity registered in the manifest. In the activity you can get the data.

Lets say your default activity is called MainActivity

public class MainActivity {

    onCreate...{
    //... usual stuff
    Intent fcmIntent = getIntent();
    if fcmIntent != null
    //check the extras and forward them to the next activity if needed
    }
}

There are two type of push message

(1)Notification Message (will receive when app is in foreground)

(2)Data Message (will receive when app is in background+foreground)

在此处输入图片说明

Reference : https://firebase.google.com/docs/cloud-messaging/android/receive

You need to set click_action in firebase notification data set to be able to receive data from background and implement onMessageReceived to handle foreground data

See updated answer here: https://stackoverflow.com/a/73724040/7904082

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM