【问题标题】:push notification to specific device from another using firebase android java使用firebase android java从另一个设备向特定设备推送通知
【发布时间】:2020-10-21 21:57:59
【问题描述】:

我的代码需要您的帮助,当用户从他的应用执行操作以通知另一个用户(我有他的设备令牌)时,我需要发送通知。

我已经做过的事情: 首先,安装 node.js 并从中下载 firebase admin SDK 并登录并将其连接到我在 firebase 上的项目,直到我成功连接 Firebase CLI。

然后我打开 index.js 文件并将这段代码添加到其中:

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

exports.pushNotification = functions.database.ref('/YourNode/{pushId}').onWrite((change, context) => {
    console.log('Push notification event triggered');

    //  Get the current value of what was written to the Realtime Database.
    const valueObject = change.after.val(); 

    // Create a notification
    const payload = {
        notification: {
            title: valueObject.title,
            body: valueObject.message,
            sound: "default"
        }
    };

    //Create an options object that contains the time to live for the notification and the priority
    const options = {
        priority: "high",
        timeToLive: 60 * 60 * 24
    };

    return admin.messaging().sendToTopic("pushNotifications", payload, options);
});
 

然后我返回 cmd 屏幕并输入 firebase deploy 但此时它返回一个错误:

当我在谷歌上搜索这个问题时,我发现我需要为 Firebase 的会员付费,对吧?

我需要什么

1- 如果正确,我的代码和步骤对吗?接下来我应该怎么做才能让通知正常工作?

2- 你能帮我提供正确和具体的步骤来完成这个吗?

不要给我任何指向 firebase 的链接,我阅读了所有文档并且真的很困惑。

感谢您的宝贵时间,等待您的帮助:)

【问题讨论】:

  • 只是为了澄清:确切的情况是您需要根据对您的 node.js 应用程序的某些请求向其他人发送推送消息?你在哪里托管你的 node.js 应用程序?
  • @mrpasqal 来自共享的代码和似乎是 Firebase's Cloud Functions 的标记。
  • 下面是我能回答的一个问题。如果问题 #1 和 #2 仍然适用于该答案,请编辑您的问题以显示运行此代码时的问题。
  • @mrpasqal 我正在尝试使用 Firebase 云功能发送推送通知,以防其他用户应用休眠

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


【解决方案1】:

我想我需要付费才能成为 firebase 的会员,对吧?

如果您的代码需要 Node.js 10 或更高版本,则无法再将其部署在 Firebase 的免费计划中,而您确实需要采用现收现付计划。您收到的错误消息也明确提到了这一点。我建议研究Firebase pricing FAQ on Cloud Functions,因为它非常复杂,我不想误引它。

【讨论】:

  • 感谢您的回复,我需要确保我采取了正确的步骤来获取通知?如果是这样,我将支付一个计划。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多