App Inventor Extensions


Alarm Manager Extension
with Notification or Autostart

See the App Inventor Extensions document about how to use an App Inventor Extension.

For questions about this extension or bug reports please start a new thread in the App Inventor community. Thank you.

For feature requests please contact me by email. To be a sponsor of a new method already is possible starting from only 10 USD! With your contribution you will help the complete App Inventor community. Thank you.

Description

Alarm Manager Extension to send a notification or autostart the app at a given time or at boot time only once or repeated by an interval. In case autostart=false then after clicking the notification your app will be started. You can define a start text, which will be passed to your app in method 'get plain start text' and trigger some action after your app is started. Also of course works if your app is not running. The defined alarms will be reinitialized after rebooting the device. You also can define several alarms, just use a different id for each alarm. Starting from version 8 you can execute almost any logic in the background (no user interface components) after an alarm occurred and even define new alarms in the background, Please be aware, that there is no user interface available in the background.
Minimum API level is 21 (Android 5).

Required permissions: android.permission.RECEIVE_BOOT_COMPLETED, android.permission.FOREGROUND_SERVICE,
android.permission.SYSTEM_ALERT_WINDOW (added in version 5), android.permission.POST_NOTIFICATIONS (added in version 7)

Itoo integration

Itoo is a powerful framework to execute blocks in the background. Version 8 of the alarmmanager extension uses the new itoox-wrapper library and provides the new OnReceive event handler (which looks like a procedure). By default the event handler is located in Screen1. But you can set the property ReferenceScreen (see documentation below) to use the event hanlder on a different screen.

The background event handler will be called from Itoo from the background after an alarm occurred. You will have to create the procedure using the name OnReceive. Also that procedure must have 3 arguments, else it will not work. Note: This background event handler also will be called if the app is in the foreground.


Now we can execute almost any logic in the background (no user interface components) after an alarm occurred and even define new alarms in the background. Please be aware, that there is no user interface available in the background.

Preconditions for using the OnReceive event handler together with Itoo

  • You should already have some experience in App Inventor and understand the basics. If you are new, then first learn App Inventor and come back later for these advances features. Alternatively you can contact me by email to get personal support for a small fee. As beginner you already can use the alarmmanager extension without the OnReceive event handler!
  • You are familiar with the Do it debugging functionality of App Inventor.
  • You are familiar with using Logcat. To find possible errors of your blocks in the background, let me suggest to filter the logcat output for the tags TaifunAlarmManager, Notifier, ItooCreator and System.err.

Guidelines for using Itoo

  • First get your background functionality running in the foreground. Like this, you can find errors much easier. And as soon as you got it working in the foreground, then test it in the background.
  • Read the complete Itoo to leaarn how Itoo works and try the example projects, See also the battery checker tutorial and notification listener and itoo tutorial.
  • Do not use global variables while in the background, use local variables instead
  • Do not use any user-visible or UI components in the background
  • In the background, use the Itoo StoreProperty/FetchProperty methods rather than TinyDB. TinyDB relies on SharedPreferences which will not ensure data synchronization across background processes.

Example use cases

  • Run a function when the alarm is activated and also set the next alarm without opening the screen or disrupting the user experience (requested by Tammi_Allen)
  • Send a text message after an alarm occurred
  • Do something regularly in the background or after booting the device.
  • Get daily prayer notifications (prayer times change from day to day) (requested by Chechnya_360)

Example blocks about how to use the alarmmanager extension together with Itoo

  • Example to set a new alarm after an alarm occurred

    This example project starts an alarm in the foreground in one minute. After the alarm occurred, start a new alarm in the background without user interaction. This is only a demonstration of how to set an alarm in the background. Of course for 2 or 3 alarms you can do the same directly in the foreground. But like this in the background, you can set a new alarm after an alarm occurred and continue this endless times.

    Note: The alarmmanager extension uses the set (Android documentation) method to set an alarm, which means, the alarm will occur not exactly at the defined time, usually a little bit later. Applications are strongly discouraged from using exact alarms unnecessarily as they reduce the OS's ability to minimize battery use.

    Screenshot of the user interface


    Description of the blocks

    • Event Screen1.Initialize: Get the required permission and check, if an alarm already is running
    • Event btnStart.Click: calls the start procedure (Note: this procedure will be called from the foreground and later also from the background)
    • Event btnStop.Click: cancels the alarm and stops the process of repeatedly setting alarms
    • Event TaifunAlarmManager1.Started: this event will be called after the alarm has been set in the foreground
    • Event btnClose.Click: closes the application
    • Procedure switchButtons: toggles the visibility of the Start and Stop button
    • Background event handler OnReceive: This is the background event handler, which looks like a procedure. The Notifier1.LogInfo method logs the current alarm information to the log, which can help to debug the project using Logcat more easily. Then the start procedure is called to set an alarm. The background event handler will be called from Itoo from the background after an alarm occurred. You will have to create the procedure using the name OnReceive. Also that procedure must have 3 arguments, else it will not work. Note: This background event handler also will be called if the app is in the foreground.
    • Procedure start: to set an alarm in one minute. The StoreProperty/FetchProperty methods from Itoo are used to increment the alarm number. Note: this procedure will be called from the foreground and also from the background.
  • Example to get a daily notification 30 minutes before sunset

    Get the sunset time of tomorrow from an API using the web component and set an alarm 30 minutes before that time daily in the background without user interaction. As location for the example project the Whalestail in Uvita, Costa Rica is used.

    Screenshot of the user interface (before and after starting)


    Flowchart of the blocks logic
    The procedures get and setAlarm will be called from the foreground and from the background.

    Main Blocks






    Helper procedures

    • tomorrow: Return tomorrows date in format YYYY-MM-DD
    • getSunset: Extract the sunset time from the returned JSON string and convert it into format YYYY-MM-DD HH:mm:ss
    • getInstantOfAlarmTime: Return the alarm time (for example sunset time minus 30 minutes) as instant
    • getNextAlarmTime: Extract the next alarm time from the stored alarm and convert it into format YYYY-MM-DD HH:mm:ss

 

