代码之家  ›  专栏  ›  技术社区  ›  Emanuel

Android FCM推送通道Id始终为空

  •  0
  • Emanuel  · 技术社区  · 4 年前

    我有一个从未被触发的自定义通知通道。我阅读了服务器和客户端的所有文档,但仍然缺少一些东西。

    我想要什么

    我想通过FCM发送高优先级推送通知,以最终唤醒应用程序中的前台服务。因此,我定义了一个自定义通知通道。我想接收推送,找出它已发送到自定义频道,并显示通知。

    我的问题

    我按预期从firebase接收所有推送通知,但channelId始终为null。

    设置

    我们直接在服务器和android应用程序的两侧使用FCM。 我们的用户使用Android 8.1及更高版本。不需要传统推送。

    后端发送的JSON如下所示:

    {
        "notification": {
            "android_channel_id": "MY_High_Prio_Push_Channel"
        },
        "data": {
            "notificationBody": "BodyText",
            "notificationTitle": "Title"
        },
        "priority": "high"
    }
    

    应用程序已成功注册推送频道。我可以在设备设置中看到它。推送频道将在任何应用程序启动时重新注册,如下所示

    private fun registerHighPrioPushToRestartTheForegroundService() {
        val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        val channel = NotificationChannel(getString(R.string.push_channel_id_high_prio), getString(R.string.push_channgel_sync_with_high_prio), NotificationManager.IMPORTANCE_HIGH)
        channel.setShowBadge(true)
        notificationManager.createNotificationChannel(channel)
    }
    

    在运行时

    如果android设备接收到来自FCM的推送,则调用onNewMessageReceived()。然后,我通过拨打以下电话检查发送给哪个channelId:

    if (remoteMessage.notification?.channelId == getString(R.string.push_channel_id_high_prio)) {
        // I would love to do my channel specific stuff here
    }
    

    正如您在下面的屏幕截图中所看到的,正文包含完整的JSON。所有其他值均为空。

    enter image description here

    0 回复  |  直到 4 年前
        1
  •  0
  •   Emanuel    4 年前

    问题出在服务器端。推送的整个JSON都放在通知的body标记中。现在这个问题已经解决了,我们发送一个JSON,如下所示,客户端中的解析工作正常。我们删除了通知标签,以便 onMessageReceived() 将在每次推送时调用。

    {
        "to": "{devicePushToken}",
        "android": {
            "priority": "high"
        },
        "priority": 10,
        "data": {
            "title": "Some Title",
            "body": "Some Message",
            "android_channel_id": "yourUniquePushId"
        }
    }