【发布时间】:2021-04-09 18:08:40
【问题描述】:
我正在尝试将 Firebase 云消息集成到我的应用中。我在showNotification 函数中使用的代码来自Android User Interface Samples。我在 Activity 中尝试过它并且它有效,但不确定为什么它在服务中不起作用。 println 显示函数正在被调用并且值按预期出现。该功能是否缺少任何内容?
class MessagingService : FirebaseMessagingService() {
override fun onMessageReceived(remoteMessage: RemoteMessage) {
if (remoteMessage.data.isNotEmpty()) {
val message = Gson().fromJson(remoteMessage.data.toString(), Message.Res::class.java).payload!!
showNotification(message.title, message.body, message.count)
}
}
private fun showNotification(title: String?, body: String?, count: Int = 0) {
println("_print::showNotification(title:$title, body:$body, count:$count)")
val mainPendingIntent = PendingIntent.getActivity(
applicationContext, BuildConfig.REQUEST_CODE,
Intent(applicationContext, MainActivity::class.java),
PendingIntent.FLAG_UPDATE_CURRENT
)
val builder = NotificationCompat.Builder(applicationContext, "channel_email_1")
.setContentTitle(title)
.setContentText(body)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher_round))
.setContentIntent(mainPendingIntent)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setColor(ContextCompat.getColor(applicationContext, R.color.primary))
.setSubText(if (count > 1) "You have $count pending notifications" else title)
.setCategory(Notification.CATEGORY_EMAIL)
.setPriority(1)
.setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
NotificationManagerCompat.from(applicationContext)
.notify(BuildConfig.NOTIFICATION_ID, builder.build())
}
}
【问题讨论】:
-
您如何在服务中收到它,什么不起作用?
-
_print::showNotification(title:Card transfer Request, body:Your request to transfer your eToll Card to Probase has been received. Ref No. BNZ160970, count1)
标签: android firebase firebase-cloud-messaging android-notifications firebase-notifications