0

I'm using firebase message service to get notification to my android app but the service not called but once! how to handle it to make it called whenever I got message? here is my code:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);

    String user_id = "0";

    if (remoteMessage.getData().size() > 0) {
        Log.d(TAG, "Message data payload: " + remoteMessage.getData());
        user_id = remoteMessage.getData().get("from_user");

    }

    String click_action = remoteMessage.getNotification().getClickAction();
    Log.d(TAG, "Message data payload: " + click_action);

    //Calling method to generate notification
    sendNotification(remoteMessage.getNotification().getBody(), remoteMessage.getNotification().getTitle(), user_id, click_action);
}

private void sendNotification(String messageBody, String messageTitle, String user_id, String click_action) {
    Intent intent = new Intent(click_action);
    intent.putExtra("user_id", user_id);

    PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent,
            PendingIntent.FLAG_ONE_SHOT);


    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
    notificationBuilder.setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle(messageTitle)
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager = (NotificationManager) 
getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, notificationBuilder.build());
}

and there is my payload:

  const payload = {
    notification: {
        title: "New Friend Request",
        body: `${userName} has sent you a Friend Request`,
        icon: "default",
        click_action: "com.example.alaa.lapitchatapp.Target_Notification"

    },
    data:{
        from_user: from_user_id
    }
};

This is the service in the Mainfest:

<service android:name=".FirebaseMessagingService">
    <intent-filter>
        <action 
 android:name=
 "com.google.firebase.MESSAGING_EVENT" 
      />
    </intent-filter>
</service>
Alaa Emad
  • 1
  • 5
  • show me your code – Ajay Chauhan Nov 07 '18 at 17:14
  • it will be better if you edit your question by including the code. If you are sending it from firebase console, try refreshing the console. – Bibin Johny Nov 07 '18 at 17:14
  • show me the code in which you are using your firebase token – Ajay Chauhan Nov 07 '18 at 17:15
  • @AjayChauhan I've included my code, Please check it for me. Thanks – Alaa Emad Nov 07 '18 at 17:32
  • where is your firebase messaging service code ,did you included that in your manifest file – Ajay Chauhan Nov 07 '18 at 17:51
  • @AjayChauhan yes, I've. I've included the service in the Mainfest. – Alaa Emad Nov 07 '18 at 17:56
  • make sure the token id is correct(by correct i mean updated, it changes if you uninstall and reinstall ) – shashank chandak Nov 07 '18 at 18:29
  • how are trying to send firebase message to your app, and which version of the firebase dependency are you using – Ajay Chauhan Nov 07 '18 at 18:33
  • @shashankchandak yes i checked it. The problem is the service is called only one time. If you checked my code especially the one in the onMessegeReceived() method, i have some Logs to check the data. But unfortunately the log message appeared only one time and i cannot get it after that. – Alaa Emad Nov 07 '18 at 18:36
  • @AjayChauhan I'm using the latest version – Alaa Emad Nov 07 '18 at 19:21
  • if you are receiving the message once then you should get other times too, we get the token as soon as firebase service started, if that token gets null,in that case you won't be receiving firebase messages – Ajay Chauhan Nov 08 '18 at 05:56
  • @AjayChauhan the problem with the service itself not with the payload or the token id. I'm receiving the notification from the server but still can't handle it with the service in my app, the method is never invoked. – Alaa Emad Nov 08 '18 at 11:30

2 Answers2

0

The way you are sending notification your onMessageReceived() will not be called when your app is in background or killed to solve this issue remove notification field while sending notification and send only data like this

{ data:{ from_user: from_user_id // you can add more field if you want } };

include "priority": "high" filed in your notification request so that you will get the notification asap for more detail check this answer

Virendra Varma
  • 895
  • 1
  • 12
  • 23
  • By removing notification from the payload, now I'm not getting any notification. what should I do? I tried to build a notification by using notification builder with the data I'm receiving from data, but it's not working as well! that means the method is still never called in the first place! is that a possible thing to be happened??? @VirendraVarma – Alaa Emad Nov 08 '18 at 11:26
0

After way too much digging, I've realised that

The notification is delivered to the device's system tray, and the data payload is delivered in the extras of the intent of my launcher Activity.

So I was supposed to handle the extras from the activity in which I was sending the notification to. The answer was in here

Therefore, there was no need to write any code inside the onMessageReceived().

Thanks a bunch for you All. Really appreciate it

Community
  • 1
  • 1
Alaa Emad
  • 1
  • 5