-2

I'm setting up Firebase cloud messaging in my app every thing is working fine except the notification icon which is appearing white square whenever I receive the notification.

I have tried to change the builder.setSmallIcon to other icons as well I have added the resource icon for firebase messaging default_notification_icon in AndroidManifest.xml

FcmMessagingService.java

private static int VIBRATION_TIME = 500; // in millisecond
private SharedPref sharedPref;

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    sharedPref = new SharedPref(this);
    if (sharedPref.getNotification()) {
        // play vibration
        if (sharedPref.getVibration()) {
            ((Vibrator) getSystemService(Context.VIBRATOR_SERVICE)).vibrate(VIBRATION_TIME);
        }
        RingtoneManager.getRingtone(this, Uri.parse(sharedPref.getRingtone())).play();

        if (remoteMessage.getData().size() > 0) {
            Map<String, String> data = remoteMessage.getData();
            FcmNotif fcmNotif = new FcmNotif();
            fcmNotif.setTitle(data.get("title"));
            fcmNotif.setContent(data.get("content"));
            fcmNotif.setPost_id(Integer.parseInt(data.get("post_id")));
            displayNotificationIntent(fcmNotif);
        }
    }
}

private void displayNotificationIntent(FcmNotif fcmNotif) {
    Intent intent = new Intent(this, ActivitySplash.class);
    if (fcmNotif.getPost_id() != -1) {
        intent = new Intent(this, ActivityPostDetails.class);
        Post post = new Post();
        post.title = fcmNotif.getTitle();
        post.id = fcmNotif.getPost_id();
        boolean from_notif = !ActivityMain.active;
        intent.putExtra(ActivityPostDetails.EXTRA_OBJC, post);
        intent.putExtra(ActivityPostDetails.EXTRA_NOTIF, from_notif);
    }
    PendingIntent pendingIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);

    Notification.Builder builder = new Notification.Builder(this);
    builder.setContentTitle(fcmNotif.getTitle());
    builder.setSmallIcon(R.drawable.ic_notification2);
    //builder.setSmallIcon(R.drawable.splash_icon);
    //builder.setSmallIcon(R.mipmap.ic_launcher);
    builder.setLargeIcon(BitmapFactory.decodeResource(this.getResources(),
         R.mipmap.ic_launcher));
    builder.setStyle(new Notification.BigTextStyle().bigText(fcmNotif.getContent()));
    builder.setContentText(fcmNotif.getContent());
    builder.setDefaults(Notification.DEFAULT_LIGHTS);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        builder.setPriority(Notification.PRIORITY_HIGH);
    }
    builder.setContentIntent(pendingIntent);
    builder.setAutoCancel(true);
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    int unique_id = (int) System.currentTimeMillis();
    notificationManager.notify(unique_id, builder.build());
}

Output

Zoe
  • 27,060
  • 21
  • 118
  • 148
  • Refer this link https://stackoverflow.com/a/30795471/2919483, For push notifications , till 5.0 version notification icon shows as we uses, but after 5.0 you have to set transparent notification icon for push notification. When you set icon there you need to apply condition for version check, in the link you will get proper answer as you want. as well as keep your notification icon in all respective folders like - hdpi, mdpi, xhdpi, xxhdpi, xxxhdpi. Do not put your notification icon inside drawable folder. – Rajshree Tiwari Apr 15 '19 at 04:30
  • Genrate your notification icon from following link - http://romannurik.github.io/AndroidAssetStudio/icons-notification.html#source.type=clipart&source.clipart=ac_unit&source.space.trim=1&source.space.pad=0&name=ic_stat_ac_unit – Rajshree Tiwari Apr 15 '19 at 04:33

2 Answers2

1

You need to call builder.setSmallIcon(getNotificationIcon()) and make sure icon background should be transparent i.e R.mipmap.transparent, If your Build.VERSION.SDK_INT is LOLLIPOP or above Build.VERSION_CODES.LOLLIPOP, else can use your application icon

 private int getNotificationIcon()
 {
    boolean useWhiteIcon = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP);
    return useWhiteIcon ? R.mipmap.transparent: R.mipmap.appicon;
 }
ॐ Rakesh Kumar
  • 1,318
  • 1
  • 14
  • 24
0
 Firebase 9.8.0 it is possible to change this icon, by adding info about this in Manifest:

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

refer this

MurugananthamS
  • 2,395
  • 4
  • 20
  • 49