Ionic 3 推送通知错误 "statusText":"Unknown Error","name":"HttpErrorResponse","message":"Http 失败响应(未知 url)

Ionic 3 Push Notification Error "statusText":"Unknown Error","name":"HttpErrorResponse","message":"Http failure response for (unknown url)

我在 Wordpress 4.9.8 上使用带后端的 Ionic 3,我按照指南制作推送通知:https://www.delitestudio.com/it/wordpress/push-notifications-for-wordpress/documentation/#configurePushNotificationsForWordPress

我在第 4.1 点,我写了这个:

export class NotifyPushProvider {

  private url="https://backendcomics.netsons.org/pnfw/register/";

  constructor(private push : Push, private http : HttpClient) {}

  register(){
      if(!isCordovaAvailable()){
        console.error('NotifyPush is off: cordova is not available');
        return;
      }else
          console.log('NotifyPush is on!');

      this.push.hasPermission()
          .then((res: any) => {
              if (res.isEnabled) {
                  console.log('We have permission to send push notifications');
              } else {
                  console.log('We do not have permission to send push notifications');
              }
          });


      this.push.createChannel({
          id: "900606371725-kt3b1chjppmhug88i7r306oivmu4qtd4.apps.googleusercontent.com",
          description: "My first test channel",
          // The importance property goes from 1 = Lowest, 2 = Low, 3 = Normal, 4 = High and 5 = Highest.
          importance: 3
      }).then(() => console.log('Channel created'));

      const options: PushOptions = {
          android: {
              vibrate :true,
              sound: 'false'
          },
          ios: {
              alert: 'true',
              badge: true,
              sound: 'false'
          },
          windows: {},
          browser: {
              pushServiceURL: this.url
          }
      };


      const pushObject: PushObject = this.push.init(options);


      pushObject.on('notification').subscribe((notification: any) => console.log('Received a notification', notification));

      pushObject.on('error').subscribe(error => console.error('Error with Push plugin', error));

      pushObject.on('registration').subscribe((registration: any) => {
          console.log('Device registered', JSON.stringify(registration))
          let x : number = new Date().getTime()
          console.log("Timestamp: "+x);

          console.log("post call")
          this.http.post(this.url,
              "oauth_consumer_key=ck_6fde6b6315c6b6646f45dbd2beec6ff5" +
              "&oauth_timestamp=" + x +
              "&oauth_nonce=" + x + "abc" +
              "&os=Android" +
              "&token=tokenless_" + x,
              {headers: {"Content-Type": "application/x-www-form-urlencoded",
                        "Access-Control-Allow-Origin": "https://backendcomics.netsons.org",
                        "Access-Control-Allow-Methods": "POST",
                        "Access-Control-Allow-Headers": "Content-Type"}}
          ).subscribe((data) => console.log("Post data:" + JSON.stringify(data)), error => console.error("Post error: " + JSON.stringify(error)));

          /*console.log("get call")
          this.http.get("https://backendcomics.netsons.org/pnfw/posts?" +
              "os=Android" +
              "&token=tokenless_" + x,
              {headers: {"Content-Type": "application/x-www-form-urlencoded"}}
          ).subscribe((data) => console.log("GET data:" + JSON.stringify(data)), error => console.error("GET error: " + JSON.stringify(error)));
*/
      });
    }
}

但是在 post 响应中我收到错误:

Post error: {"headers":{"normalizedNames":{},"lazyUpdate":null,"headers":{}},"status":0,"statusText":"Unknown Error","url":null,"ok":false,"name":"HttpErrorResponse","message":"Http failure response for (unknown url): 0 Unknown Error","error":{"isTrusted":true}}

和这个警告:

Bloccata richiesta multiorigine (cross-origin): il criterio di corrispondenza dell’origine non consente la lettura della risorsa remota da https://backendcomics.netsons.org/pnfw/register/. Motivo: richiesta preliminare (“preflight”) del canale CORS non riuscita.[Ulteriori informazioni]

Bloccata richiesta multiorigine (cross-origin): il criterio di corrispondenza dell’origine non consente la lettura della risorsa remota da https://backendcomics.netsons.org/pnfw/register/. Motivo: richiesta CORS non riuscita.[Ulteriori informazioni]

我使用 OneSignal 轻松解决了这个问题