【发布时间】:2020-03-23 05:38:36
【问题描述】:
我正在尝试使用 HttpClient 在 Ionic 中通过发布请求发送推送通知。它在 Android 和 Web 中运行,但在 iOS 中显示此错误:
{"headers":{"normalizedNames":{},"lazyUpdate":null,"headers":{}},"status":0,"statusText":"Unknown Error","url":"https://fcm.googleapis.com/fcm/send","ok":false,"name":"HttpErrorResponse","message":"Http failure response for https://fcm.googleapis.com/fcm/send: 0 Unknown Error","error":{"isTrusted":true}}
这是我的功能:
import {HttpClient, HttpHeaders} from "@angular/common/http";
...
public sendPost(){
let url = 'https://fcm.googleapis.com/fcm/send';
let appKEY = 'key=my_key';
let body = {
"condition":"'topico' in topics",
"notification" : {
"title": "Test",
"body": "Hello!!!",
"sound": "default"
}
}
let header: HttpHeaders = new HttpHeaders();
header = header.set('Authorization', appKEY);
header = header.append("Content-Type", "application/json");
const options = {
headers: header
}
this.http.post(url, body, options)
.subscribe(data => {
alert("Success!!");
console.log(data);
}, error => {
alert("Error!");
console.log(error);
});
}
我的信息
Ionic:
Ionic CLI : 5.4.16
Ionic Framework : @ionic/angular 4.11.10
@angular-devkit/build-angular : 0.900.6
@angular-devkit/schematics : 9.0.6
@angular/cli : 9.0.6
@ionic/angular-toolkit : 2.0.0
Capacitor:
Capacitor CLI : 1.4.0
@capacitor/core : 1.5.0
Cordova:
Cordova CLI : 9.0.0 (cordova-lib@9.0.1)
Cordova Platforms : none
Cordova Plugins : no whitelisted plugins (0 plugins total)
Utility:
cordova-res : 0.10.0
native-run : not installed
System:
NodeJS : v12.16.1 (/usr/local/bin/node)
npm : 6.13.4
OS : macOS Catalina
Xcode : Xcode 11.3.1 Build version 11C504
这种情况只发生在这个网址上,我已经用另外两个简单的网址(不带标题)进行了测试,并且帖子成功了。
【问题讨论】:
-
你解决了这个问题吗?
-
你找到解决办法了吗?
-
发生这种情况是因为 iOS 阻止了一些没有具体身份验证的请求。我的解决方案是在 firebase 上创建一个云功能。示例: const jwt = My_Authorization; const topic = SOME_TEXT const title = SOME_TEXT const text = SOME_TEXT await axios.post('us-central1-PROJECT.cloudfunctions.net/app/FILE', { topic, title, text }, { headers: { Authorization:
Bearer ${jwt}}, });
标签: ios ionic-framework post firebase-cloud-messaging httpclient