Skip to main content

Flutter Notification Channels Examples

Flutter Notification Channels: How To Examples

Introduction

Flutter is a popular cross-platform framework for building mobile applications. In Flutter, you can use notification channels to manage and categorize different types of notifications in your app. Notification channels allow users to have control over which notifications they receive and how they are displayed. In this tutorial, we will explore various examples of using notification channels in Flutter.

Installation

Before we start, make sure you have Flutter installed on your machine. You can follow the official Flutter installation guide to set up Flutter on your system.

To use notification channels in Flutter, you will need to add the flutter_local_notifications package to your Flutter project. Open your project's pubspec.yaml file and add the following line under the dependencies section:

dependencies:
flutter_local_notifications: ^5.0.0

Save the file and run the following command in your project's root directory to fetch the package:

flutter pub get

Now, you are ready to start using notification channels in your Flutter app.

Example 1: Creating a Notification Channel

The first example demonstrates how to create a notification channel in Flutter. A notification channel groups similar types of notifications together.

import 'package:flutter_local_notifications/flutter_local_notifications.dart';

void createNotificationChannel() async {
const AndroidNotificationChannel channel = AndroidNotificationChannel(
'channel_id',
'Channel Name',
'Channel Description',
importance: Importance.max,
priority: Priority.high,
);

final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();

await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
}

In this example, we first import the flutter_local_notifications package. We then create an instance of AndroidNotificationChannel with a unique channel_id, a Channel Name, and a Channel Description. We can also specify the importance and priority of the channel.

Next, we create an instance of FlutterLocalNotificationsPlugin. Finally, we use the createNotificationChannel method of AndroidFlutterLocalNotificationsPlugin to create the notification channel.

Example 2: Sending a Notification to a Channel

Once you have created a notification channel, you can send a notification to that channel. Let's see how to send a notification to a channel in Flutter.

import 'package:flutter_local_notifications/flutter_local_notifications.dart';

void sendNotificationToChannel() async {
const AndroidNotificationDetails androidPlatformChannelSpecifics =
AndroidNotificationDetails(
'channel_id',
'Channel Name',
'Channel Description',
importance: Importance.max,
priority: Priority.high,
);

const NotificationDetails platformChannelSpecifics =
NotificationDetails(android: androidPlatformChannelSpecifics);

final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();

await flutterLocalNotificationsPlugin.show(
0,
'Notification Title',
'Notification Body',
platformChannelSpecifics,
payload: 'item x',
);
}

In this example, we first import the flutter_local_notifications package. We then create an instance of AndroidNotificationDetails with the same channel_id used while creating the channel. We also provide a title and body for the notification.

Next, we create an instance of NotificationDetails and pass the androidPlatformChannelSpecifics to it.

Finally, we create an instance of FlutterLocalNotificationsPlugin and use the show method to display the notification. The payload parameter can be used to pass any additional data.

Example 3: Managing Notification Importance

Notification importance determines how the notification is displayed to the user. Let's see an example of how to manage notification importance in Flutter.

import 'package:flutter_local_notifications/flutter_local_notifications.dart';

void setNotificationImportance() async {
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();

await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.setNotificationChannel(
const AndroidNotificationChannel(
'channel_id',
'Channel Name',
'Channel Description',
importance: Importance.min,
priority: Priority.low,
),
);
}

In this example, we first import the flutter_local_notifications package. We then create an instance of FlutterLocalNotificationsPlugin.

Next, we use the setNotificationChannel method of AndroidFlutterLocalNotificationsPlugin to update the notification channel with a new importance and priority.

Example 4: Customizing Channel Sound

You can customize the sound played when a notification from a specific channel is received. Let's see how to customize the sound for a channel in Flutter.

import 'package:flutter_local_notifications/flutter_local_notifications.dart';

void customizeChannelSound() async {
const AndroidNotificationChannel channel = AndroidNotificationChannel(
'channel_id',
'Channel Name',
'Channel Description',
importance: Importance.max,
priority: Priority.high,
sound: RawResourceAndroidNotificationSound('custom_sound'),
);

final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();

await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
}

In this example, we first import the flutter_local_notifications package. We then create an instance of AndroidNotificationChannel with a unique channel_id, a Channel Name, and a Channel Description. We also specify the importance, priority, and sound of the channel. The RawResourceAndroidNotificationSound class is used to specify a custom sound by providing the sound's resource name.

Next, we create an instance of FlutterLocalNotificationsPlugin and use the createNotificationChannel method to create the notification channel.

Example 5: Setting Channel Vibration Pattern

You can also customize the vibration pattern for a channel in Flutter. Let's see how to set a vibration pattern for a channel.

import 'package:flutter_local_notifications/flutter_local_notifications.dart';

