I use fcm and heads up notification will show when app is open but not show when app is not open or killed. How to handle heads up notification when app is not open?
-
so far I know, FCM automatically shows notification when a message is received and the app is in the background. – Nabin Bhandari Sep 25 '17 at 03:00
-
yes normal notification is show but heads up notification not show. – FakeTon T Sep 25 '17 at 03:02
-
@FakeTonT have you got the solution for this one man? – Ram Suthakar Aug 16 '18 at 08:20
2 Answers
Doc say :
With Android 5.0 (API level 21), notifications can appear in a small floating window (also called a heads-up notification) when the device is active (that is, the device is unlocked and its screen is on). These notifications appear similar to the compact form of your notification, except that the heads-up notification also shows action buttons. Users can act on, or dismiss, a heads-up notification without leaving the current app.
As per Doc, If you want heads-up notification you have to create your own as below :
notificationBuilder.setPriority(Notification.PRIORITY_HIGH);
if (Build.VERSION.SDK_INT >= 21) notificationBuilder.setVibrate(new long[0]);
Don't abuse heads-up notification. See here for when to use heads-up notification:
MAX: For critical and urgent notifications that alert the user to a condition that is time-critical or needs to be resolved before they can continue with a particular task.
HIGH: Primarily for important communication, such as messages or chat events with content that is particularly interesting for the user. High-priority notifications trigger the heads-up notification display.
Additional note from HERE
Update :
To override GCM listener service :
<service android:name=".MyGcmListenerService"
android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
FCM would be :
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
Then override method :
GCM :
public class MyGcmListenerService
extends GcmListenerService {
@Override
public void onMessageReceived(String from, Bundle data) {
... create your heads-up notification here.
}
FCM :
public class MyFirebaseMessagingService extends FirebaseMessagingService {
/**
* Called when message is received.
*
* @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
*/
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
... create your heads-up notification here.
}

- 1,967
- 3
- 21
- 27
-
i try this but when app is not open. notification show only in system trays and heads-up notification not show. – FakeTon T Sep 25 '17 at 04:06
-
@FakeTonT yes you have to override existing receiver from FCM/GCM. default notification doesn't show as heads-up. – james Sep 25 '17 at 04:13
-
yes i put in onMessageReceived but when app is not open heads-up not show and show only when app is open. what i miss ? my phone is android android 6.0.1 – FakeTon T Sep 25 '17 at 04:34
-
@FakeTonT Then you should show your codes how you had implemented. If you received FCM message then heads-up notification will display. – james Sep 25 '17 at 04:38
-
@FakeTonT You know your are code ".setOnlyAlertOnce(true)" ??. Which means it will alert only once until it is removed the existing. Set false, and try to test your code again. – james Sep 25 '17 at 04:58
-
-
@FakeTonT Are you able to receive the message from onMessageReceived method ?? there is nothing wrong with heads-up notification. – james Sep 25 '17 at 06:47
-
i receive message and show in system tray but not heads up notification https://imgur.com/a/GYVgU – FakeTon T Sep 25 '17 at 06:56
-
Can't post in comment, so here it is. Try this, i had tested :
private void test() {
Intent intent;
intent = new Intent(this, SplashScreenActivity.class);
Bundle bundle = new Bundle();
bundle.putBoolean("isDisplayAlert", true);
bundle.putString("NOTIFICATION_DATA", "data");
intent.putExtras(bundle);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),
new Random().nextInt(), intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_location)
.setContentTitle("Title")
.setContentText("Body")
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setOnlyAlertOnce(true)
.setFullScreenIntent(pendingIntent, true);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationBuilder.setPriority(Notification.PRIORITY_HIGH);
notificationBuilder.setVibrate(new long[0]);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

- 1,967
- 3
- 21
- 27
-
i test by console firebase and shoot to my phone or emu. it same happen heads up notification not show but show only normal notification. – FakeTon T Sep 25 '17 at 07:17
-
https://imgur.com/a/rHc72 my code and manifest https://imgur.com/a/3odHb – FakeTon T Sep 25 '17 at 07:31
-
i think when app is in background mode. app not call onMessageReceived and not show heads-up notification but show normal notification – FakeTon T Sep 26 '17 at 04:13