This how-to will teach you 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 how-to will teach 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
Before starting this how-to, 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
3 Creating a Project and Configuring Notifications
Follow the instructions below to set up your first local notification:
- Open Mendix Studio Pro.
- Select File > New Project.
Select the Native Mobile Quickstart 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 and drop 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 and drop three Create variable activities onto your nanoflow and set them as string variables titled Title, Subtitle, and Body:
Double-click your Title activity and then configure it:
a. Make sure Data type is set to String.
b. Click Generate.
c. Type Title into the Constant field.
d. Type Title into Output > Variable.
e. Click OK:
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 and drop a Has notification permission activity onto your nanoflow:
Double-click your Has notification permission activity, type NotificationPermission into Variable, then click OK:
Drag and drop a decision after your Has notification permission activity onto 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 and drop a Request notification permission activity onto your nanoflow:
Double-click your Request notification permission activity and set Output > Variable to PermissionGranted:
Drag and drop 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 and drop a Show message activity on your nanoflow and connect it like this:
Double-click your Show message activity, then do the following:
a. Select Type > Error from the drop-down menu.
b. Into Template type No notification permissions, go to your app permission settings to grant permission.
c. Click OK.
Drag and drop 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 top-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.