void setChannelVibrationPattern() async {
const AndroidNotificationChannel channel = AndroidNotificationChannel(
'channel_id',
'Channel Name',
'Channel Description',
importance: Importance.max,
priority: Priority.high,
vibrationPattern: Int64List.fromList([0, 1000, 500, 2000]),
);

final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();

await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
}

In this example, we first import the flutter_local_notifications package. We then create an instance of AndroidNotificationChannel with a unique channel_id, a Channel Name, and a Channel Description. We also specify the importance, priority, and vibrationPattern of the channel. The vibrationPattern is an Int64List representing the vibration pattern in milliseconds.

Next, we create an instance of FlutterLocalNotificationsPlugin and use the createNotificationChannel method to create the notification channel.

Example 6: Grouping Notifications in a Channel

You can group multiple notifications in a channel to improve organization and display. Let's see how to group notifications in a channel in Flutter.

import 'package:flutter_local_notifications/flutter_local_notifications.dart';

void groupNotificationsInChannel() async {
const AndroidNotificationChannelGroup channelGroup =
AndroidNotificationChannelGroup(
'group_id',
'Group Name',
'Group Description',
);

const AndroidNotificationChannel channel = AndroidNotificationChannel(
'channel_id',
'Channel Name',
'Channel Description',
groupIdentifier: 'group_id',
importance: Importance.max,
priority: Priority.high,
);

final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();

await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannelGroup(channelGroup);

await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
}

In this example, we first import the flutter_local_notifications package. We then create an instance of AndroidNotificationChannelGroup with a unique group_id, a Group Name, and a Group Description.

Next, we create an instance of AndroidNotificationChannel with a unique channel_id, a Channel Name, and a Channel Description. We also specify the groupIdentifier to group the channel under the specified channel group.

Finally, we create instances of FlutterLocalNotificationsPlugin and use the createNotificationChannelGroup and createNotificationChannel methods to create the notification channel group and channel.

Example 7: Deleting a Notification Channel

If you no longer need a notification channel, you can delete it. Let's see how to delete a notification channel in Flutter.

import 'package:flutter_local_notifications/flutter_local_notifications.dart';

void deleteNotificationChannel() async {
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();

await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.deleteNotificationChannel('channel_id');
}

In this example, we first import the flutter_local_notifications package. We then create an instance of FlutterLocalNotificationsPlugin.

Next, we use the deleteNotificationChannel method of AndroidFlutterLocalNotificationsPlugin and pass the channel_id of the channel to be deleted.

Example 8: Getting List of Notification Channels

You can retrieve a list of all the notification channels created in your app. Let's see how to get a list of notification channels in Flutter.

import 'package:flutter_local_notifications/flutter_local_notifications.dart';

void getNotificationChannels() async {
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();

final List<AndroidNotificationChannel> channels =
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.getNotificationChannels();
}

In this example, we first import the flutter_local_notifications package. We then create an instance of FlutterLocalNotificationsPlugin.

Next, we use the getNotificationChannels method of AndroidFlutterLocalNotificationsPlugin to retrieve a list of notification channels.

Example 9: Checking if a Channel Exists

You can check if a specific notification channel exists in your app. Let's see how to check if a channel exists in Flutter.

import 'package:flutter_local_notifications/flutter_local_notifications.dart';

void checkChannelExists() async {
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();

final bool channelExists =
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.channelExists('channel_id');
}

In this example, we first import the flutter_local_notifications package. We then create an instance of FlutterLocalNotificationsPlugin.

Next, we use the channelExists method of AndroidFlutterLocalNotificationsPlugin and pass the channel_id of the channel to check if it exists.

Example 10: Updating a Notification Channel

You can update the properties of an existing notification channel. Let's see how to update a notification channel in Flutter.

import 'package:flutter_local_notifications/flutter_local_notifications.dart';

void updateNotificationChannel() async {
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();

await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.updateNotificationChannel(
const AndroidNotificationChannel(
'channel_id',
'Updated Channel Name',
'Updated Channel Description',
importance: Importance.min,
priority: Priority.low,
),
);
}

In this example, we first import the flutter_local_notifications package. We then create an instance of FlutterLocalNotificationsPlugin.

Next, we use the updateNotificationChannel method of AndroidFlutterLocalNotificationsPlugin to update the notification channel with new properties.

Conclusion

In this tutorial, we explored various examples of using notification channels in Flutter. We covered creating a channel, sending notifications to a channel, managing importance, customizing sound and vibration, grouping notifications, deleting a channel, getting a list of channels, checking if a channel exists, and updating a channel. By understanding these examples, you can effectively use notification channels in your Flutter app to provide a better user experience.