android - Fcm Notification not triggered in background or when device is rebooted
问题描述
I'm implementing FCM in my application , so basically i have some data that i send using data-payload , onMessageReceived() is successfully triggered when app is on foreground , but when i rebooted my device for example , notification is not triggered , i tried all possible ways to solve this issue but no luck , Help is appreciated guys , thank you .
- This is my data - payload
val notificationData = JSONObject()
val payload = JSONObject()
notificationData.put("to", "/topics/$userToken")
payload.put("title",title)
payload.put("body", body)
payload.put("notificationVibrate",isVibrationEnabled)
payload.put("notification_channel_id",getString(R.string.channelid))
notificationData.put("data", payload)
- This is how i'm receiving data in my firebaseMessagingService
override fun onMessageReceived(remotemessage: RemoteMessage) {
super.onMessageReceived(remotemessage)
if(remotemessage.data.isNotEmpty()){
val data = remotemessage.data
val title = data["title"]
val body = data["body"]
val isVibrationEnabled = data["notificationVibrate"]?.toBoolean()
val notificationChannelId = data["notification_channel_id"]
FireNotification(title!!,body!!,isVibrationEnabled!!,notificationChannelId!!)
}
}
- This is the function where i fire the notification
private fun FireNotification(title : String , body : String,isVibrationEnabled : Boolean,notificationchannelid : String){
val vibratePattern = longArrayOf(500L,500L,500L)
val notification = NotificationCompat.Builder(applicationContext, notificationchannelid)
notification.setContentTitle(body)
notification.setContentText(title)
notification.setSmallIcon(R.drawable.ic_baseline_notifications_active_24)
notification.setAutoCancel(true)
if(isVibrationEnabled){
notification.setVibrate(vibratePattern)
}
notificatomCompat.notify(1001, notification.build())
} ```