2

What I want: I want to send notification from Firebase Console. When app is in background/kill and when user taps on Push Notification it can go to particular activity. Let's call it Current Offers Activity. Same thing should happen while app will be in foreground.

What I understood: From firebase docs, when app is in foreground, push notifications sent from Firebase Console will be received in onMessageReceived method of MyFirebaseMessagingService class. But when app is in background/kill, the onMessageReceived method of MyFirebaseMessagingService class won't get called.

What I want: When app is in background/kill & user taps on push notification, I want to redirect user to particular activity (Current Offers Activity).

I tried this answer but I did not get the solution.

My code is like below.

MyFirebaseMessagingService.java

    public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private final String TAG = "mk";

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

        Log.e(TAG, "Message Body: " + remoteMessage.getNotification().getBody());
    }
}

MyFirebaseInstanceIDService.java

    public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {

    private final String TAG = "mk";

    @Override
    public void onTokenRefresh() {

        String refreshedToken = FirebaseInstanceId.getInstance().getToken();

        Log.e(TAG, "MyFirebaseInstanceIDService Token: " + refreshedToken);
    }
}

ActivityOne.java

    public class ActivityOne extends AppCompatActivity {

    private final String TAG = "mk";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_one);

        String refreshedToken = FirebaseInstanceId.getInstance().getToken();

        Intent intent = getIntent();

        if (intent != null && intent.getExtras() != null) {

            Bundle extras = intent.getExtras();

            for (String key : extras.keySet()) {
                Log.e(TAG, key + " : " + extras.get(key));
            }
        }

        Log.e(TAG, "Token: " + refreshedToken);

        //sendNotificationToPatner();
    }

    private void sendNotificationToPatner() {

        Log.e(TAG, "in sendNotificationToPatner: ");

        SendNotificationModel sendNotificationModel = new SendNotificationModel("check", "i miss you");

        RequestNotificaton requestNotificaton = new RequestNotificaton();
        requestNotificaton.setSendNotificationModel(sendNotificationModel);

        requestNotificaton.setToken("cJ-y3b3mnYk:APA91bEYTp1LtfpMHp-wdvKkq1_dVYF4DXl1SHWIaw0guwPSoTyFtbcJjPPB6GhP0FuRd9ybJ--BM6W3aFB-2-3KpVOBDITX91QV4kH1lAgxsIxHcfaj5FQLOIZ0t_HDC2bd8hqJrH-F4x_wuunJfYzanAagm4xcaA");

        ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
        retrofit2.Call<ResponseBody> responseBodyCall = apiService.sendChatNotification(requestNotificaton);

        responseBodyCall.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(retrofit2.Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
                Log.d(TAG, "in onResponse");
            }

            @Override
            public void onFailure(retrofit2.Call<ResponseBody> call, Throwable t) {
                Log.d(TAG, "in onFailure");
            }
        });
    }
}

Thanks in advance.

Maulik Dodia
  • 1,629
  • 2
  • 21
  • 48
  • You can check exact behavior of DATA and Pure Notification from FCM https://firebase.google.com/docs/cloud-messaging/android/receive#sample-receive according to app states. – Rakesh Sep 10 '18 at 09:41
  • Let me check, Although I already checked. May be something I might missed. If you can point out here in comment something which I missed, It would be better for other developers as well.@Rakesh – Maulik Dodia Sep 10 '18 at 09:51

2 Answers2

6

Solution:

  1. If you are sending push notification from firebase console & your app is in background or killed then MyFirebaseMessagingService won't get called. (This is not mentioned in firebase documentation)

  2. If you are sending push notification from firebase console & your app is in foreground then MyFirebaseMessagingService will get called.

  3. If you are sending push notification from your server(I mean back-end i.e a website or some web based dashboard) & your app is in background or kill or in foreground then MyFirebaseMessagingService will get called.

Maulik Dodia
  • 1,629
  • 2
  • 21
  • 48
  • i am doing point 3 but i am not getting callback in MyFirebaseMessagingService can you please help me what i am doing wrong – MARSH Oct 30 '22 at 08:33
5

Well, I landed here looking for a solution to a different question but I can answer your question half-way.

If you are sending notification with data payload, then you can check the intent for specific extras. For example if in your console (firebase) you have a key ('custom_message') with some value ("This is my message") as shown in image below

enter image description here

You can listen to Intent's extras in the your launch activity which can be a splashscreen or mainactivity.

 Intent intent = getIntent(); 
    if (intent.getStringExtras("custom_message") != null) {
      //go to specific activity when notification is opened
    }
   else{
     //open open default launch activity 
    }

Now, earlier I said I will half-answer your question. Why? This is because I do not know how you would achieve that when the notification has no data payload. Hoping this helps someone.

@Maulik Dodia info is also very useful. Appreciated

Tonui Nicholus
  • 395
  • 2
  • 8