FAQ Wednesday: Local Notifications

Share on Facebook0Share on Google+3Tweet about this on TwitterShare on LinkedIn0

It’s Wednesday and time for another FAQ session. Now that we added Local Notifications to Android, I thought it would be a good idea to answer some frequently asked questions on the topic.

1. How do I detect that a local notification occurred.

Local notifications can be detected in one of two ways. If the app is running or suspended and the user taps on the notification, the notification listener will fire in your app. If the application has exited (applicationExit) and a notification fires, tapping on the notification will open the app and set the launchArgs.notification parameter. Your code needs to detect that the notification parameter is set and call the notification listener (it’s won’t be called automatically if the app is started).

Here is how you can detect that your app was started (after closed) from the user touching a notification event.

In the notification listener, the event.applicationState indicates the state of the app at the time the notification was involved by the user. “active” means the notification fired while the app was running. “inactive” means the notification was fired when the app was suspended or closed.

2. How do I detect that a user ignored a local notification?

The answer is you can’t. If the user deletes (or ignores) a notification, the notification listener is never called and your app has no idea that it fired and and was ignored. (Your app could keep track of notifications scheduled and determine if a notification should have fired.)

3. What are the differences between local notifications in iOS and Android?

The main difference between iOS and Android is the “badge.” This is the number next to the app icon on iOS devices that is set when a notification fires. It may be the number of unread messages, events, etc. The value and meaning is defined by the app. Android does not use badge numbers.

4. How do I update the badge number in iOS?

The badge number is set when a notification fires or when the badge is manually updated using the code below. This code reads the current badge value and increments the number.

In your code that sets the local notification you would set the value you want the badge number to be when it fires. Generally you increment the badge number for each notification. To clear the badge number, you call native.setProperty with a badge value of 0.

5. Is there any examples using Local Notifications?

Our latest release of CoronaSDK (build 971) includes a Local Notification sample app. The sample demonstrates many of the features of using local notifications.

That’s it for today’s questions. I hope you enjoyed it and even learned a few things.

Share on Facebook0Share on Google+3Tweet about this on TwitterShare on LinkedIn0

This entry has 2 replies

  1. Alex Poon says:

    Thanks for the tutorial.

    In Walter’s blog post about local notifications, he says “we added something unique to Android — adding your own custom icons to replace Corona’s default notification icon.”

    Question though – how do we specify a custom icon? The documentation at http://docs.coronalabs.com/api/library/system/scheduleNotification.html doesn’t say.

    I really would like to replace the default icon which is a “!” inside a triangle.