[英]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:为此,您需要了解两件事:
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:在这种情况下:
the script sends a notification message .该脚本发送一条通知消息。 This type of message:
这种类型的消息:
If you want full control over what the app does when the message reaches the device, send a data message如果您想在消息到达设备时完全控制应用程序的操作,请发送数据消息
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 设备之间发送通知。
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.