1

I am following this tutorial to implement Firebase Push Notification functionality within my app.

But I found one thing, if app is in foreground then only I am getting (showing) message in a Toast and TextView.

Other hand, If app is in background on tap neither I am not getting message to show in a TextView and Toast.

Whereas I would like to show message in Toast and TextView in both the situations (Either App is in Foreground or Background).

NOTE: I am pushing message from Firebase console itself.

Is it possible ?

MyFirebaseMessagingService.java

private void handleNotification(String message) {
        if (!NotificationUtils.isAppIsInBackground(getApplicationContext())) {
            // app is in foreground, broadcast the push message
            Intent pushNotification = new Intent(Config.PUSH_NOTIFICATION);
            pushNotification.putExtra("message", message);
            LocalBroadcastManager.getInstance(this).sendBroadcast(pushNotification);

            // play notification sound
            NotificationUtils notificationUtils = new NotificationUtils(getApplicationContext());
            notificationUtils.playNotificationSound();
        }else{
            // If the app is in background, firebase itself handles the notification
        }
    }

MainActivity.java

mRegistrationBroadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {

                // checking for type intent filter
                if (intent.getAction().equals(Config.REGISTRATION_COMPLETE)) {
                    // gcm successfully registered
                    // now subscribe to `global` topic to receive app wide notifications
                    FirebaseMessaging.getInstance().subscribeToTopic(Config.TOPIC_GLOBAL);

                    displayFirebaseRegId();

                } else if (intent.getAction().equals(Config.PUSH_NOTIFICATION)) {
                    // new push notification is received

                    String message = intent.getStringExtra("message");

                    Toast.makeText(getApplicationContext(), "Push notification: " + message, Toast.LENGTH_LONG).show();

                    txtMessage.setText(message);
                }
            }
        };
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Sophie
  • 2,594
  • 10
  • 41
  • 75
  • if you want to handle notifications in background and foreground then you may need to have a look to this answer [Here](https://stackoverflow.com/questions/37711082/how-to-handle-notification-when-app-in-background-in-firebase) and to post the notifications to app using postman you may need to see this answer aswell [Here](https://stackoverflow.com/questions/38834020/sending-push-via-postman-using-firebase-messaging) – Mohammed Farhan Aug 02 '17 at 06:20

1 Answers1

1

FCM has two types of messages, Notification and Data. Use notification messages when you want FCM to handle displaying a notification on your client app's behalf. Use data messages when you want to process the messages on your client app.

Below is the sample,

 {
  "to": “token ID”,
  "notification": {
     //params
   },
  "data": {
    //params
   }
}

Behaviour when the payload with types of messages,

Notification Messages

  1. Foreground - onMessageReceived fired
  2. Background - Notification appears on the System tray and handled by FCM
  3. App not running - Notification appears on the System tray and handled by FCM

Data Messages

  1. Foreground - onMessageReceived
  2. Background - onMessageReceived
  3. App not running - onMessageReceived

Both Notification and Data

  1. Foreground - onMessageReceived
  2. Background - Notification in the tray and the data payload will be handled via extras of the intent on tap
  3. App not running - Notification in the tray and the data payload will be handled via extras of the intent on tap.

Hope it helps!

Chithra B
  • 586
  • 2
  • 9