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.
Asked
Active
Viewed 1,039 times
2 Answers
3
First read the pushwoosh manual about using the cordova plugin: http://docs.pushwoosh.com/docs/cordova-phonegap
After that i got this code working on ios and android.
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]));
}
);
}
}
Now you can import this provider in your /src/app/app.component.ts.
import { PushService } from '../providers/push-service';
@Component({
templateUrl: 'app.html',
providers: [PushService]
})
Whenever your app is launched, it will initialize pushwoosh.
Good luck ;)

user1980927
- 46
- 3
-
Is it compulsory to use project number from firebase? because i am using a mySQL database instead. – Ramsha Omer Apr 20 '17 at 11:41
-
1For android you need a firebase project number for Cloud messaging, to send notifications. – user1980927 Apr 20 '17 at 12:03
-
1Thanks it really helps me alot!! But i am still getting error on event.notification.message. Can you please explain me from where 'notification' variable comes from? My intellisense showing a red line on notification! – Ramsha Omer Apr 24 '17 at 20:59
-
I am always getting 'ERROR: Method 'onDeviceReady:' not defined in Plugin 'PushNotification' do you know why this is happening? – nareeboy Aug 02 '17 at 14:09
-
@nareeboy Did you follow the steps provided by http://docs.pushwoosh.com/docs/cordova-phonegap and install the plugin correctly? Otherwise i Need some more information about your code. – user1980927 Aug 03 '17 at 15:22
-
@RamshaS , sorry to react so late. Have you figured it out yet, if not, i changed the code a little to prevent this from happening. – user1980927 Aug 03 '17 at 15:24
1
You need to use
var message = (event as any).notification.message;
Instead of
var message = event.notification.message;

Konstantin
- 21
- 4