【问题标题】:How to implement pushwoosh with ionic 2如何使用 ionic 2 实现 pushwoosh
【发布时间】:2017-04-19 11:48:48
【问题描述】:

我想在 ionic 2 中实现 pushwoosh,我正在使用 this cordova 插件。我是 ionic 2 的新手,想知道如何使用这个插件中的方法。

【问题讨论】:

    标签: push-notification ionic2 cordova-plugins pushwoosh


    【解决方案1】:

    首先阅读有关使用cordova插件的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。

    祝你好运;)

    【讨论】:

    • 是否必须使用 firebase 的项目编号?因为我使用的是 mySQL 数据库。
    • 对于 android,您需要一个用于 Cloud 消息传递的 firebase 项目编号,以发送通知。
    • 谢谢它真的帮了我很多!但我仍然收到 event.notification.message 错误。你能解释一下“通知”变量的来源吗?我的智能感知在通知上显示一条红线!
    • 我总是收到 'ERROR: Method 'onDeviceReady:' not defined in Plugin 'PushNotification' 你知道为什么会这样吗?
    • @nareeboy 您是否按照docs.pushwoosh.com/docs/cordova-phonegap 提供的步骤正确安装了插件?否则我需要更多关于你的代码的信息。
    【解决方案2】:

    你需要使用

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

    代替

    var message = event.notification.message;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-23
      • 2017-02-01
      • 2018-11-28
      • 1970-01-01
      • 2018-08-09
      • 1970-01-01
      • 2021-01-14
      相关资源
      最近更新 更多