Phonegap CLI

This guide shows how to make a PhoneGap application (android, iOS and windows phone) supporting Puship push notifications through the Command Line Interface platform.
To support Blackberry platform projects can be made following the CLI guide that can be found on the PhoneGap site and manually adding the Puship plug-in as explained in the Blackberry page on Deprecates Methods.
Before creating the application, it is necessary to have configured correctly the CLI with the SDK of the platforms that have to be supported (a guide for cordova at this link). Otherwise you can use the online build following the guide present on the PhoneGap Online Build page

  1. Browse to the directory where the source code has to be stored and run the following command to create the project:
      cordova create puship com.puship.example Puship
      //The first parameter (puship) defines the project folder name
      //The second parameter (com.puship.example) defines the project package
      //The third parameter (Puship) defines the Application’s name
  2. Go to the folder that has just been created and run the following commands to install the needed plug-ins:
      cordova plugin add cordova-plugin-device
      
      cordova plugin add cordova-plugin-geolocation
      
      cordova plugin add cordova-plugin-console
      
      cordova plugin add cordova-plugin-dialogs
      
      cordova plugin add https://github.com/Puship/PushPlugin
  3. Add the following code in the receivedEvent method present in the js/index.js file:
      var Puship = window.plugins.puship;
      Puship.EnableLog = true;
      Puship.PushipAppId = puship_id; // I.E.: puship_id = "h1mCVGaP9dtGnwG"
      
      if (Puship.Common.GetCurrentOs()==Puship.OS.ANDROID){
      	var GCMCode = your_sender_id; // This is the senderID provided by Google. I.E.: "28654934133"
      	Puship.GCM.Register(GCMCode,
      	{
      		successCallback: function (pushipresult){
      			navigator.notification.alert("device registered with DeviceId:" + pushipresult.DeviceId);
      		},
      		failCallback: function (pushipresult){
      			navigator.notification.alert("error during registration: "+ JSON.stringify(pushipresult));
      		}
      	});
      } else if (Puship.Common.GetCurrentOs()==Puship.OS.IOS){
      	Puship.APNS.Register(
      	{
      		successCallback: function (pushipresult){
      			navigator.notification.alert("device registered with DeviceId:" + pushipresult.DeviceId);
      		},
      		failCallback: function (pushipresult){
      			navigator.notification.alert("error during registration: "+ JSON.stringify(pushipresult));
      		}
      	});
      } else if (Puship.Common.GetCurrentOs()==Puship.OS.WP){
      	Puship.WP.Register(
      	{
      		successCallback: function (pushipresult){
      			navigator.notification.alert("device registered with DeviceId:" + pushipresult.DeviceId);
      		},
      		failCallback: function (pushipresult){
      			navigator.notification.alert("error during registration: "+ JSON.stringify(pushipresult));
      		}
      	});
      } else {
      	Console.log("Not supported platform");
      }
  4. Create a new application in the Puship Manager to get the Puship App ID and replace the value “puship_id” with it in the code above.
  5. Activate the needed notification service following the guide relative to the platform you are working on (Apple, Google Play or Microsoft Store). Note that if you are using Google Play, you will have to modify the js/index.js file, setting the senderID in the GCMCode.
  6. You can modify the project package in www/config.xml and include the row access origin to enable the access to the Puship server:
      <widget 
      	xmlns="http://www.w3.org/ns/widgets" 
      	xmlns:cdv="http://cordova.apache.org/ns/1.0"
      	id="com.puship.example" 
      	version="1.0.0"> <!-- Modify here the package -->
      ...
      <access origin="https://cloudapp.puship.com" /> <!-- Add the following line after the origin -->
  7. Could be necessary to enable the access to Puship server also in file index.html:
      <meta http-equiv="Content-Security-Policy" content="default-src 'self' data: gap: https://ssl.gstatic.com https://cloudapp.puship.com 'unsafe-eval'; style-src 'self' 'unsafe-inline'; media-src *">
  8. Run the build command for add the platforms that is being supported:
      cordova platform add android
      //The platform parameter can be android or ios
  9. Run the build command for all platforms added:
      cordova build
  10. To run the application.
    • Android: Run the following command: cordova run android
    • iOS: Open the project present in the platforms/ios folder using xcode and press the Run button
    • Windows Phone: Run the following command: cordova run wp8

Once the application is running you should be able to see the “device registered”. Now you are able to connect to the Puship Manager and send push notification to your devices!

Pay attention:

  • Push notifications are not supported by the iOS emulator
  • Push notification are supported by the Android emulator but a particular and trickier setup is required so we suggest performing all the tests on an actual device.
  • If you want to add the push notification service to an existing project, start following the guide from the 2nd step


Below it’s available a video that explains how to create an application and activate push notifications with Puship.






Download a complete example using the link below to better understand the whole process
 

 

Adding tags, geolocations management and all the other api are described in the guide common to all the platform that can be consulted at the following link: