android - 推送通知 Kotlin
问题描述
我尝试使用notificationBuilder
and创建通知notificationManager
。一切似乎都很好,但是当应用程序处于后台或前台时,通知都不会出现。
你能帮我吗?这是到目前为止的代码:
class MyFirebaseMessagingService : FirebaseMessagingService() {
override fun onMessageReceived(remoteMessage: RemoteMessage) {
Log.i("info", "Message received: ${remoteMessage.from}")
Log.i("info", remoteMessage.data.toString())
if (remoteMessage.data != null) {
showNotification(remoteMessage.data.get("title"), remoteMessage.data.get("text"))
}
}
private fun showNotification(title: String?, body: String?) {
Log.i("info", "show notification $title $body")
val intent = Intent(this, MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
val pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT)
val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notificationBuilder = NotificationCompat.Builder(this)
.setContentText(body)
.setAutoCancel(true)
.setSmallIcon(R.mipmap.ic_launcher)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build())
}
}
解决方案
推荐阅读
- react-native - 基于 PassportJS cookie 的身份验证与 React Native 应用程序集成
- python - Python 排序的 lambda 函数返回一个布尔值
- database - 如何为嵌套数据设计模式
- python - 比较两个列表中 str 之间的出现次数
- c - 带/不带功能的动态内存分配
- python - 排除列表中的元素
- javascript - 开玩笑:TypeError:axios.get.mockResolvedValue 不是函数
- c# - 在 Autofac C# Resolve 中,如何将参数传递给构造函数
- node.js - Firestore 导入数据文件未正确保存(ä、ö、ü、á 显示为符号)
- javascript - Firebase Scheduled Cloud Function 执行时如何获取时间?