Using local notifications
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 should 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.
alertButtonMessage - The button text on the notification dialog that is to appear on the button that launches the application, when the application is not running.
alertPayload - A text payload that can be sent with the notification request. This payload is presented to the user via the localNotificationReceived message.
alertTime - The time at which the alert is to be sent to the application. This parameter needs to be set in seconds and is the number of seconds since the UNIX Epoch, at which the notification should be triggered.
playSound - A boolean to indicate if a sound is to be played when the alert is received.
badgeValue - The number value to which the badge of the application logo is to be set. 0 hides the badge. >0 displays the value on the badge (optional)
The script of the "Send" button is
local tCurrentTime, tNotificationTime, tAlertBody, tAlertButton, tAlertMessage, tSound
put the seconds into tCurrentTime
put field "alertbody" into tAlertBody
put field "alertbutton" into tAlertButton
put field "message" into tAlertMessage
put tCurrentTime + word 1 of field "seconds" into tNotificationTime
put the hilite of button "sound" into tSound
mobileCreateLocalNotification tAlertBody, tAlertButton, tAlertMessage, tNotificationTime, tSound
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
answer "Local Notification:" && quote & tMessage & quote with "Okay"
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.
Is it possible to schedule something else to execute in the background? I have an app and the user is requesting the "resend" of data to the server if the first attempt failed and the user closes the app. If that is possible, how would I set that up?
sorry, it is not possible to implement a resend feature in LiveCode when the application has been closed. LiveCode has to be running and the front application in order to perform operations.
Is it possible to read/react to notifications from the Android OS or other applications? Thanks.
Notifications can be sent in windows?
At the moment local notifications are only available on iOS and Android. There is a request in our Quality Control Center about Windows Notifications, you could add your email address to the CC list to receive updates when the status of the report is changed.