This module gives you the possibility to integrate OneSignal into you're Appcelerator Android app. It's even possible to target people by registering tags, send InApp Messages, register a external user id, prompt the user for location permission and a bit more.
Before setting up the Titanium SDK, you must generate the appropriate credentials for the platform(s) you are releasing on:
- ANDROID - Generate a Google Server API Key
- Titanium SDK 9.0.0+
Version 2.1.0
- Titanium SDK 8.0.0+
Version 1.8.3 uses FCM and needs:
- Firebase Core Module
- Firebase Cloud Messaging Module
If you don't want FCM, but GCM, use 1.8.2 instead.
-
Integrate the module into the
modules
folder and define them into thetiapp.xml
file:<modules> <module platform="android">ti.android.onesignal</module> </modules>
-
Configure your app into the App Settings panel for the right Platform (Android and/or iOS).
-
To use OneSignal on Android devices, register some meta-data as well:
<meta-data android:name="onesignal_app_id" android:value="[App-id]" tools:replace="android:value"/> <meta-data android:name="onesignal_google_project_number" android:value="str:[Google-Project-Number]" tools:replace="android:value"/>
-
Register device for Push Notifications
// This registers your device automatically into OneSignal var onesignal = require('ti.android.onesignal');
-
You can request permission to use notifications:
onesignal.promptForPushNotificationsWithUserResponse(function(obj) { alert(JSON.stringify(obj)); });
-
SetLocationShared Disable or enable location collection (defaults to enabled if your app has location permission).
onesignal.setLocationShared(false);
-
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)); });
-
SetExternalUserId: If your system assigns unique identifiers to users, it can be annoying to have to also remember their OneSignal user ID's as well. To make things easier, OneSignal now allows you to set an external_id for your users. Simply call this method, pass in your custom user ID (as a string), and from now on when you send a push notification, you can use include_external_user_ids instead of include_player_ids.
onesignal.setExternalUserId("user_153321568");
-
RemoveExternalUserId: If your user logs out of your app and you would like to disassociate their custom user ID from your system with their OneSignal user ID, you will want to call this method.
onesignal.removeExternalUserId();
-
SetSubscription: You can call this method with false to opt users out of receiving all notifications through OneSignal. You can pass true later to opt users back into notifications.
onesignal.setSubscription(true);
-
IdsAvailable:
onesignal.idsAvailable(function(e) { //pushToken will be nil if the user did not accept push notifications alert(e); });
-
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) { 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) { Titanium.API.info(' ***** Received! ' + JSON.stringify(evt)); });
- Subscription Changed listener:
The
subscriptionChanged
method will be fired on the passed-in object when a notification subscription property changes. This includes the following events:- Getting a Registration Id (push token) from Google
- Getting a player / user id from OneSignal
OneSignal.setSubscription
is called- User disables or enables notifications
onesignal.addEventListener('subscriptionChanged', function(evt) { Titanium.API.info(' OneSignal ***** subscriptionChanged!' + JSON.stringify(evt)); //var canNotify = evt.to.subscribed; });
- Button Clicked listener:
If you add buttons to your notification, you listen to their click events.
onesignal.addEventListener('notificationButtonClicked', function(evt) { Titanium.API.info(' OneSignal ***** Button Clicked!'); Titanium.API.info(evt.button_id); });
- Sending InApp Messages: In-App Messages are highly customizable pop-up modals that any mobile app user (subscribed or unsubscribed) can receive when they are inside your app. The link below will teach you how to use it.
-
InApp Message Clicked listener: The returned payload is available on OneSignal:
onesignal.addEventListener('inAppMessageClicked', function(evt) { /* evt.clickName //An optional click name defined for the action element. `null` if not set evt.clickUrl //An optional URL that opens when the action takes place. `null` if not set. evt.firstClick //`true` if this is the first time the user has pressed any action on the In App Message. evt.closesMessage //Did the action close the In App Message. `true` the In App Message will animate off the screen. `false` the In App Message will stay on screen until the user dismisses. */ console.log(' ***** InApp Clicked! ' + JSON.stringify(evt)); });
-
Add Trigger: Add a trigger, may show an In-App Message if its triggers conditions were met.
OneSignal.addTrigger("level", 5);
-
Add Triggers: Add a object of triggers, may show an In-App Message if its triggers conditions were met.
OneSignal.addTriggers( {"level", 5}, {"page", 8}, {"tutorial", 11} );
-
Remove Triggers for key: Removes a single trigger for the given key, may show an In-App Message if its triggers conditions were met.
OneSignal.removeTriggerForKey("level");
-
Remove Triggers for keys: Removes a list of triggers based on a collection of keys, may show an In-App Message if its triggers conditions were met.
OneSignal.removeTriggerForKeys(["level","page","tutorial"]);
-
Remove Triggers for keys: Gets a trigger value for a provided trigger key.
var trigger = OneSignal.getTriggerValueForKey("level"); console.log(trigger.object); //Returns a single trigger value for the given key, if it exists, otherwise returns `null`
-
Pause InApp Messages: Allows you to temporarily pause all In App Messages. You may want to do this while the user is watching a video playing a match in your game to make sure they don't get interrupted at a bad time.
OneSignal.pauseInAppMessages(true);
-
Enable Vibrate: By default OneSignal always vibrates the device when a notification is displayed unless the device is in a total silent mode. Passing false means that the device will only vibrate lightly when the device is in it's vibrate only mode. You can link this action to a UI button to give your user a vibration option for your notifications.
OneSignal.enableVibrate(true);
-
Enable Sound: By default OneSignal plays the system's default notification sound when the device's notification system volume is turned on. Passing false means that the device will only vibrate unless the device is set to a total silent mode. You can link this action to a UI button to give your user a different sound option for your notifications.
OneSignal.enableSound(true);
- Setting the user email:
setEmail
allows you to set the user's email address with the OneSignal SDK.OneSignal.setEmail("[email protected]");
- Email Update Success Event
onesignal.addEventListener('emailUpdatedSuccess', function(evt) { console.log(evt.success); console.log(evt.email); });
- Email Update Error Event
onesignal.addEventListener('emailUpdatedError', function(evt) { console.log(evt.error); console.log(evt.message); console.log(evt.email); });
- logoutEmail
If your app implements logout functionality, you can call
logoutEmail
to dissociate the email from the device:OneSignal.logoutEmail();
- Email Subscription Changed Event
Tracks changes to email subscriptions (ie. the user sets their email or logs out). In order to subscribe to email subscription changes you can implement the following:
onesignal.addEventListener('emailSubscriptionChanged', function(evt) { console.log(evt); });
Cheers!
- Duplicate
firebase-iid-interop
regarding Firebase Core and Firebase Cloud Messaging:- Just delete the
firebase-iid-interop-16.0.1.aar
from Firebase Cloud Messaging lib folder.
- Just delete the
- if you import the
<module platform="android">ti.playservices</module>
or any module that imports it, you will see this message on the console[WARN] OneSignal: Both GCM & FCM Libraries detected! Please remove the deprecated GCM library.
- Don't worry. There nothing to do here, just move on. :)
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