With the xtremepush module for Cordova, you can add xtremepush to both iOS and Android apps built with Cordova or PhoneGap or Ionic.
Platform Specific Information
Xtremepush supports push notifications for iOS devices via Apple's Push Notification Service (APNs for short). And push notifications for Android devices via Google’s FCM (Firebase Cloud Messaging for Android) service. This page contains PhoneGap specific instructions but you will need to connect your iOS and Android app to the platform separately.
N.B. To integrate successfully you will need to have your xtremepush App Key, APNS certs for iOS and, Firebase Sender ID and Server Key for Android. These are used to connect your app to the platform and the Apple and Google push services. You will find links to documentation on connecting to the platform below:
- Connecting your app to the platform
- Getting push certs or Server Keys
Android
After you retrieve the Google-Services.json for your app from FCM you should add it into the platforms/android/app directory.
Full Cordova integration guides are given for iOS and Android below.
Installing the Xtremepush Plugin in your Project
- In a terminal navigate to your project directory
- From a terminal navigate to your application directory and add the plugin with the following command:
- cordova plugin add https://github.com/xtremepush/XtremePush-Phonegap
- Now edit your index.js file to register with the xtremepush server on a deviceready event, taking care to switch "APP_KEY" and "FCM_PROJECT_NUMBER" to actual values.
- An orientationchange listener can also be added, to allow rotation of in-app messages, as seen below:
var app = { // Application Constructor initialize: function() { this.bindEvents(); }, // Bind Event Listeners // // Bind any events that are required on startup. Common events are: // 'load', 'deviceready', 'offline', and 'online'. bindEvents: function() { document.addEventListener('deviceready', this.onDeviceReady, false); window.addEventListener('orientationchange', doOnOrientationChange); }, // deviceready Event Handler // // The scope of 'this' is the event. In order to call the 'receivedEvent' // function, we must explicitly call 'app.receivedEvent(...);' onDeviceReady: function() { registerXtremePush(); } }; function registerXtremePush(){ XtremePush.register({ appKey: "APP_KEY", debugLogsEnabled: true, inappMessagingEnabled: true, ios: {
pushPermissionsRequest: true,
//if using locations locationsEnabled: true,
beaconsEnabled: true, locationsPermissionsRequest: true, }, android: {
gcmProjectNumber: "GCM_PROJECT_NUMBER",
//if using locations geoEnabled: true, beaconsEnabled: true } }); } function doOnOrientationChange() { XtremePush.onConfigChanged(); } app.initialize();
You will need to add NSBluetoothPeripheralUsageDescription to your config.xml file under the iOS section. . It is a required usage description you have to add to your app if it includes CoreBluetooth (even if core bluetooth is not actively used). The xtremepush iOS SDK currently requires you to do that. See docs here for more information.
Note: If you are using locations you will need to add the following to your config.xml file under the iOS section:
<platform name="ios">
<allow-intent href="itms:*" />
<allow-intent href="itms-apps:*" />
<config-file parent="NSLocationAlwaysUsageDescription" target="*-Info.plist">
<string>We use Location services to send you relevant information, when and where you need it</string>
</config-file>
<config-file parent="NSLocationAlwaysAndWhenInUseUsageDescription" target="*-Info.plist">
<string>We use Location services to send you relevant information, when and where you need it</string>
</config-file>
<config-file parent="NSLocationWhenInUseUsageDescription" target="*-Info.plist">
<string>We use Location services to send you relevant information, when and where you need it</string>
</config-file>
<config-file parent="NSBluetoothAlwaysUsageDescription" target="*-Info.plist">
<string>We use Bluetooth services to send you relevant information, when and where you need it</string>
</config-file>
</platform>
These are the messages that will show to the user when they are shown the prompt for granting location and bluetooth permission. In the example above we have added placeholder text, but this can be changed to something more relevant which allows you to relate why you are asking your users to grant location access.
At this point, your basic Xtremepush integration is complete. If you build your project now, the app should register with the Xtremepush platform and you should be able to start sending campaigns to your device.
If you have any difficulty in configuring the implementation, a sample app can be built using the following two files as a starting point:
A full list of our JavaScript functions and their parameters can be found in the xtremepush.js plugin file.
Additional Useful Options
Tagging, events and special cases
Tagging and events can then be implemented in your app as follows:
Sample button showing how to hit an event (for triggering campaigns):
<button onclick="XtremePush.hitEvent('test-event')"> Hit Event </button>
Sample button showing how to hit a tag (for analytics/segmentation purposes):
<button onclick="XtremePush.hitTag('test-tag')"> Hit Tag </button>
There are special cases where you may want to send values, such as string or maps, with an event hit. To do that you can use this structure:
Sample button showing how to call hitEventWithValue (Value can be a String or a Map)
<button onclick="hitEventWithValue()"> Hit Event With Value </button>
And in the JS file:
function hitEventWithValue() { //var stringOrMap = {0: "number0", 1: "number1"}; //var stringOrMap = "numbers"; XtremePush.hitEventWithValue('eventName', stringOrMap); }
Callback Functions
messageResponseCallback, deeplinkCallback and inboxBadgeCallback options allow you to link JavaScript functions, defined in the JavaScript files of your project, to the Xtremepush SDK. These functions will then be called by the SDK when particular events happen. After your functions have been created, you can set them with the XtremePush.register() function, using each of the following optional settings:
messageResponseCallback : "onMessageResponse", inboxBadgeCallback: "inboxBadgeCallbackFunction", deeplinkCallback : "onDeeplinkReceived",
They are called to be executed in special cases as explained below:
messageResponseCallback
A basic implementation is shown below.
The response.type variable shown above can be either present | click | dismiss
- present - when a push notification is received and the app is in the foreground
- click - when a message is clicked
- dismiss - when a message is dismissed
Below is a complete example of custom notification handling. It does multiple things:
- shows a custom dialog when a push message is received in the foreground
- opens an article page when a message is clicked
- saves information when one of the message buttons is clicked
inboxBadgeCallback
happens when the badge number updates. The badge number is checked when:
- New push message arrives
- Application is started
- Inbox window is closed
An example is shown below
deeplinkCallback
happens when a message with the click action 'Go to deeplink' is clicked.
An example is shown below:
Foreground Notification Handling
The default behaviour on both iOS and Android is to show OS style notification. But this can be changed, by adding the payload with key 'foreground' and value 'false' to your campaign.
Now when the push is received, should the app be in the foreground, the notification will not be displayed.
Advanced Options
Attributions
If you would like xtremepush to collect IDFA/Ad ID and attribution data in your app, we have a git branch of the plugin that copies the required frameworks into your app. This can be installed using the following command:
cordova plugin add https://github.com/xtremepush/XtremePush-Phonegap#master+attributions
To enable the collection functionality, the attributionsEnabled parameter in the XtremePush.register() function must also be set with a value of true.
Remove Location Services
If you would like to remove the geo-location and beacon frameworks and services from your app, please use the following command when installing the plugin:
cordova plugin add https://github.com/xtremepush/XtremePush-Phonegap#master-geo-beacon
Inbox
The inbox feature can be turned on in your app by adding the following to your XtremePush.register() function:
inboxEnabled: true
Here is a sample button showing how to open inbox
<button onclick="XtremePush.openInbox()">Open Inbox</button>
If you want to use the inbox badge functionality please use the following in your XtremePush.register() function:
inboxBadgeCallback: "onInboxBadgeUpdate"
Here is a sample button showing how to retrieve the latest badge number:
<button onclick="XtremePush.getInboxBadge()">Get Inbox Badge</button>
Other
For any further customisation of the plugin, please feel free to fork the GitHub repository and make any changes you like in your forked version, before adding the plugin to your app.
Comments
0 comments
Article is closed for comments.