1. Home
  2. API Documentation
  3. Setup Cordova/ionic Push notification

Setup Cordova/ionic Push notification

Many of platforms based on Apache Cordova project like PhoneGap, Monaca and ionic [e.g. http://cordova.apache.org/#supported_platforms_section]

so if you use one of this platform you can proceed in this tutorial to implement the push notification in your mobile app

and we will use a third-patry Cordova plugin in our tutorial to enable the push notification in our app

https://github.com/phonegap/phonegap-plugin-push

Installation

 

phonegap plugin add phonegap-plugin-push
cordova plugin add phonegap-plugin-push

Obtaining a device token

//senderID: Get Google senderID from Google console
var push = PushNotification.init({ "android": {"senderID": "12345679"},
"ios": {"alert": "true", "badge": "true", "sound": "true"}, "windows": {} } );

push.on('registration', function(data) {
	var deviceToken = data.registrationId;
	$.ajax({
		"url": "http://pushenvoy.com/app/api/save_device/",
		"dataType": "json",
		"method": "POST",
		"data": {
			"device_token" : deviceToken,
			"device_type" : 'android',
			"channels_id" : '1,2'
		},
		"success": function(response) {
			console.log("Device ID "+deviceToken+" sent successfuly");
		}
	});
});

push.on('notification', function(data) {
	// data.message,
	// data.title,
	// data.count,
	// data.sound,
	// data.image,
	// data.additionalData
	alert(data.message);
});

push.on('error', function(e) {
	console.log("Error");
});

You will need to ensure that you have installed the Android Support Library version 23 or greater, Android Support Repository version 20 or greater, Google Play Services version 27 or greater and Google Repository version 22 or greater.

 

Was this article helpful to you? Yes No

How can we help?