This module gives you the possibility to integrate OneSignal into you're Appcelerator Android or iOS-application. It's even possible to target people by registering tags.
Before setting up the Titanium SDK, you must generate the appropriate credentials for the platform(s) you are releasing on:
- iOS - Generate an iOS Push Certificate
- ANDROID - Generate a Google Server API Key
-
Integrate the module into the
modules
folder and define them into thetiapp.xml
file:<modules> <module platform="iphone" version="1.3.0">com.williamrijksen.onesignal</module> <module platform="android" version="1.3.0">com.williamrijksen.onesignal</module> </modules>
-
Configure your app into the App Settings panel for the right Platform (Android and/or iOS).
-
To use OneSignal on iOS devices, register the OneSignal-appId into
tiapp.xml
:<property name="OneSignal_AppID" type="string">[App-id]</property>
-
To use OneSignal on Android devices, register some meta-data as well:
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> <meta-data android:name="onesignal_app_id" android:value="[App-id]" /> <meta-data android:name="onesignal_google_project_number" android:value="str:[Google project id]" />
-
Register device for Push Notifications
// This registers your device automatically into OneSignal var onesignal = require('com.williamrijksen.onesignal');
-
To add the possibility to target people for notifications, send a tag:
onesignal.sendTag({ key: 'foo', value: 'bar' });
-
Delete tag:
onesignal.deleteTag({ key: 'foo' });
-
Get tags (iOS-only for now):
onesignal.getTags(function(e) { if (!e.success) { Ti.API.error("Error: " + e.error); return } Ti.API.info(e.results); });
-
Set log level (iOS-only for now):
onesignal.setLogLevel({ logLevel: onesignal.LOG_LEVEL_DEBUG, visualLevel: onesignal.LOG_LEVEL_NONE });
-
Receive notifications callback: (does not work on iOS when the app is closed (swiped away). But works fine when the app is running on background) Opened:
onesignal.addEventListener('notificationOpened', function (evt) { alert(evt); if (evt) { var title = ''; var content = ''; var data = {}; if (evt.title) { title = evt.title; } if (evt.body) { content = evt.body; } if (evt.additionalData) { if (Ti.Platform.osname === 'android') { // Android receives it as a JSON string data = JSON.parse(evt.additionalData); } else { data = evt.additionalData; } } } alert("Notification opened! title: " + title + ', content: ' + content + ', data: ' + evt.additionalData); });
-
Received:
onesignal.addEventListener('notificationReceived', function(evt) { console.log(' ***** Received! ' + JSON.stringify(evt)); });
Cheers!