Integrate Ionic and PushWoosh


#1

I did the example of PushWoosh http://docs.pushwoosh.com/docs/cordova-phonegap and all the notifications was ok, but when I put the code in a Ionic Project, the notifications doesn’t work I don’t know what happen.

I put the initPushwoosh in app.js

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    initPushwoosh();
}

And this is my code in pushwoosh.js

function initPushwoosh()

{

var pushNotification = cordova.require("com.pushwoosh.plugins.pushwoosh.PushNotification");
if (device.platform == "Android") {
    registerPushwooshAndroid();
}
//alert(pushNotification);

// Listener que espera a chegada da notificação e roda um evento

pushNotification.getLaunchNotification(
    function(notification) {
        if (notification != null) {
            alert(JSON.stringify(notification));
        } else {
            alert("No launch notification");
        }
    }
    );

}

function registerPushwooshAndroid() {

var pushNotification = cordova.require("com.pushwoosh.plugins.pushwoosh.PushNotification");

//set push notifications handler
document.addEventListener('push-notification', function(event) {
    var title = event.notification.title;
    var userData = event.notification.userdata;
    
    console.warn('user data: ' + JSON.stringify(userData));
    alert(title);
});
// initialize Pushwoosh with projectid: "GOOGLE_PROJECT_ID", appid : "PUSHWOOSH_APP_ID". This will trigger all pending push notifications on start.
pushNotification.onDeviceReady({
pushNotification.onDeviceReady({
    projectid: "GOOGLE_PROJECT_ID", // my id google
    pw_appid: "PUSHWOOSH_APP_ID" // my id pushwoosh app
});

//register for push notifications
pushNotification.registerDevice(
    function(token) {
        //alert(token); //the toke when i activate works perfect!!!!!!!!
        //callback when pushwoosh is ready
        onPushwooshAndroidInitialized(token);
    },
    function(status) {
        alert("failed to register: " + status);
        console.warn(JSON.stringify(['failed to register ', status]));
    }
    );;

}

function onPushwooshAndroidInitialized(pushToken) {
//output the token to the console
console.warn('push token: ’ + pushToken);

var pushNotification = cordova.require("com.pushwoosh.plugins.pushwoosh.PushNotification");

//if you need push token at a later time you can always get it from Pushwoosh plugin
pushNotification.getPushToken(
    function(token) {
        console.warn('push token: ' + token);
    }
    );

//and HWID if you want to communicate with Pushwoosh API
pushNotification.getPushwooshHWID(
    function(token) {
        console.warn('Pushwoosh HWID: ' + token);
    }
    );

pushNotification.getTags(
    function(tags) {
        console.warn('tags for the device: ' + JSON.stringify(tags));
    },
    function(error) {
        console.warn('get tags error: ' + JSON.stringify(error));
    }
    );


//set multi notificaiton mode
//pushNotification.setMultiNotificationMode();
//pushNotification.setEnableLED(true);

//set single notification mode
//pushNotification.setSingleNotificationMode();

//disable sound and vibration
//pushNotification.setSoundType(1);
//pushNotification.setVibrateType(1);

pushNotification.setLightScreenOnNotification(false);

//setting list tags
//pushNotification.setTags({"MyTag":["hello", "world"]});

//settings tags
pushNotification.setTags({
    deviceName: "hello",
    deviceId: 10
},
function(status) {
    console.warn('setTags success');
},
function(status) {
    console.warn('setTags failed');
}
);

//Pushwoosh Android specific method that cares for the battery
//pushNotification.startGeoPushes();

}