【发布时间】:2017-04-23 16:41:09
【问题描述】:
我尝试使用 Ionic 2 和 Angular http 向https://fcm.googleapis.com/fcm/send 发出 http post 请求,使用 postman 测试的请求工作得非常好
这个问题是我这个问题的后续问题:
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";马=2592000; v="35,34" 缓存控制:私有,最大年龄=0 内容编码:gzip 内容长度:260 内容类型:文本/html;字符集=UTF-8 日期:格林威治标准时间 2016 年 12 月 8 日星期四 12:36:14 过期时间:格林威治标准时间 2016 年 12 月 8 日星期四 12:36:14 服务器:GSE 状态:401 x 内容类型选项:nosniff x-frame-options:SAMEORIGIN x-xss-保护:1;模式=块
请求标头
请求标头 显示临时标题 内容类型:应用程序/json 来源:http://localhost:8100 推荐人:http://localhost:8100/ 用户代理:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.100 Safari/537.36
请求负载 {通知:{title:“通知标题”,正文:“通知正文”,声音:“默认”,…},…} 数据 : {你好:“这是 Firebase 云消息传递设备新消息!”} 通知 : {title: "通知标题", body: "通知正文", sound: "default",…} 到 : “/主题/烹饪”
【问题讨论】:
-
你能给我们实际从你的borwser发送的Header吗?它应该在 DevTools (F12 for chrome) 网络选项卡中。在您的问题中发布之前,当然要混淆密钥。
-
请求头是:显示临时头 content-type:application/json Origin:localhost:8100Referer:localhost:8100 User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 ( KHTML,如 Gecko) Chrome/54.0.2840.100 Safari/537.36
-
因此 Authorization.append(key, value) 由于某种原因失败,并且由于未发送密钥,因此您收到错误消息。是否定义了 someKey 值?尝试在代码中手动添加 FCM 密钥,看看发生了什么,然后从变量中追加。
-
headers.append('授权', 'key=AAAA****bHw3*****');我正在这样使用它...
-
即使从变量调用结果也是一样的
标签: angular firebase push-notification http-post firebase-cloud-messaging