Skip to end of metadata
Go to start of metadata

Introduction

Local notifications alert the user that something is happening to your application while it is in the background. On Android, notifications appear in the notification drawer until the user clears them and on the lock screen for devices running Android 5.0 or greater.  The application can also play a sound or vibrate to alert the user there is a new notification, customize the notification layout from the basic title, text and icon layout; and use the devices default alert settings or override them.  Note that the user can filter or turn notifications on and off from Settings

Lock Screen (Android 5.0 and greater)

Notification Drawer

Configure Notification Settings

Starting with Android 4.1, the user can turn notifications on and off per application, and on devices running Android 5.0 (API 21) and greater, decide how to filter notifications based on the visibility setting of the notification set by the application.

For devices running Android 4.1 or greater, to turn notifications on or off:

  1. Open Settings
  2. Under the Device group, tap Apps.
  3. Tap the application you want to configure notifications for.
  4. Check or uncheck the Show notifications checkbox to turn notifications on or off, respectively.

Additional Android 5.0 and greater options

For devices running Android 5.0 or greater, you can also control notifications from the Sound & notification screen and control the visibility of notifications in the lock screen.

To turn notifications on or off, or to display application notifications when the device is set to priority interruptions only:

  1. Open Settings
  2. Under the Device group, tap Apps.
  3. Tap App notifications.
  4. Tap the application you want to configure notifications for.
  5. Tap Block to turn notifications on or off.
  6. Tap Priority to display notifications when the device is set to priority interruptions only.


To control the visibility of notifications in the lock screen, the user must set a pattern, PIN or password, then the user will be presented the option to filter notifications:

  1. Open Settings
  2. Under the Personal group, tap Security.
  3. Tap Screen lock.
  4. Choose either PatternPIN, or Password.
  5. After you enter a pattern, pin or password, the user can filter notifications in the lock screen:
    1. Select Show all notification content to always display notifications in the lock screen.
    2. Select Hide sensitive notification content to let the application control which notifications to display based on the visibility property.
    3. Select Don't show notifications at all to no display any notification in the lock screen.
  6. Tap Done.

Create a Notification

To create a notification, use the  Titanium.Android.createNotification()  method.  Pass the method a dictionary of properties, which are described below.

Basic layout

By default, a notification contains an icon, title, message, and an optional timestamp and badge number.

Specify the following properties to modify the basic layout of the notification:

  • contentIntent: Intent to execute if the user taps the notification.  By default without an intent attached to the notification, if the user taps on a notification, it does nothing and stays in the notification drawer.  See the Respond to a Notification section for further details.
  • contentText: message displayed in the notification.
  • contentTitle: Title of the notification.
  • icon: Image file to display in the notification. Place the image file in either:
    • a density-specific folder (or use res-nodpi for all densities) in /app/assets/android/images for Alloy project or /Resources/android/images/ for Classic projects, then reference the image file using only the /images path, for example, /images/filename.png.
    • the /platform/android/res/drawable/ folder and reference the image file by removing its extension and prefixing it with the Ti.App.Android.R.drawable namespace, for example, Ti.App.Android.R.drawable.filename.
  • number: Badge number to display in the notification.
  • when: Timestamp to display in the notification.  Does NOT schedule the notification.

For example, the code below creates the notification pictured above.  A blank intent is attached to the notification, so when the user taps the notification, it will be removed from the drawer.  Note that by default, the notification's flags property (see description in the Other customizations section) is set to Titanium.Android.FLAG_AUTO_CANCEL, which automatically clears a notification when the intent is executed.

Sound

To play a sound when the notification first appears, assign the Notification object's sound property a sound file to play. Place the file in the  /platform/android/res/raw/  folder.  Reference the file by using the  Ti.Filesystem.getResRawDirectory() method to retrieve the location of the file, then append the filename to the end, for example,  Ti.Filesystem.getResRawDirectory() +  'sound.wav' .

By default, the sound plays each time the application calls the notify() method.  To override this behavior, add the Titanium.Android.FLAG_ONLY_ALERT_ONCE constant to the Notification object's flags property.

Custom layout

To use a custom layout for a notification:

  1. Create an Android XML Layout and save it to /platform/android/res/layout/.
  2. Create a  Titanium.Android.RemoteViews  object using the XML layout.  Reference the layout file by removing the file extension and prefixing it with the Titanium.App.Android.R.layout namespace.  Once the RemoteViews object is created, you can modify its layout or bind intents by calling other RemoteViews methods on the object.
  3. Assign the RemoteViews object to the Notification object's contentView property.
