HowTo: Using LocalNotifications external object in Smart DevicesOfficial Content

Local Notifications enable your app to alert users of scheduled events or alarms in the background, with no servers required as in Push Notifications in Smart Devices Application.

This document is a brief guide of how to use Local Notifications in GeneXus for Smart Devices.

Step 1: Checking the objects that will let me add Local Notifications

Check out the Smart Device API folder in your Knowledge Base KB Explorer.

There are two objects that are going to be used to enable Local Notifications.

LocalNotifIphone4

a. SDTLocal Notifications: 

This Structured Data Type (SDT) is used to define the configuration of the Local Notification. Take a look at the structure of this object; as you can see, it is a collection of Items with DateTime and Text. The DateTime will be used to know when that Local Notification will be triggered, and the text will be the info shown to the user. You can define as many Local Notifications as you want. One SDTLocal Notifications Item is equivalent to one Local Notification.

LocalNotifIphone5

b. Local Notifications:

This External Object has one method: Create Alerts which expects as an input parameter an instance of a variable based on SDTLocal Notifications. When this action is executed, the app wil schedule all the local notifications passed on the SDTLocal Notifications. 

LocalNotifIphone6

Step 2: Programming the Local Notifications

To make them work just to create the following Procedure (e.g: named "SendLocalNotificationsServer").

In this example, we are creating this procedure to load the SDTLocal Notifications, but this can be done differently because it is not compulsory to do it with a procedure.

Parm:

parm(out: &MySdtLocalNotifications);

Source:

&MySdtLocalNotificationsItems.DateTime = ymdhmstot(2012,06,07,16,56,00)
&MySdtLocalNotificationsItems.Text = "My Local Test"
&MySdtLocalNotifications.Add(&MySdtLocalNotificationsItems)

Variables:

MySdtLocalNotifications -> SDTLocalNotifications
MySdtLocalNotificationsItems -> SDTLocalNotifications.Item

Next, let's create a Panel for Smart Devices object:

Layout:

LocalNotifIphone3

Events:

 Event 'SendNotification'
      Composite
          &MySdtLocalNotifications = SendLocalNotificationsServer()
          LocalNotifications.CreateAlerts(&MySdtLocalNotifications)
      EndComposite
 EndEvent

Step 3: Execution

Run your application.

First, hit the Panel for Smart Device button to start scheduling Local Notifications. After that, the Notifications have been scheduled and they will appear on your device at the time specified.

Images:

1.

LocalNotifIphone1

2.

LocalNotifIphone2

TroubleShooting

The Create method of the external object returns a numeric value. If the value returned is 0, it means that there was no error in the execution. Otherwise, if the numeric value is different than 0, an execution error occurred.

Availability

This feature has been added to GeneXus in:

  • iOS, GeneXus Evolution 2 Upgrade 1
  • Android, GeneXus Evolution 2 Upgrade 3

As of GeneXus X Evolution 2 Upgrade 2, the following methods were added:

ListAlerts() Returns an SDTLocalNotifications with the future Notifications scheduled.
RemoveAlerts(alerts) Removes a List of Alerts sent in the alerts parameter.
RemoveAllAlerts() Remove all future alerts.

 

Notes

  • Since iOS 8 the local notifications require an authorization from the user. The user will be automatically asked for this authorization the first time you use this API. In case that the developer wants to do it manually, it can be done by using Permissions external object (e.g. when the app is in the background). 
  • If the DateTime field of the LocalNotifiation SDT is empty, the notification will be triggered instantly.

A working example of this method can be downloaded from: WorkingWithLocalNotifications.







Was this page helpful?
What Is This?
Your feedback about this content is important. Let us know what you think.