简体   繁体   English

如何使用谷歌应用脚​​本从 Firebase 发送推送通知?

[英]How to send push notification from Firebase using google apps script?

I want to write a little script that tells Firebase to push notification if a certain condition is met.我想编写一个小脚本,告诉 Firebase 在满足某个条件时推送通知。 How to send push notification from Firebase using google apps script?如何使用谷歌应用脚​​本从 Firebase 发送推送通知?

I'd never tried this before, but it's actually remarkably simple.我以前从未尝试过,但它实际上非常简单。

There are two things you need to know for this:为此,您需要了解两件事:

  1. How to send a HTTP request to Firebase Cloud Messaging to deliver a message 如何向 Firebase Cloud Messaging 发送 HTTP 请求以传递消息
  2. How to call an external HTTP API from with Apps Script 如何使用 Apps 脚本调用外部 HTTP API

Once you have read those two pieces of documentation, the code is fairly straightforward:一旦你阅读了这两个文档,代码就相当简单了:

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);
}

In this case:在这种情况下:

  1. the script sends a notification message .该脚本发送一条通知消息 This type of message:这种类型的消息:

    • shows up automatically in the system tray when the app is not active应用未激活时自动显示在系统托盘中
    • is not displayed when the app is active应用程序处于活动状态时不显示

    If you want full control over what the app does when the message reaches the device, send a data message如果您想在消息到达设备时完全控制应用程序的操作,请发送数据消息

  2. the script send the message to a specific device, identified by its device token in the to property.该脚本将消息发送到特定设备,该设备由to属性中的设备令牌标识。 You could also send to a topic, such as /topics/user_TSR .您还可以发送到一个主题,例如/topics/user_TSR For a broader example of this, see my blog post on Sending notifications between Android devices with Firebase Database and Cloud Messaging .有关这方面的更广泛示例,请参阅我的博客文章在使用 Firebase 数据库和云消息传递的 Android 设备之间发送通知

  3. the key in the Authorization header will need to match the one for your Firebase project. Authorization标头中的key需要与您的 Firebase 项目的密钥相匹配。 See Firebase messaging, where to get Server Key?请参阅Firebase 消息,从哪里获取服务器密钥?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何发送推送通知Firebase - How to send Push Notification Firebase 如何使用 firebase admin sdk 向所有用户发送推送通知? - How to send a push notification to all users using firebase admin sdk? 如何从客户端向每个人发送简单的Firebase推送通知? - How to send a simple Firebase push notification to everyone from the client? 如何从节点服务器发送 firebase 推送通知 - How to send firebase push notification from node server 如何使用自定义点击操作从Firebase控制台发送推送通知 - How to send Push Notification With Custom Click Action From Firebase Console 如何使用 Spring Boot 在 Google Firebase FCM 上推送通知? - How to push notification on Google Firebase FCM using spring boot? Firebase推送通知如何发送到设备 - How Firebase Push Notification is send to device 如何使用本地化内容向 Firebase 发送推送通知? - How to send push notification to Firebase with localized content? 使用Firebase和Google Cloud Messaging推送通知 - Push Notification using Firebase and Google Cloud Messaging 是否可以通过在“ app_remove”事件中使用Google Analytics for Firebase触发器向Android应用自动发送推送通知 - Is it possible to send automatically push notification to android app by using Google Analytics for Firebase Triggers on “app_remove” event
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM