Sample code for integrating PushWoosh


#1

Hi,

I am new to ionic and facing difficulties in integrating PushWoosh for push notifications as defined here;

https://www.pushwoosh.com/programming-push-notification/ios/ios-additional-platforms/push-notification-sdk-integration-for-phonegap/

If anyone has implemented, can I get the sample code?

Thanks!


Push notifications (iOS and Android) using Pushwoosh
#2

which os will you be supporting?


#3

Both Android and iOS


#4

Oke this is how i do it in app.js :smile:

starter.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {

	initPushwoosh();
	
  });
})

Then in my main index.html:

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

   </script>
    <script type="text/javascript" src="js/PushwooshAndroid.js"></script>
    <script type="text/javascript" src="js/PushwooshiOS.js"></script>-

and pushwooshandroid.js :

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

    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: "YOUR PROJECT ID", appid : "YOUR APP ID" });
     
     
     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]));
    					}
    			);
    		}
    
    }

And 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:"PUSH WOOSH APPID"});
     
		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);
}

#5

Thank you so much @sjerd.

I tested the Android version and it is working fine for test pushes from PushWoosh. I am wondering how do I initiate a push from the ionic app? Any idea?


#6

Thank you, this helped me out a lot.

Just one thing I would add, when I add the pushwoosh plugin I could no longer build the iOS version of the app. Easy solution though, run ‘ionic platform remove ios’ and then ‘ionic platform add ios’. And all seems to work as expected.

I just pasted your code in and changed the ID’s.


#7

Hey @sjerd

You are adding an eventListener for push-notification to handle the notifications in ios.
But I cannot find one for the android part of the code you use. Is it intentional?

I went ahead and added the listener to the android module, but had no luck.
It has been a struggle getting this to work for android.

I send a test notification from pushwoosh, but it is not received.
Getting a direction from you would be great.

Thanks


#8

Owww oops, add this to the PushwooshAndroid.js register function so it won’t get executed on ios:

 var pushNotification = window.plugins.pushNotification;

//set push notifications handler
document.addEventListener('push-notification', function(event) {
    var message = event.notification.message;
    var userData = event.notification.userdata;
                             
    if(typeof(userData) != "undefined") {
        console.warn('user data: ' + JSON.stringify(userData));
    }
                                 
    alert(message);
});

#9

Hello @sjerd , it’s possible to adapt lastest (3.4) pushwoosh phonegap version to ngCordova ? https://github.com/Pushwoosh/pushwoosh-phonegap-3.0-plugin/ Any advice a good start for this job?


#10

actually, i haven’t seen pushwoosh at all in ngcordova. you could try and create your own factory something like this(not tested):

angular.module('ngCordova.plugins.pushwoosh', [])
  .factory('$cordovaPushwoosh', ['$q', '$window', function ($q, $window) {
    return {
      initPushwoosh: function () {
        var q = $q.defer();
		var pushNotification = $window.plugins.pushNotification;
		if(device.platform == "Android") {
        	//initialize Pushwoosh with projectid: "GOOGLE_PROJECT_ID", appid : "PUSHWOOSH_APP_ID". This will trigger all pending push notifications on start.
			pushNotification.onDeviceReady({ projectid: "YOUR PROJECT ID", appid : "YOUR APP ID" });
			
		}else if(device.platform == "iPhone" || device.platform == "iOS") {
			//initialize the plugin
			pushNotification.onDeviceReady({pw_appid:"PUSH WOOSH APPID"});
			//reset badges on app start
			pushNotification.setApplicationIconBadgeNumber(0);
		}
		
		pushNotification.registerDevice(
			function(status) {
					q.resolve(status);
			},
			function(status) {
					q.reject(status);
			}
		);
		return q.promise;
      },

      unregister: function (options) {
        var q = $q.defer();
        $window.plugins.pushNotification.unregister(function (result) {
          q.resolve(result);
        }, function (error) {
          q.reject(error);
        }, options);

        return q.promise;
      },

      // iOS only
      setBadgeNumber: function (number) {
        var q = $q.defer();
        $window.plugins.pushNotification.setApplicationIconBadgeNumber(function (result) {
          q.resolve(result);
        }, function (error) {
          q.reject(error);
        }, number);
        return q.promise;
      }
    };
  }]);

just not sure how you should handle notifications in a factory.`


#11

Good afternoon,

By not create a new topic , I present here my problem that has arisen me with iOS .

I created all certificates using the following URL : https://www.pushwoosh.com/programming-push-notification/ios/ios-additional-platforms/push-notification-sdk-integration-for-phonegap/

Following the response code # 4 of this issue , I created my demo application to test push notifications on Android and iOS with Pushwoosh . Following all the steps to configure the certificates , I think I did well because I accepted everything . My question is whether to take " Sandbox" or " Production" . Compile and run the app and does nothing , nor does crash and works well because the panel Pushwoosh not record the identifier of the mobile .

I tried to remove and add the plugin Pushwoosh , I reviewed all the code and no errors . That if I happen not shown in Xcode results log me … In other apps if I appear.

I think I’ve said pretty much what you need to know but if you need any more information , I will provide.

Any help ?

Thank you very much !


#12

As he could not fix it, I’ve run as browser and firebug I detected an error, missing ‘}’, so I fixed by adding and all sorted. Thank you very much!


#13

@sjerd - Thanks for this. It worked perfectly when I needed information on how to integrate Pushwoosh into my projects. I appreciate your time and effort to publish this tip. :smile:


#14

Hello!

I have a dillema. Since I installed ngCordova for the Calendar plugin, I am wondering if I can use $cordovaPush
instead of Cordova Pushwoosh Push Notifications plugin to work with PushWoosh and also how should the code will look like, if anyone could help me. Maybe @sjerd could reply with an idea of code.

Thank you in advance!


#15

well that question was asked earlier in this topic instead of using $cordovaPush i called it $cordovaPushwoosh and made an untested ngcordova plugin. u should note when trying to use that you should add ‘ngCordova.plugins.pushwoosh’ to the ‘ngCordova.plugins’ list in the file


#16

Kind sir, please can you tell me which variant have you chosen to test and works? Have you tested the ngCordova plugin way?


#17

For everyone interested, here is a more up to date article on the subject and it plays well with Ionic’s Angular way of doing things.


#18

hello friend, could you show the files to deploy my app is finally ?, thanks as


#19

Um i’m not sure i’m understanding your question… what is it you want to do? build a release?


#20

I didn’t take me more than a minute for me to integrate Pushwoosh in my app. It was the easiest I have worked with.