If you would like to upgrade to a newer long-term support version of Studio Pro, see Moving from Mendix Studio Pro 8 to 9.
Part 5: Scheduling
Introduction
Local notifications should rarely notify a user right after they perform an action. Here you will learn to configure local notifications to trigger after a period of time. To do this, you will use a JavaScript action named ScheduleNotification. After that, you will learn how to cancel scheduled notifications.
Prerequisites
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
- Complete the preceding tutorials in this Use Local Notifications series
Scheduling a Notification
To schedule a notification for a specific time, do the following:
-
Navigate to your ACT_CreateAndSendNotification nanoflow.
-
Replace your Display notification JavaScript action with a new Schedule notification action:
-
Double-click your new Schedule notification activity.
-
Set the Date to EndOfCurrentMinute.
-
Change the body, title, and subtitle to the variables that you created.
-
Set Play sound to true.
-
For now, set Notification id to empty.
-
Set Action name and Action guid to empty.
-
Click OK.
-
Start and load the app on your mobile device.
-
Tap the Send notification button, then quickly minimize your app.
The notification should be displayed at the end of the minute.
If your app is open, it will not show the notification due to iOS and Android user guidelines. However, notifications for open apps can still trigger onReceive events. This allows you to design custom actions for notifications, such as showing a page with pop-up layout. Follow these steps to model an onReceive event:
-
Make a new nanoflow named ON_ReceiveNotification.
-
Drag a Log message activity into your nanoflow.
-
Double-click this activity.
-
In Template write I triggered on receive notification then click OK.
-
Double-click your home page’s notifications widget.
-
Click Actions > New.
-
Create a New Action named TriggerOnReceive, set On receive to Call a nanoflow, and select ON_ReceiveNotification:
-
Click OK then OK again.
-
In ACT_CreateAndSendNotification, double-click your schedule notification activity.
-
Click Action name > Edit.
-
Type ‘TriggerOnReceive’ into the argument field and click OK.
-
Click OK.
-
Start and load the app on your mobile device.
-
Tap the Send notification button, and do not minimize your app.
-
You will see the log I triggered on receive notification in the Studio Pro console.
But what if you want to cancel a scheduled notification? Read on to learn more.
Cancelling Scheduled Notifications
To cancel a scheduled notification, you can use either the Cancel Scheduled Notification or Cancel All Scheduled Notification JavaScript actions. To cancel a specific notification, provide an identifier of that particular notification to Cancel Scheduled Notification. To cancel all notifications, call the Cancel All Scheduled Notification JavaScript action. For further information, see the subsections below.
Cancelling all Scheduled Notifications
To cancel all scheduled notifications, do the following:
-
Create a nanoflow named ACT_CancelAllScheduledNotifications.
-
Drag a JavaScript action named Cancel all scheduled notifications into your nanoflow:
-
Double-click your new cancel notification action.
-
Select Use return value > No.
-
Click OK.
-
Drag this nanoflow onto your home screen and name its button Cancel all.
To test your new cancel button, do the following:
- Start and load the app on your mobile device.
- Tap your Send notification button.
- Tap your Cancel all button.
- Minimize your app.
You will not see a notification at the end of the minute, proving your cancel action a success!
Cancelling a Specific Scheduled Notification
To cancel a specific scheduled notification, you will need to supply a notification ID for the notification you wish to cancel.
-
Navigate to ACT_CreateAndSendNotification.
-
Double-click your Schedule notification activity.
-
Click Notification id > Edit.
-
Type ’testID’ into the argument field and click OK:
-
Click OK once more to close the dialog box.
-
Create a nanoflow named ACT_CancelScheduledNotifications.
-
Drag a JavaScript Action named Cancel scheduled notification into your nanoflow:
-
Double-click your new cancel notification action.
-
Click Notification id > Edit.
-
Type ’testID’ into the parameter argument field and click OK.
-
Click OK again to close the dialog box.
-
Drag this nanoflow onto your home screen and name its button Cancel a specific notification:
To test your new cancel button, do the following:
- Start and load the app on your mobile device.
- Tap your Send notification button.
- Tap your Cancel a specific notification button.
- Minimize your app.
You will not see a notification at the end of the minute, proving your cancel action a success! Congratulations on completing this series and mastering the power of local notifications.