2017-04-19 2 views

Antwort

3

Lesen Sie zuerst die pushwoosh Handbuch über die cordova Plugin: http://docs.pushwoosh.com/docs/cordova-phonegap

Danach bekam ich diesen Code auf iOS- und Android-arbeiten.

Auf Schritt 3 Sie den folgenden Code als Diensteanbieter verwenden können: in meinen Projekten Ordner ich diese Datei erstellt: Sie /src/app/providers/push-service.ts

import { Injectable } from "@angular/core"; 
import { Platform } from 'ionic-angular'; 
declare var cordova : any; 

@Injectable() 
export class PushService { 

    PUSHWOOSH_APP_ID : string = 'XXXXX-XXXXX'; // your pushwoosh app id 
    GOOGLE_PROJECT_NUMBER: string = 'XXXXXXXXXXXX'; // project number from firebase 

    constructor(public platform : Platform){ 

     this.platform.ready().then(() => { 
      if(this.platform.is('ios') || this.platform.is('android')){ 
       console.log("PushwooshService init: Running on push compatible platform "+ this.platform.userAgent() +')'); 
       this.initPushwoosh(); 
      } else{ 
       console.log("PushwooshService init: No compatible platform available. Skipping init.)"); 
       return; 
      } 
     }); 


    } 

    initPushwoosh(){ 
     let pushNotification = cordova.require("pushwoosh-cordova-plugin.PushNotification"); 

      //set push notifications handler 
      document.addEventListener('push-notification', function (event) { 
      let message = (event as any).notification.message; // Push message 
      let userData = (event as any).notification.userdata; // Custom push data 

      if (userData) { 
      // handle custom push data here 
      console.log('user data: ' + JSON.stringify(userData)); 
      } 

      }); 

      //initialize Pushwoosh with projectid: "GOOGLE_PROJECT_NUMBER", pw_appid : "PUSHWOOSH_APP_ID". This will trigger all pending push notifications on start. 
      pushNotification.onDeviceReady({ 
       appid: this.PUSHWOOSH_APP_ID, 
       projectid: this.GOOGLE_PROJECT_NUMBER 
       // serviceName: "MPNS_SERVICE_NAME" 
      }); 

      //register for pushes 
      pushNotification.registerDevice(
       function (status) { 
       var pushToken = status; 
       console.log(pushToken); 
       alert('push token: ' + JSON.stringify(pushToken)); 
       }, 
       function (status) { 
       alert(JSON.stringify(['failed to register ', status])); 
       } 
     ); 
    } 

} 

Jetzt kann diesen Anbieter in Ihre /src/app/app.component.ts importieren.

import { PushService } from '../providers/push-service'; 

    @Component({ 
     templateUrl: 'app.html', 
     providers: [PushService] 
    }) 

Wenn Ihre App gestartet wird, wird PushwooSh initialisiert.

Viel Glück;)

+0

Ist die Verwendung der Projektnummer von Firebase zwingend erforderlich? weil ich stattdessen eine mySQL-Datenbank verwende. – RamshaS

+1

Für Android benötigen Sie eine Firebase-Projektnummer für Cloud-Messaging, um Benachrichtigungen zu senden. – user1980927

+1

Dank es hilft mir wirklich viel !! Aber ich bekomme immer noch Fehler bei event.notification.message. Können Sie mir bitte erklären, woher die Variable 'Benachrichtigung' kommt? Mein Intellisense zeigt eine rote Linie bei Benachrichtigung! – RamshaS

1

Sie benötigen

var message = (event as any).notification.message;

zu verwenden, statt

var message = event.notification.message;