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

喜夏-厌秋 提交于 2020-02-07 07:23:09

问题


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 descriped in one of the comments, the poroblem seems to be that Notification-Messages won't reach the onMessageReceived() event. Apparently the firebase console can't send data-notifications (wich 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 rech my app (they do, when I add the notification section again, but of course they are not reaching the onMessageReceived() event in that case).


回答1:


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




回答2:


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
    }
}


来源:https://stackoverflow.com/questions/57076964/how-to-get-data-from-firebase-message-while-android-app-is-in-background

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!