Use the following steps to enable cross-platform push notifications on your app:
pushnotifications
.$ cordova create ~/the/path/to/your/source/pushnotifications com.gnstudio.pg.pushnotifications PushNotifications
$ cd pushnotifications $ cordova platforms add android $ cordova platforms add ios
$ git clone https: //github.com/phonegap-build/PushPlugin.git
platforms/ios
and drag-and-drop into the plugins
folder available in the Xcode Project Navigator the following files: AppDelegate+notification.h
, AppDelegate+notification.m
, PushPlugin.h
, PushPlugin.m
config.xml
file you find in the root of the Xcode project and add an entry to enable the plugin.<plugin name='PushPlugin' value='PushPlugin' />
platforms/android/src
and then copy to it the files you find in the src/android
folder of the plugin repository.res/xml
folder, open the config.xml
file, and add the plugin to the Android project configuration.<plugin name='PushPlugin' value='com.plugin.GCM.PushPlugin' />
PushNotification.js
file you find in the www
folder of the plugin repository to your project www
folder.$ cordova build
www
folder, open the index.html
file, and add a ul
tag with the class logs
inside the main div
of the app, below the #deviceready
one.<ul class='logs'></ul>
script
tag needed to embed the JavaScript interface of the plugin.<script type='text/javascript' src='PushNotification.js'></script>
www/js
folder, open the index.js
file, and add in the body of the deviceready
function the script needed to initialize the push notifications on Android and iOS.var pushNotification = window.plugins.pushNotification; if (device.platform == 'android' || device.platform == 'Android') { pushNotification.register(app.successHandler, app.errorHandler, {'senderID':'570783355289', 'ecb': 'app.onNotificationGCM'}); } else { pushNotification.register(app.tokenHandler,app.errorHandler, {'badge': 'true', 'sound':'true',"alert":'true','ecb':'app.onNotificationAPN'}); }
addLogs
in order to append the logs to the device screen.addLogs: function(message, data){ var logs = document.querySelector('.logs'), var log = document.createElement('li'), log.innerHTML = message + data; logs.appendChild(log); }
successHandler
function to be used in Android to notify the user about the successful registration of the device to the Google Cloud Messaging service.successHandler: function (result){ navigator.notification.vibrate(300); app.addLogs('success: ', result); }
tokenHandler
function to be used in iOS to notify the user about the successful registration to the Apple Push Notification service.tokenHandler: function (result) { navigator.notification.vibrate(300);app.addLogs('token: ', result); }
errorHandler
function used on both platforms that logs an eventual error.errorHandler: function (error) { navigator.notification.vibrate(300); app.addLogs('error: ', error); }
onNotificationAPN
function that will be executed in iOS each time a push notification has been received.onNotificationAPN: function (evt) { if (evt.alert) { navigator.notification.vibrate(300); app.addLogs('EVENT -> RECEIVED: ', evt.alert); } }
onNotificationGCM
function that will be executed in iOS each time a push notification has been received.onNotificationGCM: function (evt) { navigator.notification.vibrate(300); app.addLogs('EVENT -> RECEIVED: ', evt.event); }
Example/server
folder of the plugin repository and open the files pushGCM.rb
and pushAPNS.rb
in a text editor. In the pushGCM.rb
file, add the API key you created with Google Cloud Messaging and the device registration ID. In the pushAPNS.rb
file, add the developer certificate password and the path to the certificate.pushmeup
.$ gem install pushmeup
$ ruby pushGCM.rb $ ruby pushAPNS.rb
You developed an app able to react to a push notification on iOS and Android, and your device should have received another notification when the app is paused.
Q1. How can you write a plugin for multiple platforms?
Q2. Is a Plugin asynchronous?
Create a sample app that is able to send SMSes using the SMS-plugin you can find at https://github.com/phonegap/phonegap-plugins for Android and iOS. When installing the plugin, check whether there are compatibility issues with your current version of PhoneGap and eventually fix them.
3.149.243.32