Can I override default push notification icon in a

2019-04-04 17:27发布

问题:

Can I override default push notification icon in android from app icon to custom icon?

I am using default firebase implementation to display notification in system tray, when a push notification come. Since my app icon is coloured and has gradient, so when a notification comes, android tried to make a black/white version of it, and make it look like a white circle.

Is there a way to update the default notification icon to something else instead of default app icon?

PS: Looking forward for a solution which required a config change in manifest and don't want to use NotificationCompat.Builder

回答1:

you can use this in your manifest according to https://firebase.google.com/docs/cloud-messaging/android/receive

    <!-- Set custom default icon. This is used when no icon is set for incoming notification
    messages.See README(https://github.com/firebase/quickstart-android/tree/master/messaging#custom-default-icon) for more. -->
    <meta-data
        android:name="com.google.firebase.messaging.default_notification_icon"
        android:resource="@drawable/ic_stat_ic_notification" />

    <!-- Set color used with incoming notification messages.
    This is used when no color is set for the incoming notification message. See README
    (https://github.com/firebase/quickstart-android/tree/master/messaging#custom-default-color) for more. -->
    <meta-data
        android:name="com.google.firebase.messaging.default_notification_color"
        android:resource="@color/colorAccent" />

Hope it helps people.



回答2:

You can set

  .setSmallIcon(int);
  .setLargeIcon(bitmap);

A small icon, set by setSmallIcon()

NotificationCompat.Builder setLargeIcon (Bitmap icon)

Set the large icon that is shown in the ticker and notification.

in NotificationCompat.Builder.

Documentation - https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html



回答3:

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.custom_icon)
                .setContentTitle("FCM Message")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);


回答4:

Builder class for Notification objects. Provides a convenient way to set the various fields of a Notification and generate content views using the platform's notification layout template. Check Official Documentation here

 Notification noti = new Notification.Builder(mContext)
     .setContentTitle("New mail from " + sender.toString())
     .setContentText(subject)
     .setSmallIcon(R.drawable.new_mail)
     .setLargeIcon(aBitmap)
     .build();

Here you can set custom fields including Icon for your notifications.

Cheers! Happy Coding



回答5:

You can do like this:

int icon=R.drwable.icon; //Add your icon name here
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(icon);


回答6:

Yes you can do it.If you want to avoid white circle icon you must make your icon transparent and add background color to it.So the color you use appears out through the transparent icon and makes the icon visible and colorful. please check for setSmallIcon and setColor in the below sample code.Check this doc and search for transparent

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                context)
                .setContentIntent(contentIntent)
                .setSmallIcon(R.drawable.transaparentIcon)
                .setColor(context.getResources().getColor(R.color.notif_color))
                .setWhen(System.currentTimeMillis())
                .setContentTitle(context.getString(R.string.app_name))
                .setStyle(
                        new NotificationCompat.BigTextStyle()
                                .bigText(notificationText))
                .setContentText(notificationText)
                .setAutoCancel(true);