1

I want to change my push notification icon.

Code Snippet :

public class Mymessagingservice extends FirebaseMessagingService {

public  void onMessageReceived(RemoteMessage remoteMessage){
    super.onMessageReceived(remoteMessage);
    getimage(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());

}
public void getimage(String title,String message){

    NotificationCompat.Builder builder=new NotificationCompat.Builder(this,"mynotification")
            .setContentTitle(title)
            .setLargeIcon(BitmapFactory.decodeResource(this.getResources(),
                    R.drawable.otplogo
            ))
            .setSmallIcon(R.drawable.otplogo)
            .setAutoCancel(true)
            .setContentText(message);


    NotificationManagerCompat manager =NotificationManagerCompat.from(this);
    manager.notify(999,builder.build());

}
}
Kampai
  • 22,848
  • 21
  • 95
  • 95
Egarage e
  • 21
  • 6

2 Answers2

1

Unfortunately this was a limitation of Firebase Notifications in SDK 9.0.0-9.6.1. When the app is in the background the launcher icon is use from the manifest (with the requisite Android tinting) for messages sent from the console.

With SDK 9.8.0 however, you can override the default! In your AndroidManifest.xml you can set the following fields to customise the icon and color:

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

Note that if the app is in the foreground (or a data message is sent) you can completely use your own logic to customise the display. You can also always customise the icon if sending the message from the HTTP/XMPP APIs.

You have to put this tag inside Application tag of manifest

Official doc- https://firebase.google.com/docs/cloud-messaging/android/client

Thanks to this guy - https://stackoverflow.com/a/37332514/4741746

Sushant Gosavi
  • 3,647
  • 3
  • 35
  • 55
0

Bro its too simple but you have to do it carefully.

First, make a black & white icon for your notification. Now set it in your notification as a small icon like below.

mBuilder.setSmallIcon(R.mipmap.notification_icon);

now you can set a color but if I am right so you can set only predefined color instead of custom. For custom color you can try with your logic I am giving you a code of a predefined color.

 mBuilder.setColor(Color.GREEN);

It will make your notification icon Color green. Happy Coding!!!

UPDATE
Code for custom notification.

PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext,
            0 /* Request code */, resultIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    Uri soundUri = Uri.parse("android.resource://" + mContext.getApplicationContext()
            .getPackageName() + "/" + R.raw.sniper_gun);


    mBuilder = new NotificationCompat.Builder(mContext);
    mBuilder.setSmallIcon(R.mipmap.notification_icon);
    mBuilder.setSound(soundUri);
    if (image!=null) {
        mBuilder.setContentTitle(title)
                .setContentText(message)
                .setAutoCancel(false)
                .setLargeIcon(image)
                .setStyle(new NotificationCompat.BigPictureStyle()
                        .bigPicture(image).setSummaryText(message).bigLargeIcon(null))
                .setColor(Color.GREEN)
                .setContentIntent(resultPendingIntent);
    }
//        else {
//            mBuilder.setContentTitle(title)
//                    .setContentText(message)
//                    .setAutoCancel(false)
//                    .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
//                    .setContentIntent(resultPendingIntent);
//        }

    mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(Color.RED);
        notificationChannel.enableVibration(true);
        notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});

        // set custom soundUri
        if(soundUri != null){
            AudioAttributes audioAttributes = new AudioAttributes.Builder()
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .setUsage(AudioAttributes.USAGE_ALARM)
                    .build();
            notificationChannel.setSound(soundUri,audioAttributes);
        }

        assert mNotificationManager != null;
        mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
        mNotificationManager.createNotificationChannel(notificationChannel);
    }
    assert mNotificationManager != null;
    mNotificationManager.notify(0 /* Request Code */, mBuilder.build());
BlackBlind
  • 772
  • 9
  • 26