9

I have an app that should show headsup notification while the app is in both foreground and background(not in history).

In foreground case , i acheived this by the folowing method.

PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx);
    builder.setFullScreenIntent(pendingIntent,true);

But in background, it always showing in the notification area.Can anybody tell how to acheive this in background(not in history)

i tried the below parameters for notification but not working.

{ 
    "to" : "_Token_", 
    "priority":"high",
    "data":
    {
        "title": "Test",
        "text": "Fcm"
    },
    "notification":
    {
        "title": "Test",
        "text": "Fcm"
    }
}
SachinS
  • 2,223
  • 1
  • 15
  • 25

5 Answers5

4

You should completely delete notification part from your Json and just use data ! That's the trick. I mean this:

{ 
"to" : "_Token_", 
"priority":"high",
"data":
{
    "title": "Test",
    "text": "Fcm"
}
}

When you have notification tag in your Json, sometimes the library decide to handle the notifications itself. so when your app is in foreground it wont call your on message receiver and it show the notification itself.

Just remove the notification and always use data tag.

It works both when your app is in foreground/background or killed and stopped

Omid Heshmatinia
  • 5,089
  • 2
  • 35
  • 50
  • Why Google make it so complicated.. Anyway, "completely delete notification part" It worked for me very well :) – Tura May 31 '18 at 08:57
  • "completely delete notification part" works for me as well and pushes Android to show Heads-up notifications as expected https://developer.android.com/guide/topics/ui/notifiers/notifications#Heads-up – Eugene Brusov Dec 24 '18 at 15:43
  • But ios devices need "notificatoin", and without it, it doesn't work. So when I send messages to both devices, I need "notification", but then it doesn't show up in Android. Any ideas? – RJB Nov 18 '21 at 19:28
2

Firebase allows notification customization when data messages are used. Data messages invokes onMessageReceived() method even when the app in backgroud so you create your custom notification.

You can take reference of this link to know more about data notifications Firebase notifications

Neha
  • 76
  • 11
  • But if we use data messages we can't get the notifications once we clear the app from background. – SachinS Mar 03 '17 at 15:29
  • Data messages notifications are received even when we remove the app from recent apps – Neha Mar 03 '17 at 16:00
  • i tried notification messages and data messages .it is not working.The document says " When the app is in the background, Android directs notification messages to the system tray. A user tap on the notification opens the app launcher by default". – SachinS Mar 04 '17 at 13:22
0

instead of using setFullScreenIntent(), try this:

PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(ctx);
builder.setPriority(NotificationCompat.PRIORITY_MAX);
builder.setSound(URI_TO_SOUND);
builder.setVibrate(ARRAY_WITH_VIBRATION_TIME);

Edit: for background you should do simillar. In order to show Heads-up notification there should be combination of high priority + sound and/or vibration (preferably both).

Edit2: Preferably you can show this notification in onMessageReceived() method, here: https://firebase.google.com/docs/cloud-messaging/android/receive#override-onmessagereceived

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

Check this out: Create Heads-Up Display

Community
  • 1
  • 1
Walker
  • 1
  • 1
  • Welcome to Stack Overflow! Whilst this may theoretically answer the question, [it would be preferable](//meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – morten.c Mar 13 '17 at 13:44
0

You need to add your listener service, as you would in a standard GCM implementation.

    public class MyGcmListenerService extends GcmListenerService {

        private static final String TAG = "MyGcmListenerService";

        /**
         * Called when message is received.
         *
         * @param from SenderID of the sender.
         * @param data Data bundle containing message data as key/value pairs.
         *             For Set of keys use data.keySet().
         */
        // [START receive_message]
        @Override
        public void onMessageReceived(String from, Bundle data) {
            String message = data.getString("message");
            Log.d(TAG, "From: " + from);
            Log.d(TAG, "Message: " + message);

            if (from.startsWith("/topics/")) {
                // message received from some topic.
            } else {
                // normal downstream message.
            }

            // [START_EXCLUDE]
            /**
             * Production applications would usually process the message here.
             * Eg: - Syncing with server.
             *     - Store message in local database.
             *     - Update UI.
             */

            /**
             * In some cases it may be useful to show a notification indicating to the user
             * that a message was received.
             */
            sendNotification(message);
            // [END_EXCLUDE]
        }
        // [END receive_message]

Then, register your receiver in AndroidManifest.xml tag to listen on incoming notifications:

    <!-- [START gcm_listener] -->
    <service
        android:name="gcm.play.android.samples.com.gcmquickstart.MyGcmListenerService"
        android:exported="false" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
        </intent-filter>
    </service>
    <!-- [END gcm_listener] -->

This way - you won't have to handle incoming messages separately for cases when app is in foreground vs background.

https://developers.google.com/cloud-messaging/

Daniel
  • 935
  • 2
  • 6
  • 11