安卓通知:数据和消息



我的Android通知(通过FCM发送(有问题。让我解释一下。

我可以发送";经典的";显示在通知栏中的通知。我可以发送带有个性化数据的通知,但它们不会显示在通知栏中。

然而,我不能两者都做。目前,我发送的有效载荷如下:

{
"to":"token",
"message":{
"notification":{
"body":"Body",
"title":"Title"
}
},
"data":{
"id":2543,
"type":"EVENT"
"title":"Title"
"body":"Body"
}
}

它确实发送数据通知。但它不会显示在手机上。在我的服务中,我有我的onMessageReceived功能,其中包括:

val id = remoteMessage.data["id"]
val type = remoteMessage.data["type"]

只要我的通知进入数据模式,我就可以获得这些值。

所以,我认为显示通知和数据通知之间有一个我不理解的概念。

我使用了此链接:https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages

当我将data数组放入message时,通知不会到达。

如果你有线索,我很感兴趣。其基本思想是,当用户单击通知(其中包含事件的标题(时,应用程序会打开与事件详细信息一致的活动。

感谢您的帮助

[编辑]这是我的服务中的onMessageReceived(remoteMessage: RemoteMessage)函数,它扩展了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]有了新的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())

在Android上处理推送通知确实有点痛苦。解决问题的最简单方法是自己创建通知。

在代码的某个地方,必须有一个Service类来扩展您在AndroidManifest.xml中声明的FirebaseMessagingService。它有一个onMessageReceived(message: RemoteMessage)方法。

你可以像一样获得消息的内容

override fun onMessageReceived(message: RemoteMessage) {
super.onMessageReceived(message)
val remoteNotification = message.notification ?: return
val title : String = remoteNotification.title
val body : String = remoteNotification.body

然后你只需要通过创建通知来自己显示它,你可以在这里找到关于创建通知的更多信息:

https://developer.android.com/training/notify-user/build-notification

编辑:在奥利奥最新版本的Android上,你需要指定频道,你做到了。但是你忘了创建频道。还要确保您支持旧版本的Android。像这样:


var builder: NotificationCompat.Builder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val importance = NotificationManager.IMPORTANCE_DEFAULT
val notificationChannel = NotificationChannel("mainChannelId", "Main", importance)
notificationManager.createNotificationChannel(notificationChannel)
NotificationCompat.Builder(applicationContext, notificationChannel.id)
} else {
NotificationCompat.Builder(applicationContext)
}

相关内容

  • 没有找到相关文章

最新更新