This tutorial requires you to be running LiveCode 5.5.
Local notifications allow applications to communicate with the user, whether they are running in the foreground, the background or not running at all. The app schedules notifications with the operating system to be delivered to the used at a certain time.
The way in which the notification is delivered depends on the mode in which the application is in at the time the notification is received. If the application is running when the notification is received it can handle the display of the notification as required. If the app is not running then the user is notfied and can choose what action to take, for example the use can choose to dismiss the notification or launch the app.
Our sample stack
In this lesson we will use a sample stack to create, send and cancel local notifications. The stack consists of 2 buttons, for sending and cancelling notifications, 3 fields to specify the contents of the notification and button label, a picker to specify when the notification shouel be sent and a checkbox to specify if the device should make a sound/vibrate when a notification is received.
Creating a local notification
You create a local notification using the mobileCreateLocalNotification command. This command takes up to 6 parameters
alertBody - The text that is to be displayed on the notification dialog that is raised when the application is not running.
The script of the "Send" button is
You can cancel pending notifications using the mobileCancelAllLocalNotifications command. This command cancel all scheduled local notifications.
The script of the "Cancel Pending Notifications" button is
Handling notifications when the app is running
If your app is running when a local notification is sent it receives a localNotificationReceived message, this message is sent with a message parameter. This message is the alertPayload that was specified when the notification was created.
We handle the localNotificationReceived message in the card script of the stack
on localNotificationReceived tMessage
Handling notifications when the app is not running
If your app is not running when a local notification is received the user is alerted and can choose what action to take.
On iOS a dialog box can be launched or the login screen can be opened to inform the user that an application has information for them. Android can create an application icon in the status bar. The user can then decide whether or not to open the application.
If the application is opened as a result of the notification, then LiveCode can handle the localNotificationReceived message as above.