Notification & Alarms in Flutter
By: Greg Perry
Tags: iOS App Development, Flutter, Programming, Computer Science

Working with the Flutter Plugin, Android_Alarm_Manager.

Know this! This plugin only works for the Android platform! I personally don’t know of any iOS equivalent. Alternatively, a month after publishing this article, I came upon a plugin that provides notifications on both the Android and iOS platforms. I, of course, wrote an article and that as well. See below.

Know that if you want to use the plugin described here, you must follow its readme file explicitly to set things up correctly. You’re AndroidManfest.xml should at least look something like this below:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="NAME OF YOUR APPLICATION STARTING WITH COM."> <!-- The INTERNET permission access.-->

<uses-permission android:name="android.permission.INTERNET"/>



<!-- android_alarm_manager -->

<!-- Start an Alarm When the Device Boots if past due -->

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <!-- application needs to have the device stay on -->

<uses-permission android:name="android.permission.WAKE_LOCK"/> <application

android:name="io.flutter.app.FlutterApplication"

android:label="code_samples"

android:icon="@mipmap/ic_launcher">

<activity

android:name=".MainActivity"

android:launchMode="singleTop"

android:theme="@style/LaunchTheme"

android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"

android:hardwareAccelerated="true"

android:windowSoftInputMode="adjustResize">

<meta-data android:name="io.flutter.embedding.android.NormalTheme"

android:resource="@style/NormalTheme"

/>

<meta-data

android:name="io.flutter.embedding.android.SplashScreenDrawable"

android:resource="@drawable/launch_background"

/>

<intent-filter>

<action android:name="android.intent.action.MAIN"/>

<category android:name="android.intent.category.LAUNCHER"/>

</intent-filter>

</activity>



<!-- android_alarm_manager -->

<service

android:name="io.flutter.plugins.androidalarmmanager.AlarmService"

android:permission="android.permission.BIND_JOB_SERVICE"

android:exported="false"/>

<receiver

android:name="io.flutter.plugins.androidalarmmanager.AlarmBroadcastReceiver"

android:exported="false"/>

<receiver

android:name="io.flutter.plugins.androidalarmmanager.RebootBroadcastReceiver"

android:enabled="false">

<intent-filter>

<action android:name="android.intent.action.BOOT_COMPLETED" />

</intent-filter>

</receiver>

</application>

</manifest>

And of course, in your pubspec.yaml file, you will need the Flutter plugin:

# https://pub.dev/packages/android_alarm_manager

android_alarm_manager: ^0.4.0

Now, if you’ve been following my articles, you know a good many of them are about a library Dart file containing some ‘wrapper utility class’ I wrote. More often than not, I would write such a class to work with a Flutter plugin I needed or found useful. Well, this article is no exception.

In this case, it’s for setting off alarms in your app! In my case, I found the Flutter plugin, android_alarm_manager, met the needs of a recent app I was working on and so…I made a routine to easily work with it. If you want it, take a copy, make it your own and share any improvements you make. Cool?

Now if you’ve time, please continue to read what I found I had to do to make this plugin work almost ‘foolproof’ so to easily and quickly set an alarm or any another operation to be performed ‘in the background’ at some point in time in the future while the app is running. It makes Life a little easier, and that’s a good thing. Right?

Screenshots Only. Click For Gists.

As always, I prefer using screenshots over gists to show code in my articles. I find them easier to work with, and easier to read. However, you can click/tap on them to see the code in a gist or in Github. Ironically, it’s better to read this article about mobile development on your computer than on your phone. Besides, we program mostly on our computers; not on our phones. For now.

Let’s begin.

My approach here is to first present this utility class in an example and demonstrate how to use it — thus taking advantage of the Flutter plugin, android_alarm_manager. In fact, I’ll use the very same example listed in the plugin's own example page. However, this example has been modified to utilize the library file presented here instead. A copy of this example is available to you as the gist, android_alarm_manager. After the example, I’ll walk through parts of the utility class itself describing at times what needed to be done to allow such a class to be used by the unyielding masses that is the general public.

Keep It Static

In this very simple example, you are to press the button displayed, and 5 seconds later, those two zeros as pictured in the screenshot below will turn into ones. Wheeee! What’s really interesting, of course, is what’s under the hood in the code.

Note, I changed the code from the original to accommodate the asynchronous operations that have to be performed before the app could actually start running. I’ve utilized a FutureBuilder widget to accomplish this. In doing so, I defined a new function called, initSettings(), to initialize the ‘Shared Preferences’ routine used to ‘remember’ the number of total alarms fired as seen on the screenshot above.