Fork us on GitHub

Local Notifications on iOS and Android

We are happy to announce support for local notifications on i...
Post Image

Local Notifications on iOS and Android

We are happy to announce support for local notifications on iOS and Android. Local notifications are similar to push notifications, except that they are initiated locally by the app, rather than remotely. They are useful for communicating information to the user while the app is running in the background, since they manifest themselves as pop-up notifications on supported devices.

Sending Notifications

The process for sending a notification is:

  1. Create a LocalNotification object with the information you want to send in the notification.

  2. Pass the object to Display.scheduleLocalNotification().

Notifications can either be set up as one-time only or as repeating.

Example Sending Notification

LocalNotification n = new LocalNotification();
n.setAlertBody("It's time to take a break and look at me");
n.setAlertTitle("Break Time!");

        System.currentTimeMillis() + 10 * 1000, // fire date/time
        LocalNotification.REPEAT_MINUTE  // Whether to repeat and what frequency

The resulting notification will look like

f7200840 677e 11e5 8fd7 41eb027f8a6c

The above screenshot was taken on the iOS simulator.

Receiving Notifications

The API for receiving/handling local notifications is also similar to push. Your application’s main lifecycle class needs to implement the com.codename1.notifications.LocalNotificationCallback interface which includes a single method:

public void localNotificationReceived(String notificationId)

The notificationId parameter will match the id value of the notification as set using LocalNotification.setId().

Example Receiving Notification

public class BackgroundLocationDemo implements LocalNotificationCallback {

    public void init(Object context) {

    public void start() {


    public void stop() {

    public void destroy() {

    public void localNotificationReceived(String notificationId) {
        System.out.println("Received local notification "+notificationId);
localNotificationReceived() is only called when the user responds to the notification by tapping on the alert. If the user doesn’t opt to click on the notification, then this event handler will never be fired.

Cancelling Notifications

Repeating notifications will continue until they are canceled by the app. You can cancel a single notification by calling:


Where notificationId is the string id that was set for the notification using LocalNotification.setId().

Sample App

You can see a full sample that uses the new local notifications API here.

Share this Post:

Posted by Steve Hannah

Steve writes software for Codename One. He is an open source enthusiast who loves to tinker with new technologies.