Search code examples
firebaseandroid-intentandroid-activityfirebase-cloud-messagingdeep-linking

handle push notification (FCM) while app is in background / kill - Android


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.


Solution

  • 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.