我能否将Android默认的推送通知图标从应用程序图标改为自定义图标?
我正在使用默认的Firebase实现在系统托盘中显示通知,当推送通知到来时。由于我的应用程序图标是彩色的且有渐变效果,因此当通知到来时,Android会尝试制作其黑白版本,并使其看起来像一个白色的圆形。
是否有一种方式可以更新默认通知图标以使用其他东西而不是默认应用程序图标?
PS:期望解决方案需要在清单中进行配置更改,并且不想使用NotificationCompat.Builder。
我能否将Android默认的推送通知图标从应用程序图标改为自定义图标?
我正在使用默认的Firebase实现在系统托盘中显示通知,当推送通知到来时。由于我的应用程序图标是彩色的且有渐变效果,因此当通知到来时,Android会尝试制作其黑白版本,并使其看起来像一个白色的圆形。
是否有一种方式可以更新默认通知图标以使用其他东西而不是默认应用程序图标?
PS:期望解决方案需要在清单中进行配置更改,并且不想使用NotificationCompat.Builder。
你可以根据https://firebase.google.com/docs/cloud-messaging/android/receive中的说明,在你的清单文件中使用它。
你可以按照上述链接中Firebase文档的指示,在你的清单文件中使用它。 <!-- 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" />
希望能帮助到大家。
.setSmallIcon(int);
.setLargeIcon(bitmap);
setSmallIcon()是用来设置一个小图标的方法。
NotificationCompat.Builder setLargeIcon (Bitmap icon)
在通知栏和通知中显示大图标。
使用NotificationCompat.Builder设置。
文档 - https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.custom_icon)
.setContentTitle("FCM Message")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
int icon=R.drwable.icon; //Add your icon name here
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(icon);
Builder
类用于创建Notification
对象。它提供了一种方便的方式来设置通知的各个字段,并使用平台的通知布局模板生成内容视图。请查看官方文档here。
Notification noti = new Notification.Builder(mContext)
.setContentTitle("New mail from " + sender.toString())
.setContentText(subject)
.setSmallIcon(R.drawable.new_mail)
.setLargeIcon(aBitmap)
.build();
在这里,您可以设置自定义字段,包括通知图标。
祝您编程愉快!
是的,你可以做到。如果你想避免白色圆圈图标,你必须使你的图标透明并添加背景颜色。这样,你使用的颜色会通过透明的图标显示出来,使图标可见且有色彩。请在下面的示例代码中检查setSmallIcon和setColor。查看此文档并搜索“透明”
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);
对我有效的方法是:
更改清单文件中的图标标签
android:icon="@drawable/ic_icon"
我正在使用 AndroidX Media3。
NotificationCompat.Builder
çš„setSmallIcon
ć–ąćł•äĽ é€’ä¸€ä¸Şid
参数。 - Dhruv