【问题标题】:Unable to change default icon for Firebase Cloud Messaging notifications sent from Firebase console无法更改从 Firebase 控制台发送的 Firebase 云消息传递通知的默认图标
【发布时间】:2018-09-24 17:57:49
【问题描述】:
所以我在我的 Qt 应用程序中集成了 Firebase Cloud Messaging,我正在尝试为通知设置一个自定义图标(默认情况下显示一个灰色圆圈)。我知道在Notification Icon with the new Firebase Cloud Messaging system 之前已经在这里问过这个问题。现在我遵循了帖子(和其他类似帖子)中的建议。在我的清单文件中,我添加了:
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_stat_newspaper" />
然后,我根据 Google 提供的自定义状态栏图标规范(透明背景上的白色)创建了不同的图标。我的目标 SDK 设置为 26。尽管如此,通知始终显示默认图标而不是我的自定义图标。我正在从 Firebase 控制台网站发送通知。我知道有些用户建议使用 API 发送通知,因为它似乎解决了这个问题,但是是否有可能避免这种情况并通过 Firebase 控制台成功?
【问题讨论】:
-
"是否有可能通过 Firebase 控制台来避免这种情况并设法成功" - 在docs 中这可以工作。可能还有其他原因导致应用忽略您的图标。您确定该图标符合通知图标的准则吗?
-
标签:
android
firebase
notifications
firebase-cloud-messaging
【解决方案1】:
好的,我找到了解决问题的方法。问题是我在 Android 8.0 Oreo 上测试应用程序。从这个版本的 Android 开始,Google 要求应用程序使用Notification Channels,并且应用程序需要创建一个默认的通知通道。所以我在onCreate 方法中将以下代码添加到我的应用程序的主要活动中:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Create channel to show notifications.
String channelId = getString(R.string.default_notification_channel_id);
String channelName = getString(R.string.default_notification_channel_name);
NotificationManager notificationManager =
getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(new NotificationChannel(channelId,
channelName, NotificationManager.IMPORTANCE_LOW));
}
然后在manifest文件中添加默认通知id:
<meta-data android:name="com.google.firebase.messaging.default_notification_channel_id" android:value="@string/default_notification_channel_id"/>