通知在 Activity 中有效,但在 Service 中无效
Posted
技术标签:
【中文标题】通知在 Activity 中有效,但在 Service 中无效【英文标题】:Notification works in Activity but not in Service 【发布时间】: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)
【参考方案1】:
代替
val mainPendingIntent = PendingIntent.getActivity(
applicationContext, BuildConfig.REQUEST_CODE,
Intent(applicationContext, MainActivity::class.java),
PendingIntent.FLAG_UPDATE_CURRENT
)
NotificationManagerCompat.from(applicationContext)
.notify(BuildConfig.NOTIFICATION_ID, builder.build())
我用:
val notificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
// Since android Oreo notification channel is needed.
setupChannels(notificationManager, builder)
// Create pending intent, mention the Activity which needs to be triggered
// when user clicks on notification.
val notificationId = Random.nextInt()
navigateToScreen(builder, notificationId)
val notification = builder.build()
notificationManager.notify(notificationId, notification)
private fun setupChannels(notificationManager: NotificationManager,
builder: NotificationCompat.Builder)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
val channelId = "channel_email_1"
val channel = NotificationChannel(channelId, "title",
NotificationManager.IMPORTANCE_DEFAULT).apply
description = "body"
// Add other settings.
lockscreenVisibility = Notification.VISIBILITY_PUBLIC
canShowBadge()
setShowBadge(true)
notificationManager.createNotificationChannel(channel)
builder.setChannelId(channelId)
private fun navigateToScreen(builder: NotificationCompat.Builder,
notificationId: Int)
val intent = Intent(this, MainActivity::class.java)
val pendingIntent = PendingIntent.getActivity(this, notificationId, intent,
PendingIntent.FLAG_UPDATE_CURRENT)
builder.setContentIntent(pendingIntent)
【讨论】:
以上是关于通知在 Activity 中有效,但在 Service 中无效的主要内容,如果未能解决你的问题,请参考以下文章
我的应用程序中的推送通知在 iPhone 6s 上停止工作,但在 iPad 上仍然有效