Heads up notification using FCM
Asked Answered
R

4

7

Do we have any way to show heads up notification, when receive push from Firebase Cloud Messaging? In foreground it is possible using Notification. But there is no way to show heads up notification, when app in background, because onMessageReceived is not calling.
I tried to set priority to "high", but no result.

Do you have any ideas?

Roanne answered 26/10, 2017 at 13:48 Comment(3)
Also seeing this behavior.Reinhold
Possible duplicate of Firebase Messaging - Create Heads-Up display when app in backgroundTawnytawnya
You can find solution here.. #39682620Tawnytawnya
C
5

After browsing multiple solutions in stackoverflow.com, I found below explanation to be most helpful

How to handle notification when app in background in Firebase

  1. In your Android OnMessageReceived function/method, only Data Payload (RemoteMessage.getData) will trigger your custom notification such as head-up notification when App is in background. Notification Payload (RemoteMessage.getNotification) will not trigger your heads-up notification, and instead default to showing just a tiny icon on your notification tray.

  2. If you're using Firebase Cloud Functions, do check that the information you're sending through is in the data payload, and notification payload should be removed if you want the heads up notification to work. Having notification payload will default to showing just a tiny icon on the notification tray only.

Hope this piece of information helps, and happy coding.

Calcifuge answered 5/4, 2018 at 3:45 Comment(1)
And be sure you go into your app's 'app info' -> Notification Channel (e.g., 'Miscellaneous') -> "Pop on screen" is turned on, otherwise just the icon in the notification tray will continue to show.Forbis
T
1

I am using node.js as my server platform. The trick is to remove the 'notification' field and move the 'title' and 'body' fields to 'data'. For my case, I changed:

var message = { 
    token: targetID, 
    android:{
        priority: 'high'
    },
    notification: {
        title: 'X', 
        body: req.body.msg 
    },
    data: {
        topic: req.body.topic
    }
};

to

var message = { 
    token: targetID, 
    android:{
        priority: 'high'
    },
    data: {
        topic: req.body.topic,
        title: 'X', 
        body: req.body.msg 
    }
};

then...

fcm.send(message, function(err, response){...
Thrifty answered 28/11, 2019 at 9:9 Comment(0)
F
0

Please follow the below steps.. Add title and text as you want

enter image description here

Now just click next on following two steps with the default setup enter image description here

This step is very important and this is the main part which will manage the device’s system tray to show as a heads-up notification.

Now click on review and then publish. Let me know if you get the expected result.

Fusty answered 26/2, 2019 at 16:39 Comment(1)
it's useful @FustySinew
N
-1

Regardless of what platform you are calling firebase, all what you need is to add Title and Body fields to Notification inside Android and of course Priority = High

            var aMessage = new Message
            {
                Topic = "atopic",
                Notification = new Notification { Body = body, Title = title },
                Data = new Dictionary<string,string> { { "ReferenceId", refId.ToString() } },
                Android = new AndroidConfig
                {
                    Priority = Priority.High,
                    Notification = new AndroidNotification
                    {
                        Body = body,
                        Title = title,
                        Color = "#ffcc00",
                        Icon = "ic_notification",
                        Sound = "default",
                        ChannelId = "my_notification_channel"
                    },
                },
            };
Nitrosamine answered 8/2, 2020 at 8:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.