logo.png

Local Notifications

Friday, May 09, 2025

Articles/Local Notifications

Local Notifications

1. Setup

Dependencies

1. To use the flutter_local_notifications package, add this to your pubspec.yaml

     flutter_local_notifications: ^19.1.0

     timezone: ^0.10.1

This one is is for scheduled notifications.

2. Run this in your terminal

     flutter pub get

Android permissions

1. To push notifications in your Android app, you need to set up permissions first. Add these to your app/src/main/AndroidManifest.xml

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

POST_NOTIFICATIONS: Allows the app to show notifications (Android 13+).

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

RECEIVE_BOOT_COMPLETED: Lets the app run code after the device restarts.

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

VIBRATE: Enables the app to make the device vibrate.

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

ACCESS_NOTIFICATION_POLICY: Lets the app read or change Do Not Disturb settings.

This is how it should look like.

2. Now you need to add receivers for the notifications to work all the time.

<receiver
android:exported="false"
android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver" />

     ScheduledNotificationReceiver: Handles the triggering of scheduled notifications

<receiver
android:exported="false"
android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.MY_PACKAGE_REPLACED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
</intent-filter>
</receiver>

ScheduledNotificationBootReceiver: Reschedules notifications after reboot/App reinstall/update & Certain manufacturer-specific quick boot

<receiver
android:exported="false"
android:name="com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsReceiver"/>

FlutterLocalNotificationsReceiver: Showing notifications immediately

This is how it should look like

IOS permissions

1. To push notifications in your IOS app, you need to set up permissions first. Add these to your ios/Runner/AppDelegate.swift.

import flutter_local_notifications

FlutterLocalNotificationsPlugin.setPluginRegistrantCallback { (registry) in
GeneratedPluginRegistrant.register(with: registry)
​ }

if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
}

What is this? I don't know, i'm not a Swift dev, but in the package documentation, they ask us to add this.

This is how it should look like

  • Pro Tip: Flutter clean

If you face any problem, clean your app:

flutter clean

​flutter pub get

If you are on mac, you can run this also.

cd ios

pod install

cd ..

2. Code

Init

1. Let's initialize the notifications for android and ios.

Visit this Wikipedia page to find your location.

2. Now when you launch your application, you should see this if you run the future init().

3. Allow the notification otherwise it won't work.

Instant notifications

Scheduled notifications

3. Above an beyond

1. Get local timezone automatically

  • Pro Tip: Each scheduled notification need a different ID!

2. Notification per day

  • Pro Tip: Each scheduled notification need a different ID!

In this example, only Monday and Tuesday at 18:24 will trigger notifications

3. Grant notification access

If the user has rejected the local notifications popup 'Don't allow', then you can use the app_settings package to point the user into the notification settings.

app_settings: ^6.1.1

Then you create a logic to show the option only if the user has granted the notifications like the images down bellow.

4. Show pending saved notifications

This information is saved locally into the phone, so we know that there is 2 schedules notifications for Monday and Tuesday. Remember, each notification need a unique ID.

5. Finally you will need to cancel reminders (scheduled notifications)

Use the cancelAll() and the cancel(id) of the flutter_local_notifications package to delete either all or specific scheduled notification.

.cancelAll()
.​cancel(id)

PS: Check out Flutter Pro if you want to get instant access to my app code.