我正在尝试使用nativescript HTTP.request为firebase推送通知发送curl。我已经测试了卷曲,但是当我尝试通过http.request发送它时,我收到了错误的请求错误。
这是卷曲代码(出于隐私原因,我的密钥已替换为变量)
curl -X POST --header "Authorization: key=MyKey" --Header "Content-Type: application/json" https://fcm.googleapis.com/fcm/send -d "{\"notification\":{\"title\": \"My title\", \"text\": \"My text\", \"sound\": \"default\"}, \"data\":{\"foo\":\"bar\"}, \"priority\": \"High\", \"to\":\"d1LHpypPxA0:APA91bHG4HlZQnb7F_BkZAZYHv1MM00FhNbsONMhsLRB-U4p3As3C0Pp_8ALqQFusOOkgdSHZUlOfHbtt6qXU8pzCnjC-ozfMU3vTqjY0iy90XDvGHkDt0qw1w2wnr73PjFqViHEGONH\"}"
这是我的http.request
http.request({
url: 'https://fcm.googleapis.com/fcm/send',
method: "POST",
headers: { 'Authorization': 'key=MyKey','Content-Type': 'application/json'} ,
content: {
"notification": {
"title": "testingtesting",
"text": "some text",
"sound": "default",
"priority": "High"
}
},
data: { "foo": "bar" },
to: "d1LHpypPxA0:APA91bHG4HlZQnb7F_BkZAZYHv1MM00FhNbsONMhsLRB-U4p3As3C0Pp_8ALqQFusOOkgdSHZUlOfHbtt6qXU8pzCnjC-ozfMU3vTqjY0iy90XDvGHkDt0qw1w2wnr73PjFqViHEGONH"
}).then((response) => {
//HttpResult = response.content.toJSON();
console.log('----------------------------------------------------');
console.log(response.content);
}, (e) => {
console.log("Error occurred " + e);
});
任何帮助将不胜感激!
答案 0 :(得分:1)
我明白了,这是有效的代码。我在格式化方面遇到了一些问题,希望将来可以帮助某人!
var HttpResult;
http.request({
url: 'https://fcm.googleapis.com/fcm/send',
method: "POST",
headers: { 'Authorization': 'key=MyKey', 'Content-Type': 'application/json' },
content: JSON.stringify({
"notification": {
"title": "testingtesting",
"text": "some text",
"sound": "default",
"priority": "High"
},
'data': { "foo": "bar" },
'to': "d1LHpypPxA0:APA91bHG4HlZQnb7F_BkZAZYHv1MM00FhNbsONMhsLRB-U4p3As3C0Pp_8ALqQFusOOkgdSHZUlOfHbtt6qXU8pzCnjC-ozfMU3vTqjY0iy90XDvGHkDt0qw1w2wnr73PjFqViHEGONH"
})
}).then((response) => {
//HttpResult = response.content.toJSON();
console.log('----------------------------------------------------');
console.log(JSON.stringify(response));
}, (e) => {
console.log("Error occurred " + JSON.stringify(e));
});