Kodular Restrictions

Properties



Reference screen for Itoo where the procedure 'OnReceive' exists. If no property is set, the extension will take "Screen1" as the reference by default.

OnReceive has the arguments title, text and startText.

Note: The OnReceive event handler also will be called if the app is in the foreground!


Returns whether app should autostart.


Specifies whether app should autostart. Default value is False.

Note 1: To use this feature, the user needs to grant "Appear On Top" permission. See the new methods HasAppearOnTopPermission and OpenAppearOnTopSettings. So while the user is using another app, at a given time your app will get priority and will appear on top of the other currently running app.

Note 2: To be able to autostart the app at a given time make sure to close the app using the close application block, else the app will not start, because it is already running.

Note 3: For security reasons it is not possible to bring the app to the front in case the screen is locked. You first will have to unlock the screen. Please test the example app to see how you can do it. At the alarm time the app will be started automatically and a looping sound is played. If the screen is locked, you first have to unlock the screen to stop the sound.


Returns SmallIcon for notification. Recommended size is 96x96.


Sets SmallIcon for notification. Recommended size is 96x96.
Note: this feature is available starting rom Android 6 (API Level 23). For older devices this setting will be ignored. The small icon must be in png format and have a transparent background.


Returns LargeIcon for notification. Recommended size is 256x256.


Sets LargeIcon for notification. Recommended size is 256x256.
Note: this feature is available starting rom Android 6 (API Level 23). For older devices the application icon will be used as large icon.


Returns BigText for notification


Sets BigText for notification
Note: you can only use bigPicture or bigText. If you specify both, then only bigText will be displayed.


Returns BigPicture for notification. Minimum: 512x256, Balanced: 1024x512, Maximum: 2048x1024.


Sets BigPicture for notification. Minimum: 512x256, Balanced: 1024x512, Maximum: 2048x1024.
Note: you can only use bigPicture or bigText. If you specify both, then only bigText will be displayed.


Returns the notification color, i.e. the color as an alpha-red-green-blue integer.


Specifies the notification color. Note: you can use any integer color code or the color blocks of App Inventor. In the example alarmmanager app I used a simplified version of Italo's color picker example, see also the example project "StatusBarColor blocks" in the tools extension documentation.

Screenshots of the new features in Version 4

small Icon and default color

small Icon, large Icon, yellow color and big Text

small Icon, large Icon and green color

small Icon, large Icon, blue color and big Picture

Note: These are screenshots taken from a Samsung Galaxy A51 device running Android 10. Depending on the Android version you will get slightly other results.

Methods


Returns true, if post notifications permission has been granted, else false. This permission is available starting from API 33.


Ask for Post Notifications permission. Required starting from SDK 33.


This method is for alarms which only should fire once, but starting from Version 3 also for long term intervals. Start an alarm at a given date/time (instant). In case the time already has passed, the alarm will start asap. Define the title, text and start text of the notification. Interval can be one of the following values: 'ONCE', 'WEEK', 'MONTH', 'YEAR'. The start text is the value which will be passed to your app in method 'get plain start text'. Use your own id (integer value). If you use the same id again, then the previously defined alarm, which already uses that id will be overwritten..

Note: Starting from version 7b the Start method uses set (Android documentation).

Note: You might want to check, if your app has post notifications permission using the new method HasPostNotificationsPermission.

Note: You will have to build the app to be able to test this method.


Start a repeating alarm at a given hour and minute (short term intervals). Define the title, text and start text of the notification. Interval can be one of the following values: 'FIFTEEN_MINUTES', 'HALF_HOUR', 'HOUR', 'HALF_DAY', 'DAY' The start text is the value which will be passed to your app in parameter 'get plain start text'. Use your own id (integer value). If you use the same id again, then the previously defined alarm, which already uses that id will be overwritten.

Note: The StartRepeating method uses setRepeating (Android documentation).

Note: You might want to check, if your app has post notifications permission using the new method HasPostNotificationsPermission.

Note: You will have to build the app to be able to test this method.


