【问题标题】:why the notification doesn't show if I send using data message through firebase cloud function?如果我通过 Firebase 云功能使用数据消息发送,为什么不显示通知?
【发布时间】:2020-02-06 08:00:02
【问题描述】:

我像这样使用云功能发送 FCM

const payload = {
      data: {
         imagePath: imagePath,
         priority: priority,
         title: title,
         body: body
      }
 }

 await admin.messaging().sendToDevice(token,payload)

这是我的 Android 应用程序上的代码,在 onMessageReceived 中,下面的这个 onMessageReceived 代码实际上被调用了,但我不知道为什么通知没有显示在我的设备中

override fun onMessageReceived(remoteMessage: RemoteMessage) {
        super.onMessageReceived(remoteMessage)

        var title: String = ""
        var body: String = ""
        var priority: String = ""
        var imagePath: String = ""


        remoteMessage.notification?.let {
            title = it.title ?: ""
            body = it.body ?: ""
        }


        val notificationData = remoteMessage.data
        priority = notificationData["priority"] ?: ""
        imagePath = notificationData["imagePath"] ?: ""
        title = notificationData["title"] ?: ""
        body = notificationData["body"] ?: ""

        setUpNotification(title,body,priority,imagePath)
    }

并向设备显示通知

private fun setUpNotification(title: String, message: String, priority: String, imagePath: String) {


        val channel = if (priority == "high") NOTIFICATION_CHANNEL_IMPORTANT else NOTIFICATION_CHANNEL_MISCELLANEOUS

        var notification = NotificationCompat.Builder(this, channel)
            .setSmallIcon(R.drawable.ic_stat_ic_notification)
            .setContentTitle(title)
            .setContentText(message)
            .setCategory(NotificationCompat.CATEGORY_MESSAGE)
            .setAutoCancel(true)
            .setOnlyAlertOnce(true)
            .build()

        notificationManager.notify(1, notification)

    }

但是如果我像这样在有效负载上添加通知消息(不仅仅是数据消息)

            const payload = {
                notification: {
                    body: body,
                    title: title,
                },

                data: {
                    imagePath: imagePath,
                    priority: priority,
                    title: title,
                    body: body
                }
            }

然后通知将显示在我的设备上

【问题讨论】:

  • setUpNotification方法中传递静态随机数据检查这个方法是否完美工作。
  • @frankenstein 是的,它也被调用了,但我不知道为什么它不显示
  • 这个方法有问题
  • @Alexa289 你解决了吗?
  • @Black_Bacardi 是的,就我而言,问题根本不在于 FCM,而在于我在 Android 中创建频道的方式。你可以看到这个stackoverflow.com/questions/60105947/…

标签: android firebase google-cloud-functions firebase-cloud-messaging


【解决方案1】:

试试这个:

    private fun sendNotification(title: String, messageBody: String, notify_type: String) {
    var icon= BitmapFactory.decodeResource(resources,
            R.mipmap.ic_launcher);
    val intent = Intent("SplashActivity")// use your activity 
    intent.putExtra("type", "notification")// here i'm passing data onClick of notification
    intent.putExtra("title", notify_type)
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
    val r = Random()
    val i1 = r.nextInt(20 - 0 + 1) + 0
    val pendingIntent = PendingIntent.getActivity(this,i1,intent,PendingIntent.FLAG_UPDATE_CURRENT)
    val channelId = this.resources.getString(R.string.default_notification_channel_id)
    val builder = NotificationCompat.Builder(this, channelId)
            .setSmallIcon(R.drawable.ic_appicon)
            .setContentTitle(title)
            .setContentText(messageBody).setAutoCancel(true).setContentIntent(pendingIntent)
    val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val channel = NotificationChannel(channelId, "Default channel",NotificationManager.IMPORTANCE_HIGH)
        manager.createNotificationChannel(channel)
    }
    manager.notify(i1,builder.build())
}

【讨论】:

    【解决方案2】:

    根据FCM documentation,有两种类型的消息:通知消息和数据消息。

    当您将Notification 部分添加到您的按摩时,您正在将按摩类型从数据类型更改为通知类型。 在同一文档中,您可以找到通知类型“自动向最终用户设备显示消息”。

    我认为当您添加 Notification 部分时,此机制会处理显示通知。您可以搜索解决方案是您的客户端应用程序,因为问题很可能就在那里。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-18
      • 2019-02-23
      • 1970-01-01
      • 2018-06-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-04
      • 2017-12-31
      相关资源
      最近更新 更多