![](/img/trans.png)
[英]Android app cannot handle Firebase push notifications when in background
[英]Firebase push notifications show wrong icon when app in background
我在我的应用程序中使用来自服务器的 Firebase 推送通知。 当应用程序处于前台时,会显示正确的通知图标。 但是当应用程序在后台时,会有一个实心圆圈而不是通知图标。
我已经查看了类似的问题,并且我在清单中使用了正确的值。 这是我的清单中的内容:
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_stat_name" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/colorPrimary" />
通知颜色值似乎工作正常。 实心圆圈是我的应用程序的原色,如果我将其更改为某个随机 RGB 值,它会更新。 但无论如何,通知图标始终是一个实心圆圈。
以下是我在前台处理通知的方式,但可以正常工作:
private fun showNotification(notification: RemoteMessage) {
createNotificationChannel()
val notificationId = notification.data["notificationId"]
val feedItemId = notification.data["itemId"]
val feedType = notification.data["type"]
val intent = Intent(this, MainActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP
putExtra(EXTRA_NOTIFICATION, true)
putExtra(EXTRA_NOTIFICATION_ID, notificationId)
putExtra(EXTRA_NOTIFICATION_FEED_ITEM_ID, feedItemId)
putExtra(EXTRA_NOTIFICATION_FEED_TYPE, feedType)
}
val pendingIntent = PendingIntent.getActivity(this, (0..1000).random(), intent, PendingIntent.FLAG_ONE_SHOT)
val builder = NotificationCompat.Builder(this, getString(R.string.notification_channel_id))
.setSmallIcon(R.drawable.ic_stat_name)
.setContentTitle(notification.notification?.title ?: "")
.setContentText(notification.notification?.body ?: "")
.setStyle(NotificationCompat.BigTextStyle()
.bigText(notification.notification?.body ?: ""))
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
with(NotificationManagerCompat.from(this)) {
notify(System.currentTimeMillis().toInt(), builder.build())
}
}
所以通知图标不是问题(如类似问题中所建议的那样),因为当应用程序处于前台时它会正确显示。
我遇到了这个确切的问题,对我来说解决的方法是将其添加到AndroidManifest.xml
中。
<meta-data android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_notification" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.