Notifications are showing in the background but when the app is in the foreground notifications are not showing. I applied many solutions but they do not work for me. Can anyone tell me where is my mistake? thanks in advance
Here is Manifest
<service
android:exported="false"
android:name=".services.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/cute" />
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="@color/design_default_color_on_primary" />
Here is My ServicesClass
class MyFirebaseMessagingService : FirebaseMessagingService() {
override fun onMessageReceived(remoteMessage: RemoteMessage) {
sendNotification(remoteMessage.notification?.title, remoteMessage.notification?.body)
//just for testing
Toast.makeText(applicationContext, "${remoteMessage.notification?.title}", Toast.LENGTH_LONG).show()
}
private fun sendNotification(messageTitle: String?, messageBody: String?) {
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 pattern = longArrayOf(500, 500, 500, 500, 500)
val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notificationBuilder:NotificationCompat.Builder = NotificationCompat.Builder(applicationContext)
.setSmallIcon(com.dextrologix.dham.rfms.resident.R.drawable.cute)
.setContentTitle(messageTitle)
.setContentText(messageBody)
.setAutoCancel(true)
.setVibrate(pattern)
.setLights(Color.BLUE, 1, 1)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent) as NotificationCompat.Builder
val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(0, notificationBuilder.build())
}
}
It seems like you are missing notification channel. They are required for android notifications. docs link
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.