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 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
- 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.
Additional Steps for Android 14 and Above
If your app is targeting devices with Android 14 (API levels 34 and above), you must complete some required actions before scheduling local notifications. Read below for guidance.
If you are familiar with setting precise alarms on Android devices, you might notice recent changes introduced in Android 14. Specifically, the SCHEDULE_EXACT_ALARM permission (crucial for scheduling exact alarms) is no longer automatically granted to most newly installed apps targeting Android 13 and higher.
This means that for scheduling notifications on such devices, the end-user must give an explicit approval to that permission. We have updated our Native Mobile Resources, so that you can easily implement these permission exchanges.
Using Check generic permission you can first check if a SCHEDULE_EXACT_ALARM is granted to an end-user’s device. To achieve that, you must select SCHEDULE_EXACT_ALARM_ANDROID from Permissions Enum:
As an output, you will receive either the granted or blocked status. With that information, you can proceed with further actions.
Consider the default case for Android 14 devices; the SCHEDULE_EXACT_ALARM permission is not given by default. In that case, you need to use Request generic permission with selected permission to request it. Requesting of SCHEDULE_EXACT_ALARM permission assumes that user will be navigated into the app’s Alarm & Reminders settings:
After that step, you are ready to schedule notifications on an end-user’s device!
Please note that to properly ensure that an end-user allowed the alarm permission, you can use an App Events widget and call check- or request- permission actions again and again if needed (for example, when an end-user has returned from application settings, but did not set the permission to enabled).
As you might expect, if you attempt to schedule a notification without the end-user giving a granted permission, nothing will happen on end-user’s device.
But what if you want to cancel a scheduled notification? Read on to learn just that.
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.