Bonjour,
J’ai un problème concernant mes notifications Android (envoyées via FCM). Je m’explique.
J’arrive à envoyer des notifications « classiques » qui s’affichent dans la barre de notifications. J’arrive à envoyer des notifications avec des données personnalisées mais qui ne s’affichent pas dans la barre de notifications.
Par contre, je n’arrive pas à faire les deux. Actuellement, le payload que j’envoie ressemble à ceci :
{
"to":"token",
"message":{
"notification":{
"body":"Test",
"title":"Test"
}
},
"data":{
"id":2543,
"type":"EVENT"
"title":"Title"
"body":"Body"
}
}
Cela envoie bien la notifications de données. Mais elle ne s’affiche pas sur le téléphone.
Dans mon service j’ai ma fonction onMessageReceived
avec, entre autres, ceci :
val id = remoteMessage.data["id"]
val type = remoteMessage.data["type"]
Tant que ma notification part en mode « données », j’arrive à récupérer ces valeurs.
Du coup, je me dis qu’il y a un concept entre les notifications d’affichage et les notifications de données que je n’ai pas compris.
Je me suis basé sur ce lien : About FCM messages | Firebase
Et quand je met mon tableau data
dans message
la notification n’arrive pas.
Si vous avez une piste, je suis preneur.
L’idée de base est que quand l’utilisateur clique sur la notification envoyée (qui contient le titre d’un événement), l’application ouvre l’activity qui va bien avec le détail de l’événement.
Merci de votre aide
[EDIT] Voici ma fonction onMessageReceived(remoteMessage: remoteMessage)
qui se trouve dans mon service qui 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())
}
Actuellement, je log bien toute les données que je demande. Mais impossible de faire apparaître la notification à l’utilisateur…
[EDIT2] Voici mon nouveau NotificationBuilder
pour tenir compte des évolutions d’Android
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())
La, la notification s’affiche. Mais le pendingIntent
ne semble pas fonctionner …
[EDIT3] Ok cette fois ça semble bon. C’est parce que dans mon DO SOMETHING TO RETRIEVE THE EVENT
j’étais en asynchrone et donc mon intent
par défaut était envoyé et non celui avec l’évenement !