Search code examples
javaandroidpush-notificationfirebase-cloud-messagingandroid-push-notification

Custom notification like Inshorts


I want to create a custom notification like inshorts as shown in Image below

enter image description here

And i am able to achieve that as well, shown in screenshot below :

enter image description here

But there are two problem cases:

  1. When i install the app using Android Studio, initially it works fine i.e. Custom Notification layout is displayed for the notification(as shown in picture-3), but when i am killing the app from task manager and then i am sending the notification, it comes as default layout(as shown in picture-4).
  2. When i install the app using the apk on any device, i am getting the notification style as default notification(as shown in picture-4).

I have been stuck in this for long and have changed the whole code of Notification, i am still facing the issue. I am not sure where i am doing wrong.

Picture-3

enter image description here

Picture-4

enter image description here


Solution

  • I was able to achieve what i was looking for i.e. I was looking forward to use the custom layout for showing Notifications eveytime, but the problem was that i was getting custom layout notification if the app used to be in the background and the default notification style when the app is not in the background.

    "So what exactly the problem was, Push notifications behave differently in the cases where the app is in background or foreground."

    When the app used to be in background the Notification builder code which i have written was being used and the notification with custom layout was displayed. Moreover, when the app used to be in foreground, the android system couldn't reach the notification builder code of my project, in that case notifications were being processed by the Google Service process, which uses the default notification layout.

    Solution :

    Earlier, the JSON object which i was getting from the server was named as "notification", which generally is used by almost every developer. Changing the name of that JSON object to "data" did the magic.

    And the code inside the Notification builder used to be something like this:

    String notTitle = remoteMessage.getData().get("title");
    

    which ealier used to be:

    String notTitle = remoteMessage.getNotification().getTitle();
    

    This way, my notifications are always being handled by the app, by the NotificationService, and not by the Google Service process.

    I hope this helps somebody. :)