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:
Integrate the module into the modules
folder and define them into the tiapp.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>
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]" />
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 versionyarn install
ios
directorycarthage update
OneSignal.framework
from Carthage/Build/iOS
to platform
titanium.xcconfig
to build with the preferred SDKrm -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 versionyarn install
build.properties.dist
to build.properties
and edit to match your environmentrm -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
For the version you want use.
classes.jar
to google-play-services-<part>.jar
.lib
folder.google-play-services-basement.jar