如何使用ionic 2实现pushwoosh

编程入门 行业动态 更新时间:2024-10-22 05:11:01
本文介绍了如何使用ionic 2实现pushwoosh的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I want to implement pushwoosh in ionic 2 i am using this cordova plugin. i am new to ionic 2 want to know how to use methods from this plugin.

推荐答案

首先阅读有关使用cordova插件的pushwoosh手册: docs.pushwoosh/docs/cordova-phonegap

First read the pushwoosh manual about using the cordova plugin: docs.pushwoosh/docs/cordova-phonegap

之后,我使这段代码在ios和android上运行.

After that i got this code working on ios and android.

在第3步中,您可以将以下代码用作服务提供者: 在我的项目文件夹中,我创建了以下文件:/src/app/providers/push-service.ts

On step 3, you can use the following code as a service provider: in my projects folder i created this file: /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/appponent.ts中导入此提供程序.

Now you can import this provider in your /src/app/appponent.ts.

import { PushService } from '../providers/push-service'; @Component({ templateUrl: 'app.html', providers: [PushService] })

只要您的应用启动,它将初始化pushwoosh.

Whenever your app is launched, it will initialize pushwoosh.

祝你好运;)

更多推荐

如何使用ionic 2实现pushwoosh

本文发布于:2023-10-09 06:12:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1474920.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   ionic   pushwoosh

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!