2017-04-19 32 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;