Oreo Notification Bar Round Icon
Asked Answered
M

2

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

Moa answered 10/1, 2018 at 8:3 Comment(6)
This is the link to the screenshot of the notification bar. ibb.co/d4i2P6Moa
Is this issue only happening on 8.0? Have you checked it on any other 5+ devices? Are there any alpha values in you notification icon?Handshake
Yes it is only happening in android oreo devices, below api 26 devices, icon is showing correctly.Moa
can you try that remove meta-dataSimonnesimonpure
If meta data removed, will it show notification icon when app is in the background or killed?Moa
@Moa are you able to solve the issue when the app is in background ?Gail
M
-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);
    }
}
Methaemoglobin answered 10/1, 2018 at 9:15 Comment(8)
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?Moa
Yes it works because the object mNotification is notified via NotificationManager and set to the Notification Channel.Methaemoglobin
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?Moa
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.Methaemoglobin
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.Moa
To trigger notifications from FirebaseMessagingService, you need to override onMessageReceived. Also you need to implement this method in order to successfully handle notifications: github.com/firebase/quickstart-android/blob/…Methaemoglobin
thank you sir its working. but in android oreo it is only working white small icons. thank you for your help.Moa
@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?Gail
Q
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

Quixotism answered 14/3, 2018 at 23:31 Comment(0)
M
-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);
    }
}
Methaemoglobin answered 10/1, 2018 at 9:15 Comment(8)
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?Moa
Yes it works because the object mNotification is notified via NotificationManager and set to the Notification Channel.Methaemoglobin
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?Moa
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.Methaemoglobin
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.Moa
To trigger notifications from FirebaseMessagingService, you need to override onMessageReceived. Also you need to implement this method in order to successfully handle notifications: github.com/firebase/quickstart-android/blob/…Methaemoglobin
thank you sir its working. but in android oreo it is only working white small icons. thank you for your help.Moa
@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?Gail

© 2022 - 2024 — McMap. All rights reserved.