7

I want to know that how can I change the notification bar small icon in android Oreo (API 26). It has round white icon showing in the notification bar. How can I change it? Manifest file default notification icon set as below.

<meta-data
     android:name="com.google.firebase.messaging.default_notification_icon"
     android:resource="@drawable/ic_status" />

See the image below

Screenshot

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Shanu
  • 169
  • 2
  • 10

2 Answers2

5

There is a bug in Firebase SDK 11.8.0 on Android 8.0 (API 26), which causes the display of a white version of the app's launcher icon in the status bar instead of the notification icon.

screenshot 1

Some people have fixed it by overriding the Application class's getResources() method.

Another way that worked for me was to use an HTTP POST request to send the notification as a data message:

https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
{
   "to": "/topics/test",
   "data": {
       "title": "Update",
       "body": "New feature available"
    }
 }

And then subclass FirebaseMessagingService to display the notification programmatically:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Intent i = new Intent(context, HomeActivity.class);
        PendingIntent pi = PendingIntent.getActivity(this, 0, i, 
                                         PendingIntent.FLAG_ONE_SHOT);

        NotificationCompat.Builder builder = 
            new NotificationCompat.Builder(this, GENERAL_CHANNEL_ID)
                 .setSmallIcon(R.drawable.ic_stat_chameleon)
                 .setContentTitle(remoteMessage.getData().get("title"))
                 .setContentText(remoteMessage.getData().get("body"))
                 .setContentIntent(pi);

        NotificationManager manager = 
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(0, builder.build());
    }
}

In the end, I got this, even on Android 8.0:

screenshot 2

Josselin
  • 2,593
  • 2
  • 22
  • 35
-4
  1. Remove <meta-data> tag from AndroidManifest.xml.
  2. Notification Builder has .setSmallIcon(int icon, int level) which accepts icon as a compulsory argument & level parameter as an optional argument.

NOTE: .setSmallIcon accepts drawables & DOES NOT accept mipmaps.

This is how it should be according to Android 8.0 Oreo Notification Channels & behaviour changes:

public class MainActivity extends AppCompatActivity {

    private CharSequence name;
    private int notifyId;
    private int importance;
    private String id;
    private String description;

    private Notification mNotification;
    private NotificationManager mNotificationManager;
    private NotificationChannel mChannel;
    private PendingIntent mPendingIntent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        binding.btnNotification.setOnClickListener(v -> notification());
    }

    private void notification() {

        mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notifyId = 1;
        description = "Hello World, welcome to Android Oreo!";

        Intent intent = new Intent(this, MainActivity.class);
        mPendingIntent = PendingIntent.getActivity(this, notifyId, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        if (SDK_INT >= O) {
            id = "id";
            name = "a";
            importance = NotificationManager.IMPORTANCE_HIGH;

            mChannel = new NotificationChannel(id, name, importance);
            mChannel.setDescription(description);
            mChannel.enableLights(true);
            mChannel.setLightColor(Color.WHITE);
            mChannel.enableVibration(true);
            mChannel.setVibrationPattern(new long[] {100, 300, 200, 300});
            mNotificationManager.createNotificationChannel(mChannel);

            mNotification = new Notification.Builder(MainActivity.this, id)
                .setContentTitle(id)
                .setContentText(description)
                .setContentIntent(mPendingIntent)
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .build();
        } else {
            mNotification = new Notification.Builder(MainActivity.this)
                .setContentTitle(id)
                .setContentText(description)
                .setContentIntent(mPendingIntent)
                .setSmallIcon(R.drawable.ic_launcher_foreground)
                .setLights(Color.WHITE, Color.RED, Color.GREEN)
                .setVibrate(new long[] {100, 300, 200, 300})
                .build();
        }
            mNotificationManager.notify(notifyId, mNotification);
    }
}
Sneh Pandya
  • 8,197
  • 7
  • 35
  • 50
  • As i know, fcm onmessagereceived didnt calling when app is in the bg or killed. Because of that i have set default icon in the manifest. Will it work in this method, when app is in background or killed? – Shanu Jan 10 '18 at 10:05
  • Yes it works because the object `mNotification` is notified via `NotificationManager` and set to the Notification Channel. – Sneh Pandya Jan 10 '18 at 10:10
  • little bit confusing in here, what is the activity that i want to change like this? is it my mainativity class or the firebasemessagingservice class? – Shanu Jan 10 '18 at 10:39
  • You can put the `notification()` method anywhere you want to trigger it from. I have put it in a activity as an example where the notification gets triggered when I click the related button. You can trigger the notification via a service or activity. – Sneh Pandya Jan 10 '18 at 10:40
  • i have placed it in the firebasemessagingservice class and call it in the onmessagereceived method. it is working when the app is in the foreground, but it is not working when app is in the background or killed. – Shanu Jan 10 '18 at 11:08
  • To trigger notifications from `FirebaseMessagingService`, you need to override `onMessageReceived`. Also you need to implement this method in order to successfully handle notifications: https://github.com/firebase/quickstart-android/blob/3bc0a364aa4f66181b7962956adcab41f3fd4b1c/messaging/app/src/main/java/com/google/firebase/quickstart/fcm/MyFirebaseMessagingService.java#L111 – Sneh Pandya Jan 10 '18 at 11:58
  • thank you sir its working. but in android oreo it is only working white small icons. thank you for your help. – Shanu Jan 18 '18 at 13:30
  • @SnehPandya how will your solution solve the issue for the notifications when app is not in foreground and FCM delivers the notification to system tray? – Umar Hussain Feb 09 '18 at 11:38