Part 1: Local Notifications
Introduction
This guide teaches you how to build local notifications for native mobile applications. Local notifications will only allow you to schedule and send notifications confined to one mobile device. These notifications do not use an internet connection. One use of a local notification might be an alarm app which sends a notification after an amount of time has elapsed.
This guide teaches you how to do the following:
- Build a button connected to a nanoflow which calls a local notification
- Make your notification nanoflow request device permission for local notifications
Prerequisites
Before starting this guide, make sure you have completed the following prerequisites:
- Review the basic differences between local notifications and push notifications
- Install the Make It Native app on your mobile device
Creating an App and Configuring Notifications
Follow the instructions below to set up your first local notification:
-
Open Mendix Studio Pro.
-
Select File > New App.
-
Select the Blank Native Mobile App (also available online here):
-
Click Use this starting point.
-
Click Create App to close the dialog box:
-
Make sure you have a Native phone profile enabled:
-
Drag a Call nanoflow button onto your app’s home page, then click New to make a new nanoflow (note: you may wish to rename this button Send Notification):
-
Name the nanoflow ACT_CreateAndSendNotification and click OK:
-
In ACT_CreateAndSendNotification, drag three Create variable activities into your nanoflow and set them as string variables titled Title, Subtitle, and Body:
-
Double-click your Title activity and then configure it:
- Make sure Data type is set to String.
- Click Generate.
- Type Title into the Constant field.
- Type Title into Output > Variable.
- Click OK:
- Make sure Data type is set to String.
-
Double-click your Subtitle activity and configure similarly to your Title activity.
-
Double-click your Body activity and configure similarly to your Title activity.
Now you will set up the final logic necessary for your app to display a notification. A user must give permission for an app to send notifications. You will include a Request notification permission activity in your nanoflow to account for this, and include a few other activities.
-
Drag a Has notification permission activity into your nanoflow:
-
Double-click your Has notification permission activity, type NotificationPermission into Variable, then click OK:
-
Drag a decision after your Has notification permission activity into your nanoflow:
-
Double-click that decision and give it the Caption Permission:
-
Click Expression wizard, select Variable > NotificationPermission (Boolean), and then click OK until you are back at your nanoflow:
-
Drag a Request notification permission activity into your nanoflow:
-
Double-click your Request notification permission activity and set Output > Variable to PermissionGranted:
-
Drag a decision next to your Request notification permission activity.
-
Connect your activities and decisions, and set those connections’ values like so:
-
Double-click the decision, then set the Caption as Permission?.
-
Click Expression wizard
-
Select Value > Variable > Permission (Boolean) from the drop-down menu. When finished, your Decision should look like this:
-
Navigate back to your nanoflow.
-
Drag a Show message activity into your nanoflow and connect it like this:
-
Double-click your Show message activity, then do the following:
- Select Type > Error from the drop-down menu.
- Into Template type No notification permissions, go to your app permission settings to grant permission.
- Click OK.
- Select Type > Error from the drop-down menu.
-
Drag an End event under your Show message and connect them like this:
Now you will set up the final piece of your nanoflow’s logic.
-
Delete the end event in the upper-right corner of your nanoflow, drag and drop a Merge in its place, and rebuild your connections:
-
Drag and drop a Display Notification activity and connect it to your merge like this:
-
Set its Body, Title, and Subtitle to the variables that you created in the same nanoflow:
-
Set Play sound to true.
-
Set Action name and Action guid to empty:
-
Select Use return value > no:
-
Click OK, then navigate back to your nanoflow.
-
Add a final End event next to your Display notification activity and connect it like this:
-
When you are all finished, your nanoflow will look like this:
Now you can run your app and see if your notification works.
-
Start and load your app in your mobile device, then tap Send Notification:
-
When prompted to Allow notifications, tap OK.
-
After you allow notifications, you will receive a notification:
If you did not see a notification, try clicking Run Locally to reload your app. Then, tap the Send Notification button again.
Congratulations! You can now see local notifications on your device. Next, in How to Use Local Notifications Part 2: Badges, you will learn how to configure notification badges.