0

I'm building a chat app that pushes notification like facebook, Instagram. when a user sends message notification will show automatically. The problem is I don't know how to handle background service.

For example, when the same user sends the message, a notification will append the message like this:

Image Notificaiton

But when FCM send notification this is what happened:

My notification

I need to be able to handle background service

Here is my code

public class FirebaseInstaceService extends FirebaseMessagingService {


FirebaseToken firebaseToken = new FirebaseToken();
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

@Override
public void onNewToken(String s) {
    super.onNewToken(s);
    Log.d("NewToken","New token: " +s);

    if (user != null){
        firebaseToken.sendToken(s, new OncompleteCallback() {
            @Override
            public void callback() {
                Log.d("NewToken","Send complete");
            }
        });
    }
}

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    try {
        Log.d("CloudMessage",remoteMessage.getNotification().getBody());
    }catch (Exception e){
        Log.d("CloudMessage",e.toString());
        throw e;
    }
  }
}
Gaurav Mall
  • 2,372
  • 1
  • 17
  • 33
Supagon Srisawas
  • 113
  • 1
  • 1
  • 9
  • not clear what do you mean by ```handle background service``` . can you explain – Pwn Aug 11 '19 at 15:32
  • As you can see in the picture above FCM creates a push notification automatically using background service, so I can't do anything with that. For example, when the same user send message I need to append a message to the same notification but the background service create a new one automatically Thank you – Supagon Srisawas Aug 11 '19 at 16:56
  • its default behaviour of FCM its depend of data your are sending in push payload . onMessageReceived will always called if you have data key in payload. https://firebase.google.com/docs/cloud-messaging/concept-options#data_messages – Pwn Aug 12 '19 at 06:29

1 Answers1

1

If I understand your question correctly, you need to do work on the non UI thread when a push notification is received. In that case, you're in luck - as explained here, onReceive() is called on a non-UI thread.

You just create and show Notifications as you see fit, storing the notification ID uniquely to allow user->notification mapping. That way, you can access the notification shown when the same user sends another message.

Micha
  • 396
  • 2
  • 8
  • Thank you I know that but the problem is when the application is closed, function inside OnReceive won't work that way I need a solution for it – Supagon Srisawas Aug 12 '19 at 04:51