10

With FCM I receive push notifications in the system tray when the app is in the background or not running. When the app is in the foreground I can override onMessageReceived and create my own heads-up notification with NotificationCompat.

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

Thanks

EDIT: For reference here is the message payload I am using 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"
    }
}
Brien Crean
  • 2,599
  • 5
  • 21
  • 46

2 Answers2

6

I found solution: I just remove notification tag from json sent to firebase server from our local server then I am generating callback in MyFirebaseMessagingService : onMessageReceived() method. n this method I am generating local notification using 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());
    }
Shivang
  • 935
  • 8
  • 18
  • Wow, Thanks a lot for the hint. onMessageReceived not called if we have notification section. It only calls when we remove notification section and move everything to a data section. – nuynait May 14 '18 at 17:34
5

You will get heads up notification only if you are using some other app while your app is in background or not running. If your phone is not being used then you will receive system tray notification or lock screen notification.

If you are using app server to send push notification via http protocol then you may even set priority as high in your json data sent to fcm endpoint.

If you are using firebase console then Under advanced notification section settings make sure priority is high.

High priority will ensure you receive heads up notifications in most cases.

EDIT: This is how your edited json should look like for 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 drawable resource that you want to set as your notification icon.

I have omitted data for test purpose. Just this much should give you heads up notification.

Nishant Dubey
  • 2,802
  • 1
  • 13
  • 18
  • 3
    Thanks for the reply. I added the JSON payload I am using via curl to my question. I just double checked and I get a sound notification when I am in another app (or app not running) but I don't get a heads-up notification at all. – Brien Crean Sep 25 '16 at 06:20
  • Looking at your json. I would suggest you two things instantly - remove **Sound and Priority from Data**. Keep it only at one location. Also **there is no need to set content_available unless you developing for iOS**. Once test with this setup and also test with removing data completely keep notification only as suggested and then see how it behaves. – Nishant Dubey Sep 25 '16 at 08:09
  • Also note that **you don't have to use notification compat builder to make notifications while using fcm. As fcm will itself handle it. I would also ask you to remove any such notification code for now. Once you are getting proper heads up notifications from pure fcm then you can customize further.** – Nishant Dubey Sep 25 '16 at 08:16
  • 5
    Not Working...Its not showing heads up notification. – Shivang Oct 25 '16 at 11:56
  • @Shivang that can be possible due to many other conditions which might not be true in your context. The procedure explained above is definetly right. If its not working in your case then explain your context reason might be different for you. – Nishant Dubey Oct 25 '16 at 11:59
  • @Nishant: Here is json we are using: Please check the post. – Shivang Oct 26 '16 at 05:28