【问题标题】:Cloud Functions for Firebase: Sending topic notification to Android device not workingCloud Functions for Firebase:向 Android 设备发送主题通知不起作用
【发布时间】:2017-08-11 04:12:36
【问题描述】:

我正在学习 Udacity 的本教程,其中 Cloud Functions for Firebase 用于更改添加数据的 ref 数据。 我想使用类似的功能,但用于向订阅主题的用户发送推送通知。

这是我正在使用的功能。

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.sendNotification = functions.database.ref('/messages/{pushId}/text').onWrite((event) => {
    const data = event.data;
    console.log('Message received');
    if(!data.changed()){
        console.log('Nothing changed');
        return;
    }else{
        console.log(data.val());
    }

    const payLoad = {
        notification:{
            title: 'Message received',
            body: 'You received a new message',
            sound: "default"
        }
    };

    const options = {
        priority: "high",
        timeToLive: 60*60*2
    };

    return admin.messaging().sendToTopic("Message_Notifications", payLoad, options);
});

查看 Firebase 函数日志,我可以看到当我向数据库添加新值时调用了该函数。我正在订阅 mainactivity 中的主题

FirebaseMessaging.getInstance().subscribeToTopic("Message_Notification");

几个小时后,我可以在 firebase 通知控制台的主题列表中看到该主题。我从那里发送了一个通知,它起作用了。

为了在每次添加新消息时显示通知,我还需要做哪些其他事情?

感谢任何建议。我对 Firebase 相当陌生。因此,如果有更好的博客/帖子,请重定向我,以便我了解更多信息。

提前致谢。

【问题讨论】:

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


    【解决方案1】:

    在您的 Android 应用中,您正在订阅 "Message_Notification" 主题。

    在您的脚本中,您发送到"Message_Notifications" 主题。

    在您的 Android 应用中添加一个 s 作为主题名称,或者从您的脚本中删除多余的 s

    【讨论】:

    • 我看了很多东西。甚至没有注意到我是如何错过那个的。感谢您耐心阅读。需要改进我的校对阅读。干杯。
    • 很多开发人员都会遇到这种情况。 :) 祝你的应用好运。干杯!
    猜你喜欢
    • 2017-11-16
    • 2017-10-29
    • 2020-05-11
    • 2017-10-08
    • 1970-01-01
    • 2018-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多