Cordova CLI (EXAMPLE)

This guide shows how to make a Cordova application (android, iOS and windows) supporting Puship push notifications through the Cordova Command Line Interface platform.
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).

  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
  1. Go to the folder that has just been created and run the following commands to install the needed plug-in:
cordova plugin add puship-plugin
  1. Add the following code in the onDeviceReady method present in the js/index.js file:
onDeviceReady: function() {
    app.receivedEvent('deviceready');
    
    const push = PushNotification.init({
    	android: {
    	},
        browser: {
            pushServiceURL: 'http://push.api.phonegap.com/v1/push'
        },
    	ios: {
    		alert: "true",
    		badge: "true",
    		sound: "true"
    	},
    	windows: {}
    });
    
    push.on('registration', (data) => {
    	//alert("registrationid: " + data.registrationId);
    	
    	//CODE TO ADD
    	//Puship.EnableLog(true);
    	Puship.Register(
        	data.registrationId,
        	'YOUR-PUSHIP-APP-CODE',
        	{
        		successCallback: function (pushipresult) {
        			alert("device registered with DeviceId:" + pushipresult.DeviceId());
        		},
        		failCallback: function (pushipresult) {
        			alert("error during registration: "+ JSON.stringify(pushipresult));
        		}
        	}
        );
    
    });
    
    push.on('notification', (data) => {
        alert(JSON.stringify(data));
    	// data.message,
    	// data.title,
    	// data.count,
    	// data.sound,
    	// data.image,
    	// data.additionalData
    });
    
    push.on('error', (e) => {
    	// e.message
    });
},
  1. Follow the guide at this link for create a new application in the Puship Manager to get the Puship App ID and replace the value “YOUR-PUSHIP-APP-CODE” with it in the code above.
  2. Activate the needed notification service following the guide relative to the platform you are working on (Apple, Google Play or Microsoft Store).
  1. Run the build command for add the platforms that is being supported:
cordova platform add android
//The platform parameter can be android or ios
  1. Run the build command for all platforms added:
cordova build
  1. To run the application
    • Android: Run the following command: cordova run android
    • iOS: Run the following command: cordova run ios
    • Windows: Run the following command: cordova run windows

Once the application is running you should be able to see the “device registered with…”. 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 are two videos that explain step by step how to create an application and activate push notifications with Puship for Android and for iOS:



Below there is an example of a project already configured. In that project there are various examples of how to using the Puship method. If you prefer you can just clone it and start from this one

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