2017-04-19 68 views

回答

3

首先閱讀pushwoosh手冊有關使用科爾多瓦插件:http://docs.pushwoosh.com/docs/cordova-phonegap

之後,我得到這個代碼,iOS和Android上工作。

在步驟3中,您可以使用下面的代碼作爲一個服務提供商: 在我的項目文件夾,我創建這個文件:/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])); 
       } 
     ); 
    } 

} 

現在你可以將此提供程序導入到/src/app/app.component.ts中。

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

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

每當您的應用程序啓動時,它將初始化pushwoosh。

祝你好運;)

+0

是否必須使用Firebase中的項目編號?因爲我正在使用mySQL數據庫。 – RamshaS

+1

對於android,您需要雲消息傳遞的Firebase項目編號才能發送通知。 – user1980927

+1

謝謝它真的幫助我很多! 但我仍然在event.notification.message錯誤。你能解釋一下'通知'變量來自哪裏嗎?我的智能感知在通知中顯示紅線! – RamshaS

1

您需要使用

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

而不是

var message = event.notification.message;