Push notifications (iOS and Android) using Pushwoosh


#1

Hi everybody. I’ve followed some threads concerning this argument, but still cannot succeed in make Push Notifications work on iOS and Android. The app neither asks me permission to send push notifications…so I think there is something wrong.

This is my code in header of index.html:

<script src="lib/ngCordova/dist/ng-cordova.js"></script>
<!-- cordova script (this will be a 404 during development) -->
<script src="cordova.js"></script>

<!--NOTIFICATIONS -->
<script>
    function initPushwoosh() {
        var pushNotification = window.plugins.pushNotification;
        if(device.platform == "Android")
        {
            registerPushwooshAndroid();
        }else if(device.platform == "iPhone" || device.platform == "iOS")
        {
            registerPushwooshIOS();
        }
    }

This is the content of app.js:

.run(function($ionicPlatform,$rootScope, auth, store, jwtHelper, $location) {
$ionicPlatform.ready(function() {
//initPushwoosh();
document.addEventListener(“deviceready”, initPushwoosh, true);
[…]

And this is the content of PushwooshiOS.js:

function registerPushwooshIOS() {
var pushNotification = window.plugins.pushNotification;

//set push notification callback before we initialize the plugin
document.addEventListener('push-notification', function(event) {
			//get the notification payload
			var notification = event.notification;

			//display alert to the user for example
			alert(notification.aps.alert);

			//clear the app badge
			pushNotification.setApplicationIconBadgeNumber(0);
	});

//initialize the plugin
pushNotification.onDeviceReady({pw_appid:"XXXX-XXXX"});

	if(!window.localStorage['Pushwoosh']){ 
		//register for pushes
		pushNotification.registerDevice(
				function(status) {
						var deviceToken = status['deviceToken'];
						console.warn('registerDevice: ' + deviceToken);
						window.localStorage['Pushwoosh'] = true;
				},
				function(status) {
						console.warn('failed to register : ' + JSON.stringify(status));
						alert(JSON.stringify(['failed to register ', status]));
				}
		);
	}

//reset badges on app start
pushNotification.setApplicationIconBadgeNumber(0);

}

And this of PushwooshAndroid.js:

function registerPushwooshAndroid() {

  var pushNotification = window.plugins.pushNotification;


    //initialize Pushwoosh with projectid: "GOOGLE_PROJECT_ID", appid : "PUSHWOOSH_APP_ID". This will trigger all pending push notifications on start.
    pushNotification.onDeviceReady({ projectid: "XXXX-XXXX", appid : "XXXX-XXXX" });


 if(!window.localStorage['Pushwoosh']){ 
    //register for pushes
			pushNotification.registerDevice(
					function(status) {
							var pushToken = status;
							console.warn('push token: ' + pushToken);
							window.localStorage['Pushwoosh'] = true;
					},
					function(status) {
							console.warn(JSON.stringify(['failed to register ', status]));
					}
			);
		}

}

Where am I wrong?

PS: sorry if I made some mistake in asking the question, I’m new of the forum, I have to lear how to correctly ask questions here (copy/paste code, etc).


#2

I think that at this moment the deviceready event already has been called.


#3

Thanks, but I’ve tried using:

$ionicPlatform.ready(function() {
initPushwoosh();
[…]

And it doesn’t work neither…


#4

Hi,

I’m trying to do the exact same thing actually so if you succed I’m interested :wink:

Some ideas to test :

  • add ng-codova.js after cordova.js
  • I don’t see where you have added your iOS and Android js files
  • create directly the initPushwoosh in your $ionicPlatform.ready
  • Maybe it ridiculous but have you added the Pushwoosh plugin to your project

GL


#5

Can you confirm you’re using the following plugin?

Also, can you confirm the registerPushwooshAndroid() function is getting called or not with a console.log?


#6

Thanks guys. Yes I’ve added exactly this plugin. I’ve debugged with console.log and alerts and I’ve understood that the scripts succeeds in call the initPushwoosh() function, but executes only “half” of this function. I explain you:

<script>
    function initPushwoosh() {
        alert("ECCOCI QUI");
        alert(window.plugins.pushNotification);
        var pushNotification = window.plugins.pushNotification;
        alert(device.platform);
        if(device.platform == "Android")
        {
            registerPushwooshAndroid();
        }else if(device.platform == "iPhone" || device.platform == "iOS")
        {
            registerPushwooshIOS();
        }
    }

The first alert “ECCOCI QUI” is triggered. While the next, no. Maybe the problem is in window.plugins.pushNotification ?


#7

Does alert(device.platform) actually show you an expected result? (make that one the first alert/console.log for testing. I would always recommend console.log instead of alert.

I know it’s a bit of a cop out, but have you tried removing the platform ionic remove platform android and then adding it again?

While Pushwoosh was working for me with Android I just couldn’t get it to work with iOS, in the end I deleted the iOS platform, added it again and as if by magic, it sorted things out. I was as happy as I was annoyed that it worked!


#8

alert(device.platform) is not triggered at all: no alert appears. That’s strange…

I also prefer the console.log, but I cannot see any console.log in my Xcode debug screen (that’s weird). That’s why I use alerts.

I’ve tried removing iOS platform and re-adding it, but no, nothing changes…


#9

look at my post here : Sample code for integrating PushWoosh


#10

Thanks, but your post was exactly the post I used to copy/paste the code. But it doesn’t work on my side…as I told, the initPushwoosh() is called but crashes in var pushNotification = window.plugins.pushNotification; …


#11

did you include libraries/plugin???


#12

I’ve done: sudo ionic plugin add https://github.com/Pushwoosh/pushwoosh-phonegap-3.0-plugin


#13

you got solution to this problem, and if solucionaste could share the solution because I have the same problem. regards