2

I've found similar problems regarding small icons on android notifications, but Google won't give me any hints to following issue:

Simply and short, my notifications won't show color and icon when the app is not opened, but it'll work perfectly while the app is visible on the screen.

App is running in background when notification appears:

App is running in background when notification appears

App is visible on the screen when notification appears:

App is visible on the screen when notification appears

For me it seems that the service fails to load some resources with its current context. Currently I'm using my own python server calling the firebase REST Api for sending the notifications.

While Title and Text-Body are sent directly via the notification's own payload, the icon and color are chosen by the app.

This is how the notification is built in the service.

final Context context = this.getApplicationContext();

final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                .setDefaults(Notification.DEFAULT_SOUND)
                .setContentTitle(notification.getTitle())
                .setContentText(notification.getBody())
                .setColor(ContextCompat.getColor(context, R.color.notification))
                .setSmallIcon(R.drawable.ic_local_shipping_white_24dp);

And this is the snippet for eventually showing the notification:

final NotificationManagerCompat mNotificationManager = NotificationManagerCompat.from(context);
final boolean notificationsEnabled = mNotificationManager.areNotificationsEnabled();
if (notificationsEnabled) {
    mNotificationManager.notify(notifyID, mBuilder.build());
}

I appreciate your help!

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
TheWhiteLlama
  • 1,276
  • 1
  • 18
  • 31
  • Possible duplicate of [Grey square as notification icon using Firebase notifications](http://stackoverflow.com/questions/39157134/grey-square-as-notification-icon-using-firebase-notifications) – Manza Dec 02 '16 at 11:25
  • if you test push using firebase push notification tool then push not working well because if your app in background mode at that time notification received then this notification will manage by system and system show your app Icon that's why your app not showing proper notification – Ravi Makvana Dec 02 '16 at 11:28
  • @Manza I've tried to add it to the manifest, but it still won't work. Since I also want to decide which icon to use dynamically it's probably another usecase to. I did read very often that it has to be a white icon on transparent, I also can verify this for the material icon I'm using at the moment. – TheWhiteLlama Dec 02 '16 at 11:30
  • @Manza your solution is perfect for TheWhiteLlama(You) just add there lines in your Manifeast.xml – Ravi Makvana Dec 02 '16 at 11:32
  • you have to try push using REST and do not pass notification Message then it's works well – Ravi Makvana Dec 02 '16 at 11:33
  • https://firebase.google.com/docs/notifications/android/console-device read Receive and handle messages content – Ravi Makvana Dec 02 '16 at 11:35
  • @TheWhiteLlama what version of firebase are you using? – Manza Dec 02 '16 at 11:38
  • @Manza 9.2.0 for core and messaging. I've tried the other solutions and it did not work. I'm currently already using REST not the console. But since I want to pass a server side depending notification I don't wanna leave out the message text. – TheWhiteLlama Dec 02 '16 at 11:41
  • 1
    @TheWhiteLlama the solution I linked works from firebase version 9.8.0. Or check my answer below – Manza Dec 02 '16 at 11:43
  • @Manza Oh haha, I thought it was the latest version already. I will try to update and test your solutions again. – TheWhiteLlama Dec 02 '16 at 11:47
  • 1
    @TheWhiteLlama I've updated the answer – Manza Dec 02 '16 at 11:55

1 Answers1

2

You can try to:

1. Update to version 9.8.0 and check THIS solution

2. Set icon and color when sending the push notification.

Example JSON:

{
     "notification" : {
      "body" : "My amazing body!",
      "title" : "My amazing title",
      "icon" : "ic_logo",
      "sound" : "default",
      "color" : "#D63A49"
    },
    "registration_ids": ["XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"]
}

ic_logo is the resource name

3. Send data notification. This way the onMessageReceived(RemoteMessage message) method will be called even if the app is in backgrdound

Example JSON:

{
    "data": {
        "my_custom_key" : "my_custom_value",
        "whatever" : "YES",
        "other_key" : true
     },
    "to": "XXXXXXX"
}
Community
  • 1
  • 1
Manza
  • 3,427
  • 4
  • 36
  • 57