使用Ionic 2和angular http我试图向https://fcm.googleapis.com/fcm/send发送http post请求,使用邮递员测试的请求完全正常
这个问题是我这个问题的后续问题:
HTTP.post to FCM Server not working
控制台告知错误请求缺少验证密钥(FCM令牌)。请访问firebase.google.com/docs/cloud-messaging/server,参阅FCM文档的“身份验证”部分。错误401
http请求的代码是
import { Http, Headers } from '@angular/http';
......
constructor(public http: Http) { }
sendPushNotification(deviceId: string) {
let url = 'https://fcm.googleapis.com/fcm/send';
let body =
{
"notification": {
"title": "Notification title",
"body": "Notification body",
"sound": "default",
"click_action": "FCM_PLUGIN_ACTIVITY",
"icon": "fcm_push_icon"
},
"data": {
"hello": "This is a Firebase Cloud Messagin hbhj g Device Gr new v Message!",
},
"to": "device token"
};
let headers: Headers = new Headers();
headers.append('content-type', 'application/json');
headers.append('Authorization', 'key='+someKey);
this.http.post(url, body, headers).map(response => {
return response;
}).subscribe(data => {
//post doesn't fire if it doesn't get subscribed to
console.log(data);
});
}
来自chrome控制台的标题如下:
一般标题
请求网址:https://fcm.googleapis.com/fcm/send 请求方法:POST 状态代码:401 远程地址:[2404:6800:4009:807 :: 200a]:443
回复标题
存取控制允许来源:http://localhost:8100 访问控制展露报头:内容编码,内容长度,内容类型,日期,服务器 ALT-SVC:QUIC = “:443”; MA = 2592000; V = “35,34” cache-control:private,max-age = 0 内容编码:gzip 内容长度:260 内容类型:文本/ HTML;字符集= UTF-8 日期:星期四,2016年12月8日12:36:14 GMT 到期日:2016年12月8日星期四12:36:14 GMT 服务器:GSE 状态:401 X-内容类型选项:nosniff X框选项:SAMEORIGIN X-XSS-保护:1;模式=块
请求标题
请求标头 显示临时标题 内容类型:application / JSON 起源:http://localhost:8100 引用者:http://localhost:8100/ User-Agent:Mozilla / 5.0(X11; Linux x86_64)AppleWebKit / 537.36(KHTML,与Gecko一样)Chrome / 54.0.2840.100 Safari / 537.36
请求有效负载 {notification:{title:“Notification title”,body:“Notification body”,sound:“default”,...},...} 数据 : {hello:“这是Firebase云消息传递设备的新消息!”} 通知 : {title:“通知标题”,正文:“通知正文”,声音:“默认”,...} 至 : “/主题/烹饪”
答案 0 :(得分:1)
通过添加标题作为请求选项来实现这一点,
let headers: Headers = new Headers({
'Content-Type': 'application/json',
'Authorization': 'key='+this.someKey
});
let options = new RequestOptions({ headers: headers });