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).
local launchArgs = ... -- at the top of your program code local function notificationListener( event ) -- Handle Local Notification here end
Here is how you can detect that your app was started (after closed) from the user touching a notification event.
if launchArgs and launchArgs.notification then -- Need to call the notification listener since it won’t get called if the --the app was already closed. notificationListener( launchArgs.notification ) end
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.
local badge = native.getProperty( "applicationIconBadgeNumber" ) badge = badge + 1 native.setProperty( "applicationIconBadgeNumber", badge )
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.