Firebase Messages - Create a Heads-Up Display When an App Is in the Background

With FCM, I get push notifications in the system tray when the application is in the background or not working. When the application is in the foreground, I can override onMessageReceived and create my own heads-up NotificationCompat using NotificationCompat .

Is there a way to create a heads-up notification when my application is in the background or not working?

thanks

EDIT: For reference: the message payload that I use via curl to https://fcm.googleapis.com/fcm/send

 { "to":"push-token", "content_available": true, "priority": "high", "notification": { "title": "Test", "body": "Mary sent you a message!", "sound": "default" }, "data": { "message": "Mary sent you a Message!", "notificationKey":"userID/notification_type", "priority": "high", "sound": "default" } } 
+6
android firebase-cloud-messaging android-notifications heads-up-notifications
source share
3 answers

You will receive a heading notification only if you are using some other application when your application is in the background or not working. If your phone is not in use, you will receive a notification about the system tray or a notification about the locked screen.

If you use the application server to send push notifications via the HTTP protocol, you can even set the priority to high in your json data sent to the fcm endpoint.

If you use the firebase console, in the "Advanced notification settings" section, make sure that the priority is high.

High priority ensures that you receive notification notifications in most cases.

EDIT . This is how your edited json should look like a successful test -

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

youriconname is the name of the resource that you want to set as the notification icon.

I skipped the data for testing purposes. Only this should give you a notice of the heads.

+2
source share

I found a solution: I just remove the notification tag from json sent to the firebase server from our local server, then I create a callback in the MyFirebaseMessagingService: onMessageReceived () method. n This method I generate a local notification using the NotificationCompat.Builder class. Here is the code for Android:

 private void sendNotification(RemoteMessage remoteMessage) { Intent intent = new Intent(this, SplashActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); intent.putExtra(AppConstant.PUSH_CATEGORY, remoteMessage.getData().get("category")); intent.putExtra(AppConstant.PUSH_METADATA, remoteMessage.getData().get("metaData")); intent.putExtra(AppConstant.PUSH_ACTIVITY, remoteMessage.getData().get("activity")); intent.putExtra(AppConstant.PUSH_ID_KEY, remoteMessage.getData().get("_id")); PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT); Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.mipmap.ic_launcher) .setContentTitle(remoteMessage.getData().get("title")) .setContentText(remoteMessage.getData().get("body")) .setAutoCancel(true) .setSound(defaultSoundUri) .setPriority(NotificationCompat.PRIORITY_HIGH) .setContentIntent(pendingIntent); NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0, notificationBuilder.build()); } 
+6
source share

Go to settings and activate access to notifications for your application.

0
source share

All Articles