简体   繁体   English

如何发送来自 Google Apps 脚本的 POST 数据?

[英]How to send POST data coming from Google Apps Script?

I used Firebase Cloud Messaging as a way to have push notifications in my web app.我使用 Firebase Cloud Messaging 作为在我的 Web 应用程序中推送通知的一种方式。 So now I am using Postman as a way to send my push notifications but i only send it manually所以现在我使用 Postman 作为发送推送通知的一种方式,但我只手动发送

From google apps script I need to send my push notification.从谷歌应用程序脚本我需要发送我的推送通知。

This is what I use in Postman which has no problem and it works这是我在 Postman 中使用的,它没有问题并且可以正常工作

在此处输入图片说明

This is from my Google apps script这是来自我的 Google 应用程序脚本

function firebaseNotification() {

  var headers = { 
    "Authorization" : "key=key",
    "Content-Type" : "application/json"
  };

  var options =
   {
      "notification": {
        "title": "CWMS",
        "body": "from google apps scritpt",
        "click_action": "site",
        "icon": "http://url-to-an-icon/icon.png"
    },
    "to": "key"
   };

  var response = UrlFetchApp.fetch("https://fcm.googleapis.com/fcm/send", options);  

}

What needs to happen is that from Google Apps Script function firebaseNotification() but be triggered there and can POST the data needed and the web app to receive the push notification需要发生的是从 Google Apps 脚本function firebaseNotification()但在那里被触发并且可以发布所需的数据和网络应用程序以接收推送通知

How about this modification?这个改装怎么样?

Modified script:修改后的脚本:

function firebaseNotification() {
  var headers = {
    "Authorization" : "key=key"
  };

  // Modified
  var payload = {
    "notification": {
      "title": "CWMS",
      "body": "from google apps scritpt",
      "click_action": "site",
      "icon": "http://url-to-an-icon/icon.png"
    },
    "to": "key"
  };

  // Modified
  var options = {
    method: "POST",
    contentType: "application/json",
    headers: headers,
    payload: JSON.stringify(payload) // <--- Modified
  }

  var response = UrlFetchApp.fetch("https://fcm.googleapis.com/fcm/send", options);
}

Reference:参考:

If this modification didn't resolve your issue, I apologize.如果此修改未能解决您的问题,我深表歉意。 At that time, please check the parameters for authorizing again.届时请再次检查授权参数。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM