Я пытаюсь создать уведомление с notificationBuilder
и 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())
}
}