我能否将Android中默认的推送通知图标从应用程序图标更改为自定义图标?

25

我能否将Android默认的推送通知图标从应用程序图标改为自定义图标?

我正在使用默认的Firebase实现在系统托盘中显示通知,当推送通知到来时。由于我的应用程序图标是彩色的且有渐变效果,因此当通知到来时,Android会尝试制作其黑白版本,并使其看起来像一个白色的圆形。

是否有一种方式可以更新默认通知图标以使用其他东西而不是默认应用程序图标?

PS:期望解决方案需要在清单中进行配置更改,并且不想使用NotificationCompat.Builder。


你可以给NotificationCompat.Builder的setSmallIcon方法传递一个id参数。 - Dhruv
7个回答

36

你可以根据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" />

希望能帮助到大家。


这些信息不完整,你贴的链接也是。 - Becario Senior
@BecarioSenior 我没有遇到任何错误。请参考Firebase文档。 - elsennov
4
很惊讶我得通过谷歌搜索才找到这个。感谢你的帮助和参考资料。 - Alan

3

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

0
你可以这样做:
int icon=R.drwable.icon; //Add your icon name here
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(icon);

有没有一种方法可以不使用Notification.Builder来完成。比如说默认图标是从清单中获取的,它等同于应用程序图标。是否可以在配置级别上完成,以便适用于所有通知? - Shashank
你想生成自定义通知吗? - Dhruv
他正在使用默认的Firebase通知,因此无法访问通知构建器。 - EdgarK

0

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();

在这里,您可以设置自定义字段,包括通知图标。

祝您编程愉快!


0

是的,你可以做到。如果你想避免白色圆圈图标,你必须使你的图标透明并添加背景颜色。这样,你使用的颜色会通过透明的图标显示出来,使图标可见且有色彩。请在下面的示例代码中检查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);

0

对我有效的方法是:

更改清单文件中的图标标签

        android:icon="@drawable/ic_icon"

我正在使用 AndroidX Media3。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接