【问题标题】:Firebase Messaging - Can't receive notifications from subscriptionsFirebase 消息 - 无法接收来自订阅的通知
【发布时间】:2017-11-14 10:28:38
【问题描述】:

情况如下:我有一个 firebase 云函数,它在每次写入某个数据库集合(称为“jamrooms”)时运行。 NodeJS脚本如下:

const functions = require('firebase-functions');

// The Firebase Admin SDK to access the Firebase Realtime Database.
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.newJamroom = functions.database.ref('/jamrooms/{jamroomId}').onWrite(event => {

        // Grab the current value of what was written to the Realtime Database.
        var jamroomId = event.params.jamroomId;

        var topic = "new-jamroom";
        var payload = {
            data: {
                title: "New jamroom available !",
                body: String("Jamroom id = ").concat(jamroomId)
            }
        };

        // Send a message to devices subscribed to the provided topic.
        return admin.messaging().sendToTopic(topic, payload)
            .then(function (response) {
                // See the MessagingTopicResponse reference documentation for the
                // contents of response.
                console.log("Successfully sent message:", response);
            })
            .catch(function (error) {
                console.log("Error sending message:", error);
            });
});

在客户端(Android)上,我订阅了“new-jamroom”主题:

FirebaseMessaging.getInstance().subscribeToTopic("new-jamroom");

每次向集合中添加新的键值对时,脚本都会成功执行:

但客户端永远不会在后台或前台收到通知。

我不知道从哪里看才能了解哪里出了问题。

更新

即使从控制台发送通知(使用控制台中存在的主题“new-jamroom”)也不会将其发送到客户端(发送了 Firebase 记录 0)。

【问题讨论】:

    标签: android firebase firebase-cloud-messaging


    【解决方案1】:

    由于您的负载包含 data 键,因此您发送的是仅数据消息,而不是通知:

        var payload = {
            data: {
                title: "New jamroom available !",
                body: String("Jamroom id = ").concat(jamroomId)
            }
    

    数据消息和通知消息由接收方handled differently。纯数据消息导致在接收器中调用onMessageReceived()。要生成通知,请使用 notification 键构建您的有效负载:

        var payload = {
            notification: {  // <= CHANGED
                title: "New jamroom available !",
                body: String("Jamroom id = ").concat(jamroomId)
            }
    

    【讨论】:

    • 正确,我今天下午注意到了。所以,改变它使它工作但不适用于我的模拟器,也就是说,一个共同开发者收到了通知,但不是我。我认为这是一个配置问题,但我还不知道是哪一个。
    • @ArthurDeschamps:检查你的 logcat 是否有 W/GooglePlayServicesUtil: Google Play services out of date。您的模拟器可能有旧版本的 Google Play 服务。
    • 其实我也试过我的实体安卓手机,也没有通知。无论哪种情况,logcat 中都没有这样的消息。
    • @ArthurDeschamps;如果故障设备是 API 26,则可能是通知通道的问题。
    • 我刚刚收到通知.. 但现在两次。这真是一团糟,但现在总体上似乎可以正常工作。感谢您的帮助!
    猜你喜欢
    • 2022-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-11
    • 2020-05-03
    • 2016-09-20
    • 2018-01-02
    • 1970-01-01
    相关资源
    最近更新 更多