Provides support for Android push notifications.
The CloudPush module supports Android push notifications for a device by providing a connection to the push notification server to the device to receive push notifications and to optionally present a tray notification to the user when a new push notification is received.
This module is only used to receive push notifications. For information on configuring your project to send push notifications, see Modules.Cloud.PushNotifications.
As of Release 3.2.0, the CloudPush module only supports Google Cloud Messaging (GCM) for push notifications. MQTT support is deprecated and support was removed in Release 3.2.0. Upgrade your application to use the GCM protocol.
GCM supports devices that run Android 2.2 and later, and requires that the Google Play Store application be installed. For pre-4.0 devices, the user is required to set up their Google account.
To use the CloudPush module in your JavaScript code, you need to require it in and
get a device token with the retrieveDeviceToken
method to enable push notifications with Arrow Push.
In your project's tiapp.xml
, you need to add some keys to configure push notifications.
Note that this module is not included in the Titanium namespace, but it is bundled with the Titanium SDK as of version 2.0.0. To use it, you must require it, like this:
var CloudPush = require('ti.cloudpush');
Then, call the retrieveDeviceToken
method to get a unique token specific to that device and
Arrow DB application. This token is used with Arrow PushNotifications calls
to subscribe and unsubscribe the device to push notification channels.
Once the device is subscribed to at least one push channel, listen to the module's callback
,
trayClickLaunchedApp
, and trayClickFocusedApp
events to monitor for incoming push notifications.
This module must also be added to the modules section in your tiapp.xml. This can be done using the Modules list in the Titanium Studio TiApp Editor, or by editing the XML directly and adding the following line to the modules element:
<module platform="android">ti.cloudpush</module>
Your app must prove that it is allowed to talk to Arrow. This keeps your data secure by preventing anyone from making requests to Arrow that impersonate your app.
Titanium Studio creates a pair of keys (Development and Production) for each Titanium application depending on the
user preference specified during new project creation. This pair of keys will be stored in tiapp.xml
and one of the
keys will be used during application build depending on the build type (development or production).
To use push notifications, in the tiapp.xml
file, you need to specify the Arrow Push keys and push
type. The supported properties in tiapp.xml
are:
<!-- Property keys for Arrow Push (required) -->
<property name="acs-api-key-development" type="string">YOUR DEVELOPMENT API KEY HERE</property>
<property name="acs-api-key-production" type="string">YOUR PRODUCTION API KEY HERE</property>
<property name="acs-api-key" type="string">YOUR API KEY HERE</property>
If a deployment-specific setting is provided (production or development) then that value will be used for the current deployment environment.
WARNING: The default properties of this module are used until you set a property for the first time. Because the properties are persisted to the device settings (via Titanium.App.Properties), the most recent value you set will always be used.
If you are using a virtual private cloud (VPC), you need to configure your Arrow Push dispatcher
URL in order to send push notifications with GCM. In the tiapp.xml
file, add the acs-push-api-url
application property and set the node text to the push dispatcher URL provided to you. The URL may
be the same as your custom ArrowDB endpoint and have deployment-specific settings, that is, two
URLs--one for production and another for the development environment.
<ti:app>
<property name="acs-push-api-url-production">https://api-prod.fooinc.com</property>
<property name="acs-push-api-url-development">https://api-dev.fooinc.com</property>
</ti:app>
If the device receives multiple push notifications from the application, Android can group these notifications together as one notification.
To use grouped notifications, add a grouped notification template to either the tiapp.xml
file
or the i18n
folder for internationalized versions:
tiapp.xml
:
<property name="acs-grouped-notification-message-development" type="string">You have $number$ unread messages.</property>
<property name="acs-grouped-notification-message-production" type="string">You have $number$ unread messages.</property>
-or-
<property name="acs-grouped-notification-message" type="string">You have $number$ unread messages.</property>
i18n/es/strings.xml
(example for Spanish):
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="acs_grouped_notification_message">Tienes $number$ mensajes no leidos.</string>
</resources>
The $number$
variable indicates the number of unread messages.
This example lets the application retrieve the device token and listens for several events to monitor incoming push notifications.
var CloudPush = require('ti.cloudpush');
CloudPush.retrieveDeviceToken({
success: function deviceTokenSuccess(e) {
// Use this device token with Ti.Cloud.PushNotifications calls
// to subscribe and unsubscribe to push notification channels
Ti.API.info('Device Token: ' + e.deviceToken);
},
error: function deviceTokenError(e) {
alert('Failed to register for push! ' + e.error);
}
});
// These events monitor incoming push notifications
CloudPush.addEventListener('callback', function (evt) {
alert(evt.payload);
});
CloudPush.addEventListener('trayClickLaunchedApp', function (evt) {
Ti.API.info('Tray Click Launched App (app was not running)');
});
CloudPush.addEventListener('trayClickFocusedApp', function (evt) {
Ti.API.info('Tray Click Focused App (app was already running)');
});
Code returned from isGooglePlayServicesAvailable. Google Play services has been disabled on this device.
Code returned from isGooglePlayServicesAvailable. The version of Google Play services installed on this device is not authentic.
Code returned from isGooglePlayServicesAvailable. Google Play services is not installed on the device.
Code returned from isGooglePlayServicesAvailable. Google Play services is out of date.
Code returned from isGooglePlayServicesAvailable. Google Play services is available, and the connection is successful.
The name of the API that this proxy corresponds to.
The name of the API that this proxy corresponds to.
The value of this property is the fully qualified name of the API. For example, Button
returns Ti.UI.Button
.
Indicates if the proxy will bubble an event to its parent.
Some proxies (most commonly views) have a relationship to other proxies, often established by the add() method. For example, for a button added to a window, a click event on the button would bubble up to the window. Other common parents are table sections to their rows, table views to their sections, and scrollable views to their views. Set this property to false to disable the bubbling to the proxy's parent.
Default: true
Whether or not this device will receive push notifications.
deprecated
3.2.0 This property was only required to enable push notifications for the legacy MQTT protocol.
Default: false
Whether or not your application is brought to the foreground whenever a new push is received.
Note that this behavior is rather disruptive to users, and is strongly discouraged.
Default: false
The Window or TabGroup whose Activity lifecycle should be triggered on the proxy.
The Window or TabGroup whose Activity lifecycle should be triggered on the proxy.
If this property is set to a Window or TabGroup, then the corresponding Activity lifecycle event callbacks will also be called on the proxy. Proxies that require the activity lifecycle will need this property set to the appropriate containing Window or TabGroup.
Whether or not clicking the tray notification will bring your application to the foreground.
This is only applicable if you have set showTrayNotification to true.
Default: true
Whether or not to show a tray notification when a new push is received.
If your payload is only a string, it will be used as the contentText and tickerText, and your application's name will be used as the contentTitle with a system icon.
Note that in your payload, you can customize this tray notification using any of the properties of a Titanium.Android.Notification, except for contentIntent and deleteIntent (those are automatically set).
Default: true
Whether or not to show tray notifications when your application is in the foreground.
Instead of showing a notification, the callback event will be immediately fired instead.
This is only applicable if you have set showTrayNotification to true.
Default: false
Set to true to trigger a single callback for the selected push notification when multiple push notifications are displayed in the tray.
In previous versions of the module, clicking on one of the application's push notifications triggered the callbacks for all of the application's push notifications. The new module retains the same behavior by default.
Set this property to true to receive a single callback for the push notification selected by the user, regardless of how many push notifications are displayed in the tray.
Default: false
Adds the specified callback as an event listener for the named event.
Name of the event.
Callback function to invoke when the event is fired.
Applies the properties to the proxy.
Properties are supplied as a dictionary. Each key-value pair in the object is applied to the proxy such that myproxy[key] = value.
A dictionary of properties to apply.
Clears the CloudPush module's stored status, including the push type, device token, GCM sender ID, etc.
This method is useful to transfer the push type from MQTT to GCM, or changing the GCM sender ID when needed.
After calling this method, the CloudPush module is reinitialized. Use retrieveDeviceToken
to
get the device token again.
Fires a synthesized event to any registered listeners.
Name of the event.
A dictionary of keys and values to add to the Titanium.Event object sent to the listeners.
Gets the value of the enabled property.
deprecated
3.2.0 This property was only required to enable push notifications for the legacy MQTT protocol.
Returns a code to indicate whether Google Play Services is available on the device.
One of the following status codes: <Modules.CloudPush.SUCCESS>, <Modules.CloudPush.SERVICE_MISSING>, <Modules.CloudPush.SERVICE_VERSION_UPDATE_REQUIRED>, <Modules.CloudPush.SERVICE_DISABLED> or <Modules.CloudPush.SERVICE_INVALID>.
Removes the specified callback as an event listener for the named event.
Multiple listeners can be registered for the same event, so the
callback
parameter is used to determine which listener to remove.
When adding a listener, you must save a reference to the callback function in order to remove the listener later:
var listener = function() { Ti.API.info("Event listener called."); }
window.addEventListener('click', listener);
To remove the listener, pass in a reference to the callback function:
window.removeEventListener('click', listener);
Name of the event.
Callback function to remove. Must be the same function passed to addEventListener
.
Asynchronously retrieves the application specific device token.
This token is used in calls to Appcelerator Cloud Services to subscribe or unsubscribe to push notification channels. This token is unique to each application and device.
Dictionary specifying token retrieval related options.
Sets the value of the bubbleParent property.
New value for the property.
Sets the value of the enabled property.
deprecated
3.2.0 This property was only required to enable push notifications for the legacy MQTT protocol.
New value for the property.
Sets the value of the focusAppOnPush property.
New value for the property.
Sets the value of the lifecycleContainer property.
New value for the property.
Sets the value of the showAppOnTrayClick property.
New value for the property.
Sets the value of the showTrayNotification property.
New value for the property.
Sets the value of the showTrayNotificationsWhenFocused property.
New value for the property.
Sets the value of the singleCallback property.
New value for the property.
Fired whenever a push notification is received.
If your application is not running when a push is received, the push will be saved in a queue until the next time you add an event listener for this.
This will only be fired once per push notification.
This event will be fired at different times depending on your settings and the application's present state (foreground, background, or not running at all). When focusAppOnPush is true, this will fire as soon as you receive a push. When showTrayNotification is true, this will fire as soon as the user touches the tray notification to focus your application. Or, if showTrayNotificationsWhenFocused is false, and your application is in the foreground, it will be called right away. If both focusAppOnPush and showTrayNotification are false, this will fire the next time your application is launched and you add an event listener for it.
You can use the trayClickLaunchedApp and trayClickFocusedApp events to distinguish between the various ways a push notification can return a user to your application.
A JSON string of your data payload. Use JSON.parse to turn this into an object you can use.
Source object that fired the event.
Name of the event fired.
True if the event will try to bubble up if possible.
Set to true to stop the event from bubbling.
Fired when a tray notification is shown and the application is already running.
Touching it focuses the app, and fires this event.
This is only applicable if you have set showTrayNotification to true.
A JSON string of your data payload. Use JSON.parse to turn this into an object you can use.
Source object that fired the event.
Name of the event fired.
True if the event will try to bubble up if possible.
Set to true to stop the event from bubbling.
Fired when a tray notification is shown and the application is not running.
Touching it launches the app, and fires this event.
This is only applicable if you have set showTrayNotification to true.
A JSON string of your data payload. Use JSON.parse to turn this into an object you can use.
Source object that fired the event.
Name of the event fired.
True if the event will try to bubble up if possible.
Set to true to stop the event from bubbling.