Icon

If the application calls the setLatestEventInfo() method or sets either the contentTitle or contentText properties, the notification will use the default basic layout even if the contentView property is set.

 

Example

To create the following notification that displays a message with two buttons:

  1. Create an XML layout that uses a LinearLayout to display a TextView and two Button widgets: 

    /platform/android/res/layout/customview.xml
  2. In the application, create a RemoteViews object using the XML layout as a reference, then modify the RemoteViews by changing the default text and binding PendingIntents to the buttons: 

    /Resources/app.js
  3. Finally, assign the RemoteViews object to the Notification object's contentView property. 

    /Resources/app.js

Other customizations

  • defaults: Set to one of the following constants or bitwise-OR the values together to use the device's default notification settings:
    • Titanium.Android.DEFAULT_ALL: Use all of the device's default settings.

    • Titanium.Android.DEFAULT_SOUND: Use the device's default notification sound.

    • Titanium.Android.DEFAULT_VIBRATE: Use the device's default vibrate setting.

    • Titanium.Android.DEFAULT_LIGHTS: Use the device's default LED setting.

  • deleteIntent: Intent to execute if the user taps the clear all notifications button in the notification drawer. 
  • flags: Additional flags to set on the notification to modify its behavior.  Set to one of the following constants or  bitwise -OR the constants together:
    • Titanium.Android.FLAG_AUTO_CANCEL: Clear the notification if the user taps it. Requires that the contentIntent property to be set or else the notification will not be cleared.

    • Titanium.Android.FLAG_INSISTENT: Repeat the sound until the notification is canceled or removed from the notification drawer.

    • Titanium.Android.FLAG_NO_CLEAR : Do not remove the notification from the notification drawer if the user taps the remove all  notification  button.

    • Titanium.Android.FLAG_ONGOING_EVENT: Indicates if the notification is in reference to an ongoing event.

    • Titanium.Android.FLAG_ONLY_ALERT_ONCE: If the notification is present in the notification drawer, do no replay the sound, vibrate or flash the LED again.

    • Titanium.Android.FLAG_SHOW_LIGHTS: Set if you want to use the LED with the notification.  Need to also set the notification's LED properties.  Note that most devices will not allow you to control the LED.

  • priority: set to one of the following constants to determine where the notification is displayed in the drawer.  Requires Titanium SDK 4.0.0 and greater and only works on devices running Android 4.1 and greater.
    • Titanium.Android.PRIORITY_MAX: Use for urgent or time-critical notifications, for example, turn-by-turn directions or emergency alerts.  Notifications will appear at the very top of the notification drawer.

    • Titanium.Android.PRIORITY_HIGH: Use for high priority notifications like real-time chat messages.

    • Titanium.Android.PRIORITY_DEFAULT: Default priority if it does no fit into another priority category.

    • Titanium.Android.PRIORITY_LOW: Use for low priority notifications like software updates.

    • Titanium.Android.PRIORITY_MIN : Use for expired events.  Note that the user will not be alerted to the  notification (sound, vibration, etc.)  and a notification icon will not appear in the status bar, but the notification will appear in the drawer.

  • tickerText: Text to display in the status bar when the notification first appears. This features does not work on devices running Android 5.0 (API 21) and later.

Android 5.0 and greater customizations

The following properties are available for devices running Android 5.0 and greater and requires Titanium SDK 4.0.0 and greater:

  • category: Set to one of the following constants if the notification falls into one of the pre-defined notification categories.  Android uses the category to determine where and how it should be displayed.
    • Titanium.Android.CATEGORY_ALARM

    • Titanium.Android.CATEGORY_CALL

    • Titanium.Android.CATEGORY_EMAIL

    • Titanium.Android.CATEGORY_ERROR

    • Titanium.Android.CATEGORY_EVENT

    • Titanium.Android.CATEGORY_MESSAGE

    • Titanium.Android.CATEGORY_PROGRESS

    • Titanium.Android.CATEGORY_PROMO

    • Titanium.Android.CATEGORY_RECOMMENDATION

    • Titanium.Android.CATEGORY_SERVICE

    • Titanium.Android.CATEGORY_SOCIAL

    • Titanium.Android.CATEGORY_STATUS

    • Titanium.Android.CATEGORY_TRANSPORT

  • visibility: Set to one of the following constants to filter notifications in the lock screen:
    • Titanium.Android.VISIBILITY_PUBLIC: All information is displayed in the lock screen.

    • Titanium.Android.VISIBILITY_PRIVATE: Only the application name and icon appear in the lock screen with the message: "Contents hidden".

    • Titanium.Android.VISIBILITY_SECRET: The notification is not displayed in the lock screen.

