【问题标题】:Google Cloud Function Node.js Firebase UrlGoogle Cloud Function Node.js Firebase 网址
【发布时间】:2017-06-16 05:47:45
【问题描述】:

我有一个 Android 应用程序,我可以在其中以 http 格式从我的设备发送推送通知,但现在我想编写一个节点,我希望在特定日期和时间安排推送通知,请有人帮忙我很长一段时间都陷入了这个过程

这是我的节点;

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


exports.sendNotification = functions.https.onRequest((req, res) => {
  const to = req.query.to;
  const title = req.query.title;
  const body = req.query.body;

  var payload;
  if (body != undefined && body !== '') {
    payload = {
      notification: {
        title: title,
        body: body
      }
    };
  } else {
    payload = {
      notification: {
        title: title
      }
    };
  }

  var options = {
    priority: "high",
    timeToLive: 60 * 60 * 24
  };

  if (to == 'all') {
    admin.messaging().sendToTopic(to, payload, options)
      .then(function (response) {
        res.send(200, 'ok');
      })
      .catch(function (error) {
        res.send(200, 'failed');
      });
  } else {
    admin.messaging().sendToDevice(to, payload, options)
      .then(function (response) {
        res.send(200, 'ok');
      })
      .catch(function (error) {
        res.send(200, 'failed');
      });
  }
});

这里我有运行推送通知时的 URL,

https://MyProjectName.cloudfunctions.net/sendNotification?to=all&title=hello&body=EnterBodyHere

现在,请帮我安排这样的推送通知

localhost:8080:/schedulePush?day=17&month=10&hour=12&minute=22

【问题讨论】:

    标签: android node.js firebase push-notification google-cloud-functions


    【解决方案1】:

    Cloud Functions 目前没有内置的调度机制。如果您想在特定时间执行功能,则需要使用其他一些调度程序。有一个blog post 是关于这个的,sample code 也讨论了这个。

    【讨论】:

    • 我的朋友说,你只需要创建 Node.js 端点来安排推送通知。 URL 查询中的日期/时间............就像,npmjs.com/package/cron 使用这个库,它有时区并使用它们...... .......我试过了,但想不通。
    • 该包不适用于 Cloud Functions,因为它没有运行一个始终保持活动状态的单个 node.js 实例。相反,多个托管实例可以而且将会随着对您的功能的需求随时间而变化。
    • 那么您对如何在预定的日期和时间运行链接有什么建议吗?
    • 在我的应用程序中可以说有一个订单,我在 20 日下午 5 点下订单,然后点击提交。现在我想要 20 日下午 5 点的预定触发通知
    • 您是否阅读了我在回答中链接到的博客文章和示例代码?
    猜你喜欢
    • 2019-04-30
    • 2022-09-28
    • 2018-09-09
    • 2022-08-16
    • 1970-01-01
    • 2019-01-18
    • 2019-11-12
    • 2019-10-09
    • 1970-01-01
    相关资源
    最近更新 更多