livecode.com
  • Home
  • Download
  • Developers
  • Academy
  • Community
  • Store
  • Support
  • About
LiveCode Lessons » How To - LiveCode Mobile Tasks » Using local notifications

Topics

  • Getting Started with Android 3
    • How do I Become an Android Developer on a PC?
    • How do I Become an Android Developer on a Mac?
    • The Basics: How do I Create Hello World on Android?
  • Getting Started with iOS 2
    • How do I Become an iOS Developer?
    • How do I use Core Location in revMobile
  • LiveCode Mobile Tasks 22
    • How do I Develop Cross-Platform in LiveCode?
    • How do I send an email from my mobile device?
    • How do I use multi-touch to move more than one object?
    • How do I use the Question and Password Dialogues in LiveCode Mobile?
    • How do I Capture Images in LiveCode Mobile?
    • How do I detect a shaking motion using LiveCode mobile?
    • LiveCode Mobile Video How-To's - Shake
    • How do I implement a multi-touch pinch motion?
    • LiveCode Mobile Video How-To's - Multi-touch
    • How do I play a video in part of the screen in iOS
    • Displaying Assets On Differing Screen Resolutions
    • How to create and use an SQLite database
    • How do I get an image from my mobile photo library
    • How do I implement in-app purchases in LiveCode?
    • Accessing Facebook Api's using LiveCode
    • Mobile Orientations
    • Using local notifications
    • Using custom URL schemes
    • Using multi-channel audio on mobile
    • How do I get the Location and use the Digital Compass?
    • How do I use Ads in LiveCode?
    • Creating a native scroller to scroll a field
  • iOS Tasks 19
    • How do I build an iPhone application for iOS?
    • How do I Configure the Status Bar in iOS?
    • How do I make a phone call on the iPhone?
    • How do I use Native Text Controls on iOS
    • How do I use the Browser Control?
    • How do I read/write to files in iOS?
    • How do I Send HTML E-Mails with Attachments in iOS?
    • Installing custom fonts on iOS
    • How do I use the Question and Password Dialogues in iOS?
    • How do I play sounds on an iOS device?
    • How do I use the Picker View on an iPhone?
    • How do I Access Maps on iOS?
    • Creating a simple stock control application for the iPad
    • How do I Create a Distribution Profile for iOS?
    • How do I Submit an iOS App to the App Store?
    • How do I set up an App for Submission to iTunes Connect?
    • How do I use the Date Picker View on an iPhone?
    • How do I use Push Notifications with iOS?
    • How do I Develop iOS Code for Standard and Retina Devices?
  • Android Tasks 4
    • Using the hardware "back" button on Android
    • How do I Create an Android App for Distribution?
    • How do I Create a Self-Signed Certificate for an Android App?
    • How do I use Push Notifications with Android?

Last Updated

Mar 20, 2012

Other Resources

  • Getting Started with LiveCode

  • Get Up and Running with LiveCode
  • Getting Started with LiveCode Development
  • LiveCode Concepts

  • Features, concepts and aspects of LiveCode
  • LiveCode Lessons

  • How To - Step-By-Step Guides To Tasks In LiveCode
  • How To - LiveCode Server Tasks
  • How To - LiveCode Mobile Tasks
  • How To - LiveCode Sample Scripts
  • How to - LiveCode Marketplace Products
  • How to Purchase and License LiveCode
  • Tutorials

  • Creating a Video Library Application
  • Data Grid

  • LiveCode Data Grid
  • Data Grid Tips & Tricks
  • Converting the Stock Program

Comments

0 for this lesson

  • Prev: Mobile Orientations
  • Next: Using custom URL schemes

Using local notifications

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.

Attached Files

  • LocalNotifications.livecode

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.
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

on mouseUp
    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
end mouseUp

Cancelling notifications

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

on mouseUp
    mobileCancelAllLocalNotifications
end mouseUp

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"
end localNotificationReceived

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.

  • Prev: Mobile Orientations
  • Next: Using custom URL schemes

Comments (0)

Add your comment

E-Mail me when someone replies to this comment
Brought to you by RunRev Ltd, Registered in Scotland, No. SC200728