简体   繁体   中英

Ionic - HTTP POST Request not working in iOS, but working in Android and Web

I am trying send a push notification via post request in Ionic using HttpClient. It's working in Android and Web, but in iOS show this error:

{"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}}

This is my function:

 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);
    });
  }

My infos

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

That happens only with this url, I alread has tested with two others simple urls (without headers) and the post was succefull.

I have solved it.

return new Promise((resolve, reject) => {
  this.http.setServerTrustMode('nocheck');
  this.http.setHeader('*', 'Access-Control-Allow-Origin' , '*');
  this.http.setHeader('*', 'Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT');
  this.http.setHeader('*', 'Accept','application/json');
  this.http.setHeader('*', 'content-type','application/json');
  this.http.setHeader('*', 'Authorization', 'key=your-key-here')
  
  this.http.setDataSerializer('json');
  this.http.post(url, data, {}).then(res =>{
    resolve(JSON.parse(res.data));
  })
  .catch(err =>{
    reject(err);
  });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM