SachinS
SachinS

Reputation: 2253

How to get Heads Up notification in android using Firebase Cloud Messaging(Notification) while the app is not in foreground?

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

Upvotes: 9

Views: 6119

Answers (5)

Daniel
Daniel

Reputation: 975

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/

Upvotes: 0

Walker
Walker

Reputation: 1

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

Upvotes: 0

Omid Heshmatinia
Omid Heshmatinia

Reputation: 5236

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

Upvotes: 4

Raphau
Raphau

Reputation: 126

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

Upvotes: 0

Neha
Neha

Reputation: 76

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

Upvotes: 2

Related Questions