I have a problem with my Android notifications (sent via FCM). Let me explain.
I can send "classic" notifications which are displayed in the notification bar. I can send notifications with personalized data but they are not displayed in the notification bar.
However, I can't do both. Currently, the payload I send looks like this:
{
"to":"token",
"message":{
"notification":{
"body":"Body",
"title":"Title"
}
},
"data":{
"id":2543,
"type":"EVENT"
"title":"Title"
"body":"Body"
}
}
It does send the data notification. But it doesn't display on the phone. In my service I have my onMessageReceived function with, among other things, this:
val id = remoteMessage.data["id"]
val type = remoteMessage.data["type"]
As long as my notification goes to data mode, I can get these values.
So, I think there is a concept between display notifications and data notifications that I don't understand.
I used this link: https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages
And when I put my data
array in message
the notification does not arrive.
If you have a clue, I'm interested. The basic idea is that when the user clicks on the notification (which contains the title of an event), the application opens the activity that goes well with the details of the event.
Thanks for your help
[EDIT]
This is my onMessageReceived(remoteMessage: RemoteMessage)
function in my service that extends FirebaseMessagingService
override fun onMessageReceived(remoteMessage: RemoteMessage) {
super.onMessageReceived(remoteMessage)
val intent:Intent?
val id = remoteMessage.data["id"]
val type = remoteMessage.data["type"]
val title = remoteMessage.data["title"]
val body = remoteMessage.data["body"]
Log.i("YANN", "RemoteMessage Data : " + remoteMessage.data)
Log.i("YANN", "Id : $id")
Log.i("YANN", "Type : $type")
Log.i("YANN", "Title : $title")
Log.i("YANN", "Body : $body")
when (type) {
"NOTIFICATION" -> {
// TODO : Load the NotificationFragment
intent = Intent(this, MainActivity::class.java)
}
"POST" -> {
var post:Post = Post(0, "", "", "", "", "")
// DO SOMETHING TO RETRIVE THE POST
intent = Intent(this, PostActivity::class.java)
intent.putExtra("post", post)
}
"EVENT" -> {
var event = Event(0, "", "", "", "", "", "", "")
// DO SOMETHING TO RETRIEVE THE EVENT
intent = Intent(this, EventActivity::class.java)
intent.putExtra("event", event)
}
else -> {
intent = Intent(this, MainActivity::class.java)
}
}
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT)
val soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val notification = NotificationCompat.Builder(this, resources.getString(R.string.default_notification_channel_id))
.setSmallIcon(R.drawable.ic_dashboard_black_24dp)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setSound(soundUri)
.setContentIntent(pendingIntent)
NotificationManagerCompat.from(this).notify(1, notification.build())
}
[EDIT2] There is the new NotificationBuilder
val builder: NotificationCompat.Builder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val importance = NotificationManager.IMPORTANCE_DEFAULT
val notificationChannel = NotificationChannel(resources.getString(R.string.default_notification_channel_id), resources.getString(R.string.default_notification_channel_id), importance)
val notificationManager:NotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(notificationChannel)
NotificationCompat.Builder(this, notificationChannel.id)
} else {
NotificationCompat.Builder(this)
}
builder.setSmallIcon(R.drawable.ic_dashboard_black_24dp)
builder.setContentTitle(title)
builder.setContentText(body)
builder.setAutoCancel(true)
builder.setSound(soundUri)
builder.setContentIntent(pendingIntent)
val notificationID = (0..999999999).random()
NotificationManagerCompat.from(this).notify(notificationID, builder.build())