【问题标题】:How to send push notification from Firebase using google apps script?如何使用谷歌应用脚​​本从 Firebase 发送推送通知?
【发布时间】:2017-06-29 16:28:25
【问题描述】:

我想编写一个小脚本,告诉 Firebase 在满足特定条件时推送通知。如何使用Google Apps脚本从Firebase发送推送通知?

【问题讨论】:

标签: android firebase google-apps-script push-notification firebase-cloud-messaging


【解决方案1】:

我以前从未尝试过,但它实际上非常简单。

为此,您需要了解两件事:

  1. How to send a HTTP request to Firebase Cloud Messaging to deliver a message
  2. How to call an external HTTP API from with Apps Script

一旦您阅读了这两个文档,代码就相当简单了:

function sendNotificationMessage() {
  var response = UrlFetchApp.fetch('https://fcm.googleapis.com/fcm/send', {
    method: 'POST',
    contentType: 'application/json',
    headers: {
      Authorization: 'key=AAAAIM...WBRT'
    },
    payload: JSON.stringify({
      notification: {
        title: 'Hello TSR!'
      },
      to: 'cVR0...KhOYB'
    })
  });
  Logger.log(response);
}

在这种情况下:

  1. 脚本发送notification message。此类消息:

    • 应用未激活时自动显示在系统托盘中
    • 应用处于活动状态时不显示

    如果您想在消息到达设备时完全控制应用的操作,请发送data message

  2. 脚本将消息发送到特定设备,由to 属性中的设备令牌标识。您还可以发送到一个主题,例如/topics/user_TSR。有关这方面的更广泛示例,请参阅我在 Sending notifications between Android devices with Firebase Database and Cloud Messaging 上的博客文章。

  3. Authorization 标头中的 key 需要与您的 Firebase 项目匹配。见Firebase messaging, where to get Server Key?

【讨论】:

  • @TSR 你试过了吗?你有快速的示例代码吗?
猜你喜欢
  • 1970-01-01
  • 2018-06-01
  • 2018-02-28
  • 1970-01-01
  • 1970-01-01
  • 2019-11-11
  • 2017-02-24
  • 2019-03-18
  • 1970-01-01
相关资源
最近更新 更多