How to get Firebase notifications received in the background?

Asked

Viewed 571 times

1

I implemented the notification service in my app, working, even receives notification in the background, however I wanted to save the notification message, searched a little and created a service ( code below ) works, but only when the app is open ( in the foreground ), background does not work.

public class FirebaseNotificationService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.i("PVL", "Mensagem Recebida");
        if (remoteMessage.getNotification().getBody() != null) {
            Log.i("PVL", "Mensagem recebida: " + remoteMessage.getNotification().getBody());
        } else {
            Log.i("PVL", "Mensagem recebida: " + remoteMessage.getData().get("message"));
        }

    }

}
  • Do you want to save the notification message where? On mobile?

  • You can pass data beyond the notification through the FCM, so in principle, you don’t have to capture the notification itself, just pass the message as a notification data and read the received data in the app. Have a look: https://firebase.google.com/docs/cloud-messaging/concept-options?hl=pt-br

  • @acklay at first I want to save in a list, to later display in a listview, make a history of received notifications

1 answer

1


The fact that you are not receiving the message when the application is in the background is why the object of push is being sent as Notification. The correct for what you receive in the background is named Data.

For example:

{
"data": {
    "title": "Push",
    "text": "Nova mensagem",
},
"priority": "Normal",
"to": "/topics/general"
}

So, in the service class that mounts the notification, you will do the following to capture the message text:

 if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData());
        String mensagem = remoteMessage.getData().get("text");    
    }
  • I am sending by firebase console, how to send as date?

  • I did, using ARC

  • But a doubt .. I can implement this post request that ARC does on a site for example right ?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.