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.
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.
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.
Step 2: Programming the Local Notifications
To make them work just 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.
&MySdtLocalNotificationsItems.DateTime = ymdhmstot(2012,06,07,16,56,00)
&MySdtLocalNotificationsItems.Text = "My Local Test"
MySdtLocalNotifications -> SDTLocalNotifications
MySdtLocalNotificationsItems -> SDTLocalNotifications.Item
Next, let's create a Panel for Smart Devices object:
&MySdtLocalNotifications = SendLocalNotificationsServer()
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.
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.
This feature has been added to GeneXus in:
- iOS, GeneXus Evolution 2 Upgrade 1
- Android, GeneXus Evolution 2 Upgrade 3
Working With Local Notifications
In GeneXus X Evolution 2 Upgrade 2 the following methods were added:
||Returns an SDTLocalNotifications with the future Notifications scheduled.
||Removes a List of Alerts sent in the alerts parameter.
||Remove all future alerts.
NOTE: 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.
There is a scenario where you want to do it manually when it is possible that the first time you use local notifications when the app is in the Background.
This can be done using the iOSPermissions external object (X Evolution 3).
A working example of this method can be downloaded from: WorkingWithLocalNotifications.