Local Notifications In Flutter
Tags: iOS App Development, Flutter, Android, Dart, Notifications

Integration

Due to the fact that Flutter is a cross platform framework, every package that is created for it needs to support both iOS and Android devices. Because notifications are handled very differently between iOS and Android, there are several calibrations that need to be made when using the local notifications package.

First we need to create an instance for the FlutterLocalNotificationPlugin. We will use this object to initialize settings for Android and iOS and also for other notification purposes.

We now need to initialize the local notification plugin with specific settings for Android and for iOS. To do so, we need to create an InitializationSettings object. It accepts arguments for the following operating systems: Android, iOS and MacOS.

We won’t be discussing MacOS here as it is similar to configuring iOS

Android

Here, it is fairly simple as there is only one mandatory argument to pass, defaultIcon(String). It represents the icon that will be displayed in the notification. Here you need to pass the name of the icon you want to be used. You must place this icon inside the drawable directory. The full path to it is:

YOUR_APPLICATION_NAME\android\app\src\main\res\drawable\YOUR_APP_ICON.png

Location of app_icon

There is no need for requesting any permissions.

iOS

As with most subjects related to iOS, things here get a bit more complicated. Due to the nature of how notifications are handled between different versions of the operating system, there is a need here for some extra configurations.

Inside the AppDelegate file you need to add the following lines of code:

It is required to ask permission from the user for various issues related to notifications. Therefore, the initializer object for iOS has the following arguments:

Each of them is self explanatory but relates to a different aspect in a notification. To correspond with these permissions are also default values you can set for each of them.

These options are there, since the initialization of the local notifications plugin may cause the operating system to present permissions dialogs to the user at a time when you don’t want them to show up. If you don’t want this behavior, you can set all of these values to false.

One more caveat in iOS has to do with the difference in behavior between notifications being presented to the user when your application is in the foreground or when it is in the background. Out of the box, the operating system will not display a notification to the user if the application is in the foreground. The plugin itself will take care of displaying a notification when the application is in the foreground, but below iOS10, there is a need to give a callback method onDidReceiveLocalNotification that will handle the user’s interaction with the notification.

After configuring specific platform initializations, its time to wrap all this logic into a method in our notification service. Our best approach here is to create an init method which will get called from our main.dart file when the application first launches.

Notice that after creating instances for platform specific initialization settings, we also need to create an InitializationSettings object which we pass in our platform specific initialization settings objects.

Our last step here is to call the initialize method on the FlutterLocalNotificationsPlugin object. Besides the initialization settings from above, it also has another argument called onSelectNotification. This argument represents the callback that will be called once a notification has been tapped and it is an optional argument. This callback has one argument called payload which will hold any data that is passed through the notification.

In our main.dart file, we will call the init method like this: