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

I have an application that should display a heads-up notification when the application is in the foreground and in the background (not in history).

In the case of the foreground, I got this using the following 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 the background, it is always displayed in the notification area. Someone will tell you how to do this in the background (not in history).

I tried the following options for notification, but did not work.

 { "to" : "_Token_", "priority":"high", "data": { "title": "Test", "text": "Fcm" }, "notification": { "title": "Test", "text": "Fcm" } } 
+7
android firebase-cloud-messaging
source share
5 answers

Firebase lets you customize notifications when using data messages. Data messages call the onMessageReceived () method, even when the application is in backgroud, so you create your own notification.

You can link to this link to learn more about data notifications. Firebase notifications

+2
source share

instead of using setFullScreenIntent (), try the following:

 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 have to make simillar. To show a Heads-up notification, there must be a combination of high priority + sound and / or vibration (preferably both).

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

0
source share

You should completely remove the notification part from your Json and just use the data! This is a trick. I mean this:

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

When you have a notification tag in Json, sometimes the library decides to handle the notifications themselves. therefore, when your application is in the foreground, it will not call your message receiver, and it displays the notification itself.

Just delete the notification and always use the data tag.

It works when your application is in the foreground / background or killed and stopped

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

Check it out: Create a Header Manager

0
source share

You need to add a listener service, as in the standard implementation of GCM.

  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 the receiver in the AndroidManifest.xml tag to listen for 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] --> 

Thus, you do not have to process incoming messages separately for cases when the application is in the foreground and background.

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

0
source share

All Articles