Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Messaging - Create Heads-Up display when app in background

With FCM I receive push notifications in the system tray when the app is in the background or not running. When the app is in the foreground I can override onMessageReceived and create my own heads-up notification with NotificationCompat.

Is there a way to create a heads-up notification when my app is in the background or not running?

Thanks

EDIT: For reference here is the message payload I am using via curl to https://fcm.googleapis.com/fcm/send

{
  "to":"push-token",
    "content_available": true,
    "priority": "high",
    "notification": {
      "title": "Test",
      "body": "Mary sent you a message!",
      "sound": "default"
    },
    "data": {
      "message": "Mary sent you a Message!",
      "notificationKey":"userID/notification_type",
      "priority": "high",
      "sound": "default"
    }
}
like image 495
Brien Crean Avatar asked Sep 25 '16 00:09

Brien Crean


People also ask

How do I handle the Firebase notification when an app is in foreground?

Firebase notifications behave differently depending on the foreground/background state of the receiving app. If you want foregrounded apps to receive notification messages or data messages, you'll need to write code to handle the onMessageReceived callback.

Can Firebase do push notifications?

Firebase Cloud Messaging (FCM) provides a reliable and battery-efficient connection between your server and devices that allows you to deliver and receive messages and notifications on iOS, Android, and the web at no cost.

How does Firebase push notification work internally?

The FCM backend receives the message request, generates a message ID and other metadata, and sends it to the platform specific transport layer. When the device is online, the message is sent via the platform-specific transport layer to the device. On the device, the client app receives the message or notification.


Video Answer


2 Answers

You will get heads up notification only if you are using some other app while your app is in background or not running. If your phone is not being used then you will receive system tray notification or lock screen notification.

If you are using app server to send push notification via http protocol then you may even set priority as high in your json data sent to fcm endpoint.

If you are using firebase console then Under advanced notification section settings make sure priority is high.

High priority will ensure you receive heads up notifications in most cases.

EDIT: This is how your edited json should look like for successful test -

{
  "to":"push-token",
    "priority": "high",
    "notification": {
      "title": "Test",
      "body": "Mary sent you a message!",
      "sound": "default",
      "icon": "youriconname"
    }
}

youriconname is the name of drawable resource that you want to set as your notification icon.

I have omitted data for test purpose. Just this much should give you heads up notification.

like image 143
Nishant Dubey Avatar answered Sep 21 '22 14:09

Nishant Dubey


I found solution: I just remove notification tag from json sent to firebase server from our local server then I am generating callback in MyFirebaseMessagingService : onMessageReceived() method. n this method I am generating local notification using NotificationCompat.Builder class. Here is the code for android:

private void sendNotification(RemoteMessage remoteMessage) {
        Intent intent = new Intent(this, SplashActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra(AppConstant.PUSH_CATEGORY, remoteMessage.getData().get("category"));
        intent.putExtra(AppConstant.PUSH_METADATA, remoteMessage.getData().get("metaData"));
        intent.putExtra(AppConstant.PUSH_ACTIVITY, remoteMessage.getData().get("activity"));
        intent.putExtra(AppConstant.PUSH_ID_KEY, remoteMessage.getData().get("_id"));

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

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(remoteMessage.getData().get("title"))
                .setContentText(remoteMessage.getData().get("body"))
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0, notificationBuilder.build());
    }
like image 34
Shivang Avatar answered Sep 18 '22 14:09

Shivang