Heads-up push notification from REST and Firebase when app is in background or foreground

Md. Asaduzzaman Noor
4 min readFeb 26, 2019

--

Heads-up notification sample image

Let’s start with some casual definition > Beginning with Android 5.0, notifications can briefly appear in a floating window called a heads-up notification. This behaviour is normally for important notifications that the user should know about immediately, and it appears only if the device is unlocked.

Foreground state

Let’s discuss about the foreground status of your app where you can easily manage notification popups, sounds and vibrations by your own code.

Now, the point is any notification you can display very easily with your minimum code, will not achieve the honour of being a heads-up notification. If you have already tried then you will know better what I mean to say. If you didn’t try yet then you may try first then you can differentiate with my given code.

I am showing only the process of showing heads-up notification. So, you have to finish the minimum implementation(manifest code, firebase setup for push and notification icon) of showing a normal notification.

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
sendNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody(),remoteMessage.getData());}
private void sendNotification(String messageTitle, String messageBody, Map<String, String> data) {Intent intent = HomeActivity.getHomeActivityIntent(this,data.get(Constants.PUSH_URL));PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,PendingIntent.FLAG_ONE_SHOT);NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, "1").setSmallIcon(R.drawable.icon_notification).setContentTitle(messageTitle).setContentText(messageBody).setContentIntent(pendingIntent).setDefaults(DEFAULT_SOUND | DEFAULT_VIBRATE) //Important for heads-up notification
.setPriority(Notification.PRIORITY_MAX); //Important for heads-up notification
Notification buildNotification = mBuilder.build();int notifyId = (int) System.currentTimeMillis(); //For each push the older one will not be replaced for this unique id// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String name = getString(R.string.channel_name);
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_HIGH; //Important for heads-up notification
NotificationChannel channel = new NotificationChannel(getResources().getString(R.string.default_notification_channel_id),name,importance);
channel.setDescription(description);
channel.setShowBadge(true);
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
NotificationManager notificationManager = getSystemService(NotificationManager.class); if (notificationManager != null) { notificationManager.createNotificationChannel(channel);
notificationManager.notify(notifyId, buildNotification);
}
}else{
NotificationManager mNotifyMgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
if (mNotifyMgr != null) {
mNotifyMgr.notify(notifyId, buildNotification);
}
}
}

To display heads-up, copy and paste the whole code and also fix errors based on strings and imports. After getting successful heads-up, remove or add anything as your requirement.

Background state

When your app is in background I believe that you don’t have any control on your app. So, the above code has no effect on background state. Each and every action will be taken by the system tray. All my saying and brief is for upgraded API versions.

So, what’s the plan to manage system tray of a device? Not just manage we have to convince or make it understand that this particular notification is a heads-up notification not a general one.

We have to play with the payload that is coming from firebase console or any REST api to your app.

Heads-up Notification from Firebase Console

Add title and text as you want
Select your app and keep other fields blank

Now just click next on following two steps with the default setup

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.

If you got the result as expected then don’t forget to give some claps. Let’s jump into the next part where you may want to send notification from your server using REST Api.

Heads-up Notification from REST Server

I think at first you need to read some doc from the android official page to get some understanding when making your payload.

From the doc you would get some understanding which keys should add where and how will be the flow of your JSON.

Please check out the below coding to get better understanding about the minimum code by which you can display heads-up notification.

var payload = new
{
to = deviceTokens.First(),

notification = new{
title = title,
body = message,
android_channel_id = "1",
sound = "default"
},
data = new{
url = url
},
content_available = false, //important for iOS
priority = "high",
time_to_live = 50000
};

You have to convert this code with your desired programming language by which you have implemented your REST server.

Please comment below if you have anything to ask or any kind of help on this topic. Thanks and inspire by claps.

--

--

Md. Asaduzzaman Noor

Android App Developer using Kotlin, Java and Android Studio