1

I have a small problem with my Android app. It received notifications through FCM and shows them as push notification. So far it is all working, but the strange problem is, sometimes the icon is white and sometimes it is colourful.

When the app is open on the screen and I receive a push notification in this moment, the colourful push notification is shown on top of the screen.

When the app is closed, I get a push notification with a white icon.

I have attached a screenhot: Screenshot

Here is the code snippet, where the push notification is created:

        Notification.Builder notificationBuilder = new Notification.Builder(this)
            .setSmallIcon(android.R.drawable.ic_dialog_alert)
            .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
            .setAutoCancel(true)
            .setVisibility(Notification.VISIBILITY_PUBLIC)
            .setPriority(Notification.PRIORITY_HIGH)
            .setColor(Color.parseColor("#83c3ed"))
            .setLights(Color.RED, 1000, 500)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);
    Notification.InboxStyle inboxStyle = new Notification.InboxStyle();
    inboxStyle.setBigContentTitle("WetterApp");
    inboxStyle.addLine(notification.getTitle());
    inboxStyle.addLine(notification.getBody());
    notificationBuilder.setStyle(inboxStyle);

    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    notificationManager.notify(0, notificationBuilder.build());

My mobile device has Android 6.0.1, my SDK-version is 23.

Thanks for your help.

3 Answers3

4

Well, I think the problem was something different. My code for creation the notification is only called, when the app is open on screen. When I receive a notification and the app is closed, the notification is handled by the Android system automaticly.

I had to set the color in the notification, which is send to the FCM Server:

 $data = [
        'notification' => [
            'title' => 'Warnung: Wohnzimmer',
            'text' => 'Innen: 20,3°C Außen: 24,5°C, Tendenz: -0,2°C',
            'color' => '#83c3ed',
            'sound' => 'default'
        ],
        'to' => '/topics/testNotification'
    ];

Now I get a lightblue background icon within the app and also when the app is closed.

0

Follow the guide of google to create your icons, it's probably that it is occurring in the status bar too.

Then, try this:

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
            builder.setTicker(context.getResources().getString(R.string.app_name));
            builder.setSmallIcon(R.mipmap.ic_your_status_bar_logo);
            builder.setAutoCancel(true);
            builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
            builder.setContentIntent(pendingIntent);          
       builder.setContentTitle(
context.getResources().getString(R.string.app_name));
            builder.setContentText(message);
            builder.setDefaults(Notification.DEFAULT_SOUND);
            builder.setPriority(NotificationCompat.PRIORITY_HIGH);
            builder.setColor(ContextCompat.getColor(context, R.color.color_primary));
            NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            nm.notify(NOTIFICATION_ID, builder.build());
jmarkstar
  • 1,335
  • 14
  • 21
-1

I think that a better solution is to add a silhouette icon to the app and use it if the device is running Android Lollipop.

For instance:

Notification notification = new Notification.Builder(context)
            .setAutoCancel(true)
            .setContentTitle("My notification")
            .setContentText("Look, white in Lollipop, else color!")
            .setSmallIcon(getNotificationIcon())
            .build();

    return notification;

And, in the getNotificationIcon method:

private int getNotificationIcon() {
    boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP);
    return useWhiteIcon ? R.drawable.icon_silhouette : R.drawable.ic_launcher;
}
Quick learner
  • 10,632
  • 4
  • 45
  • 55