Ionic 3 Push Notification Error «statusText»: «Неизвестная ошибка», «name»: «HttpErrorResponse», «message»: «Http-ошибка ответа для (неизвестный URL) - PullRequest
0 голосов
/ 20 ноября 2018

Я использую Ionic 3 с бэкэндом на Wordpress 4.9.8 и следую руководству, чтобы сделать push-уведомление: 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 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]

1 Ответ

0 голосов
/ 26 ноября 2018

Я решил это легко, используя OneSignal вместо этого

...