Start an alarm at boot time.
The start text is the value which will be passed to your app in method 'get plain start text'. Use your own id (integer value). If you use the same id again, then the previously defined alarm, which already uses that id will be overwritten."

Note: You will have to build the app to be able to test this method.


Return a list of all alarm Ids.


Retrieve the alarm in JSON format stored under the given id. If there's no such id, then return empty string.


Get all Alarms in JSON format.


Cancel an alarm using the id you previously used to start the alarm.


Open Notification Settings (deprecated).
In version 8 method AskForNotificationsPermission has been added. You might want to use the new nethod instead of OpenNotificationsSettings.


If user likes to autostart the app, special permission is required, Using this method, you can check, if the permission has been granted.


Open Appear On Top Settings to enable Autostart for app.

Until Android 10 the user can enable the Appear On Top setting directly after using the Open Appear On Top Settings method.

Starting from Android 11 you first have to select your app ...

... before you can enable the Appear On Top setting.

Events


Event returning success or failure after starting an alarm.


Event returning success or failure after canceling an alarm.


Event indicating that an alarm occurred. Event will fire only if the app is up and running, else a notification will be displayed. For multiple screen apps this happens only for those screens, the extension is being used or has been dragged to the working area.

Example App "Alarm Manager with Notification Test"


Screenshots:





Example App "Alarm Manager with Autostart Test"

For security reasons it is not possible to bring the app to the front in case the screen is locked. You first will have to unlock the screen. Please test the example app to see how you can do it. At the alarm time the app will be started automatically and a looping sound is played. If the screen is locked, you first have to unlock the screen to stop the sound.

Example App "Picture Of The Day": get daily at a given time the Astromomic picture of the day by the NASA APOD API.


Screenshots:






Test

Tested successfully on several devices starting from Android 5 up to Android 14.

Questions and Answers

Q1: What is the difference between this Notification extension and others, which are also available in the community?
A: Generally my extension works starting from Android 5 up to the latest version Android 12. It is working even if the app is not running and the notification still will be sent at the defined date and time after rebooting the device. It does not need any further service like One Signal.

There are currently the following notification extensions available:

 

Q2: Woud it be possible to add a feature to the extension to restart my app at a defined date and time in the future?
A: This is now possible starting with version 5. However Google restricts this feature like this (taken from here): Android 10 (API level 29) and higher place restrictions on when apps can start activities when the app is running in the background. These restrictions help minimize interruptions for the user and keep the user more in control of what's shown on their screen. In nearly all cases, apps that are in the background should display time-sensitive notifications to provide urgent information to the user instead of directly starting an activity.
Also starting from Android 10 the app must have been granted the SYSTEM_ALERT_WINDOW permission by the user to be able to restart itself from the background. And in Android 11 this has been even restricted further.
For security reasons it is not possible to bring the app to the front in case the screen is locked. You first will have to unlock the screen.

Q3: I'm using a Xiaomi device and the alarm does not work anymore after rebooting the device. What can I do?
A: After rebooting the device, the Boot Service must reconfigure the alarms. However on Xiaomi devices, Auto Start is disabled by default for all applications. See here how to enable it for your app. See also https://dontkillmyapp.com/xiaomi

Q4: How to get an instant of the 25th of the current month?
A: See my answer here.

Q5: How to initiate a specific screen after clicking on push notification?
A: You will have to use the Screen.Initialize event like this

   if not is empty get plain start text
   then open another screen "your screen name"

You can find the not block in the logic drawer, the is empty block in the text drawer and the get plain start text block in the controls drawer.

Q6: I am trying to send a text message using AfterAlarm method when app is on the forefront sms goes but it does not go when app is in the background or after phone restarts
A: This is now possible starting from Version 8 of the extension.

Q7: I would like to start my app regularly to do something without user interaction. Is this possible using this extension?
A: Rather than starting the app what about executing some functionality in the background after an alarm occurred? This is now possible starting from Version 8 of the extension.

For questions about App Inventor,
please ask in the App Inventor community.Thank you.

Test the functionality of the Alarm Manager with Notification extension

3 test apps are available in Google Play. You can test the extension following these steps

  1. Follow the opt-in URL for the alarmmanager example app to get access to the app and to be able to download it to your device. Here you can test all the features.
  2. Follow the opt-in URL for the Picture Of The Day example app to get access to the app and to be able to download it to your device.
  3. Follow the opt-in URL for the Autostart example app to get access to the app and to be able to download it to your device.
  4. Start testing.

Buy the Alarm Manager extension for App Inventor


You can buy this extension (aix file).
With your payment you accept the terms and conditions below.

Payment options

1. Paypal

Please transfer 15 USD via Paypal
to Pura Vida Apps

PayPal - The safer, easier way to pay online!

2. Bitcoin

Then send me the transaction id by email and tell me the extension you are interested in.

After having received your payment I will be happy to send the download link to you. Please let me know your Google account! I usually will send the download link not later than 24 hours after having received your payment.
Thank you! Taifun


Terms and Conditions

Back to top of page ...


Home | Snippets | Tutorials | Extensions | Links | Search | Privacy Policy | Contact