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.
Please check https://github.com/williamrijksen/com.williamrijksen.onesignal/releases to download latest releases of the module.
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="2.1.1">com.williamrijksen.onesignal</module> <module platform="android" version="2.1.1">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="onesignal_app_id" android:value="[App-id]" />
-
To use rich notifications on iOS 10 you need to add an extension to your app. To do so see:
-
Register device for Push Notifications
// This registers your device automatically into OneSignal var onesignal = require('com.williamrijksen.onesignal');
-
On iOS you'll need to request permission to use notifications:
onesignal.promptForPushNotificationsWithUserResponse(function(obj) { alert(JSON.stringify(obj)); });
-
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:
onesignal.getTags(function(e) { if (!e.success) { Ti.API.error("Error: " + e.error); return } Ti.API.info(Ti.Platform.osname === "iphone"? e.results : JSON.parse(e.results)); });
-
Set External User ID:
onesignal.setExternalUserId('your_db_user_id');
-
Remove External User ID:
onesignal.removeExternalUserId();
-
Get if user is subscribed (Boolean):
var subscribed = onesignal.retrieveSubscribed();
-
Get One Signal Player ID (String):
var res = onesignal.retrievePlayerId();
-
Get One Signal Token (String):
var token = onesignal.retrieveToken();
-
Get Permission Subscription State (iOS-only for now):
var res = onesignal.getPermissionSubscriptionState(); /* res example: { "subscriptionStatus": { "userSubscriptionSetting": true, "subscribed": false, "userId": "123-123-123-123-123456789", "pushToken": null }, "permissionStatus": { "status": 2, "provisional": false, "hasPrompted": true }, "emailSubscriptionStatus": { "emailAddress": null, "emailUserId": null } } */
-
postNotification (iOS-only for now):
//You can use idsAvailable for retrieving a playerId onesignal.postNotification({ message:'Titanium test message', playerIds:["00000000-0000-0000-0000-000000000000"] });
-
Set log level (iOS-only for now):
onesignal.setLogLevel({ logLevel: onesignal.LOG_LEVEL_DEBUG, visualLevel: onesignal.LOG_LEVEL_NONE });
-
Opened listener:
The returned content is matching the available payload on OneSignal: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 listener: The returned content is matching the available payload on OneSignal:
onesignal.addEventListener('notificationReceived', function(evt) { console.log(' ***** Received! ' + JSON.stringify(evt)); });
Cheers!
If you already have Titanium installed, skip the first 2 steps, if not let's install Titanium locally.
brew install yarn --without-node
to install yarn without relying on a specific Node version- In the root directory execute
yarn install
- Step into the
ios
directory - If you want to update the OneSignal SDK:
- Run
carthage update
- Drag and drop the
OneSignal.framework
fromCarthage/Build/iOS
toplatform
- Alter the
titanium.xcconfig
to build with the preferred SDK - To build the module execute
rm -rf build && ../node_modules/.bin/ti build -p ios --build-only
brew install yarn --without-node
to install yarn without relying on a specific Node version- In the root directory execute
yarn install
- Step into the android directory
- Copy
build.properties.dist
tobuild.properties
and edit to match your environment - To build the module execute
rm -rf build && mkdir -p build/docs && ../node_modules/.bin/ti build -p android --build-only
Since Titanium 7.x this module relies on https://github.com/appcelerator-modules/ti.playservices
If you still need to support Titanium 6.x and you need to change the used Google Play Services version, execute the following actions:
-
Install the Google Play Services on your system:
sdkmanager "extras;google;m2repository"
-
Fetch the 4 needed *.aar files from the SDK path
extras/google/m2repository/com/google/android/gms
- base
- basement
- gcm
- idd
- location
For the version you want use.
-
Extract the *.aar file, and rename the
classes.jar
togoogle-play-services-<part>.jar
. -
Update the used jars in the
lib
folder. -
Update the res folder with the one from the
google-play-services-basement.jar