Search code examples
androidandroid-notificationsfirebase-cloud-messagingheads-up-notifications

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"
    }
}

Solution

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