Send a Notification

To send a notification, use the  Titanium.Android.NotificationManager.notify()  method.  Pass the method a numeric ID as the first argument to identify it if it needs to be removed and the notification object as the second argument.  This method sends the notification immediately.

 

Update a Notification

To update a notification that is currently being displayed, use the Notification object's  setLatestEventInfo()   method.  Pass the method an updated or the existing title, message and intent.  You need to pass the method all three parameters.

Remove a Notification

If the application needs to remove a notification it sent from the notification drawer, it can either selectively choose which notification(s) to remove or remove all previously sent notifications.

To remove all notifications, call the  Titanium.Android.NotificationManager.cancelAll()  method.

To remove a specific notification, call the  Titanium.Android.NotificationManager.cancel()  method and pass it the ID that was assigned to it by the Titanium.Android.NotificationManager.notify() call.

Respond to a Notification

By default, when a user taps a notification, it does nothing.  In order to perform an action when the user taps the notification, assign a  Titanium.Android.PendingIntent  object to the Notification's contentIntent property.  For more information about Intents, see Android Intents.

For example, if you want to launch the application when the user taps the notification, the application needs to create an intent that launches the main activity of your application.  To create an Intent, use the  Titanium.Android.createIntent()  method and pass the method a dictionary with the following properties:

  • action: Assign it a Titanium.Android.ACTION_* constant which specifies the action to execute.  For this example, assign it  Ti.Android.ACTION_MAIN to launch the main activity.
  • className:  Name of the main activity of the application prefixed with project's application ID (or packageName).  The name of the main activity is the name of the application/project with only the first letter capitalized and Activity appended to the end of it.  For example, if the name of the project is MyApp and the application ID is com.appcelerator.testapp, the class name will be com.appcelerator.testapp.MyappActivity.  You can also find the name of the main activity in the build/android/AndroidManifest.xml file after you build your application.
  • packageName: Project's application ID located in the tiapp.xml file.

After creating an Intent, modify its flags by bitwise-OR-ing the Ti.Android.FLAG_ACTIVITY_SINGLE_TOP and  Ti.Android.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED constants.  These constants tell the intent not to launch the application if it is already open and resets the activity to its initial state if needed, respectively.

Use the intent's addCategory() method to add the Ti.Android.CATEGORY_LAUNCHER category to indicate that the activity is listed in the application launcher.

 Pass the intent to the  Titanium.Android. createPendingIntent()  method to create a PendingIntent, which can be assigned to the Notification's contentIntent property.

Schedule a Notification

To schedule a notification in the future, the application can create an Android service using a JavaScript context.  The service runs in the background at a regular intervals.  The service will need to wait for the appropriate time to create and send the notification allowing the notification to be sent while the application is in the background.  For more information about Android Services, see Android Services.

Icon

The service may stop if the application is killed by either backing out of the application or if the application is removed from the recent apps tray.

To create a service that sends a notification at a specific time:

  1. In a separate JavaScript file, code the creation of the notification.  After the notification is sent, be sure to stop the service since it will no longer be needed and to stop it from sending the same notification again. 

    Resources/ExampleService.js
  2. Add the <services> tag to the <android> section of the tiapp.xml and add a <service> tag as a child of the <services> tag.  In the <service> tag, assign the type attribute to interval to indicate the service runs at regular intervals and the url attribute to the JavaScript file the application needs to run as a service. 

    tiapp.xml
  3. In the main application, create an intent by assigning the JavaScript file to the Intent's url property and start the service by passing the intent to the Titanium.Android.startService () method.  The application needs to set the interval for the service by using the Intent object's  putExtra() method.  You can also use this method to pass in data to the service.  Pass the method the property name as the first argument and a value as the second argument.  To set the service interval, set the property name to interval and the value to how often to call the service in milliseconds. 

    Resources/app.js

Example

In the following example, the main application passes the message and title of the notification to the service as well as when to send it.  The service waits until the right moment to create and send the notification, then stops itself.  Note that the service can only send one notification.  The application can launch more service instances but all of them will be stopped when the first notification is sent. 

Resources/app.js
Resources/ExampleService.js
tiapp.xml

 

Further Reading

Android Developers: Notifications

Titanium.Android.Notification API Reference