【问题标题】:Android Notification : Data & MessageAndroid 通知:数据和消息
【发布时间】:2021-07-15 08:46:33
【问题描述】:

我的 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 时,通知没有到达。

如果你有线索,我很感兴趣。 基本思想是,当用户点击通知(包含事件标题)时,应用程序会打开与事件详细信息相匹配的 Activity。

感谢您的帮助

[编辑] 这是我的服务中的 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 firebase kotlin firebase-cloud-messaging


    【解决方案1】:

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

    您的代码中的某处必须有一个扩展您在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

    编辑: 在来自 Oreo 的最新版本的 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)
            }
    

    【讨论】:

    • 您好,感谢您的帮助。我已经在使用扩展FirebaseMessagingService 的自定义服务。我会编辑我的原始帖子给你看;)
    • 你是说这个onMessageReceived 函数没有被调用?只有当应用程序在前台时才应该调用它。 stackoverflow.com/questions/37358462/…
    • 这个函数被调用。日志很好,数据正确。但不显示通知。而且我不明白为什么......
    • 我已经编辑了我的答案,如果可行,请告诉我
    • 好的,不确定是否理解notificationManager.createNotification 行。你还没有宣布它。我添加了它,它似乎工作。我会再次编辑我的帖子。谢谢!
    猜你喜欢
    • 2017-02-23
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-15
    • 2017-08-21
    • 2016-07-24
    相关资源
    最近更新 更多