在解析云代码中使用 OneSignal 发送推送通知

Send Push Notification using OneSignal in parse cloud code

我几乎复制并粘贴了 OneSignal 的文档示例,该示例说明了解析云函数的外观,只是尝试推出一个简单的测试。这是我的云代码:

Parse.Cloud.define('followPush', function(request, response) {
send = function(request) {

var promise = new Parse.Promise();

var jsonBody = {
app_id: "XXX",
included_segments: ["All"],
contents: {en: "English Message"},
data: {foo: "bar"}
};

Parse.Cloud.httpRequest({
                        method: "POST",
                        url: "https://onesignal.com/api/v1/notifications",
                        headers: {
                        "Content-Type": "application/json;charset=utf-8",
                        "Authorization": "Basic XXX"
                        },
                        body: JSON.stringify(jsonBody)
                        }).then(function (httpResponse) {
                                promise.resolve(httpResponse)
                                },
                                function (httpResponse) {
                                promise.reject(httpResponse);
                                });

return promise;
};

exports.send = send;

});

我在服务器日志中得到 "request timeout",在客户端得到 "JSON text did not start with..."。如果我从 OneSignal 网站发送推送通知,它会到达用户。我让它与 Parse 一起工作,但不理解与 oneSignal 的关系。

您似乎可以联系 OneSignal 开发团队来解决这个问题。 (我帮助开发 OneSignal)

为了其他 Whosebug 用户的利益,解决方案是将您的 httpRequest 代码更改为如下:

Parse.Cloud.httpRequest({ 
  url: "https://onesignal.com/api/v1/notifications", 
  method: "POST", 
  headers: { 
    "Content-Type": "application/json;charset=utf-8", 
    "Authorization": "Basic XXX" 
  }, 
  body: JSON.stringify(jsonBody), 
  success: function(httpResponse) { 
    response.success("sent"); 
  }, 
  error: function(httpResponse) { 
    response.error('Failed with: ' + httpResponse.status); 
  } 
});