添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

什么是flutter_local_notifications插件

flutter_local_notifications Flutter 应用程序中使用的一个插件,它允许您在 Android iOS 设备上发送通知,包括状态栏通知和系统通知。

该插件支持自定义通知栏,包括通知图标、标题和内容,还支持本地化通知。同时该插件支持在 Android 上发送 BigPicture BigTextStyle 通知,使通知更加生动、醒目。

发送系统通知

在Android上发送系统通知

Android 上发送系统通知需要先获取通知权限,然后构建通知对象,最后发送通知。下面是发送系统通知的步骤:

1.添加依赖项

要在 Flutter 应用程序中使用 flutter_local_notifications 插件,需要将其添加到应用程序的 pubspec.yaml 文件中。下面是 pubspec.yaml 文件中添加依赖项的示例代码:

dependencies:
  flutter:
    sdk: flutter
  flutter_local_notifications: ^14.0.0+1

2.编写通知

在构建通知之前,需要先定义通知细节,包括通知的标题、内容和图标等。

var androidPlatformChannelSpecifics = AndroidNotificationDetails(
    'your channel id', 'your channel name', 'your channel description',
    importance: Importance.max, priority: Priority.high, ticker: 'ticker');
var platformChannelSpecifics = NotificationDetails(android: androidPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
    0, 'plain title', 'plain body', platformChannelSpecifics,
    payload: 'item x');

3.发送通知

向Android发送通知时,需要检查设备是否已经授予通知权限。如果设备没有授予通知权限,应该请求授予权限。

Future<void> checkPermission() async {
  var status = await flutterLocalNotificationsPlugin.getNotificationAppLaunchDetails();
  if (status?.didNotificationLaunchApp ?? false) {
    // handle notification tapped logic here
  var settings = await flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<
      AndroidFlutterLocalNotificationsPlugin>()?.getActiveNotifications();
  if (settings == null || settings.isEmpty) {
    var requestedSettings = await flutterLocalNotificationsPlugin
        .resolvePlatformSpecificImplementation<
            AndroidFlutterLocalNotificationsPlugin>()
        ?.requestPermissions();
    if (requestedSettings ?? false) {
      // handle permission granted case here
    } else {
      // handle permission denied case here
  } else {
    // handle notification already enabled case here
Future<void> sendNotification() async {
  var androidPlatformChannelSpecifics = AndroidNotificationDetails(
      'your channel id', 'your channel name', 'your channel description',
      importance: Importance.max, priority: Priority.high, ticker: 'ticker');
  var platformChannelSpecifics =
      NotificationDetails(android: androidPlatformChannelSpecifics);
  await flutterLocalNotificationsPlugin.show(
    0, 'plain title', 'plain body', platformChannelSpecifics,
    payload: 'item x');

在iOS上发送系统通知

在iOS上发送系统通知也需要获取通知权限,然后构建通知对象,最后发送通知。下面是在iOS上发送系统通知的步骤:

1.添加依赖项

要在Flutter应用程序中使用flutter_local_notifications插件,需要将其添加到应用程序的pubspec.yaml文件中。下面是pubspec.yaml文件中添加依赖项的示例代码:

dependencies:
  flutter:
    sdk: flutter
  flutter_local_notifications: ^14.0.0+1

2.编写通知

var iOSPlatformChannelSpecifics = IOSNotificationDetails(
        sound: 'default',
        presentAlert: true,
        presentBadge: true,
        presentSound: true,
        badgeNumber: 1,
var platformChannelSpecifics = NotificationDetails(iOS: iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
        'plain title', 
        'plain body', 
        platformChannelSpecifics,
        payload: 'item x',

3.发送通知

在向iOS发送通知时,和Android发送通知不同,不需要检查通知权限。

Future<void> sendNotification() async {
  var iOSPlatformChannelSpecifics = IOSNotificationDetails(
      sound: 'default',
      presentAlert: true,
      presentBadge: true,
      presentSound: true,
      badgeNumber: 1,
  var platformChannelSpecifics = NotificationDetails(iOS: iOSPlatformChannelSpecifics);
  await flutterLocalNotificationsPlugin.show(
    0, 'plain title', 'plain body', platformChannelSpecifics,
    payload: 'item x');

发送状态栏通知

在Android上发送状态栏通知

发送状态栏通知需要定义通知细节,包括通知的标题、内容、优先级和点击处理等,然后发送通知。下面是在Android上发送状态栏通知的步骤:

1.添加依赖项

要在Flutter应用程序中使用flutter_local_notifications插件,需要将其添加到应用程序的pubspec.yaml文件中。下面是pubspec.yaml文件中添加依赖项的示例代码:

dependencies:
  flutter:
    sdk: flutter
  flutter_local_notifications: ^14.0.0+1

2.编写通知

在构建状态栏通知之前,需要先定义通知组,然后定义通知细节,包括通知的标题、内容、图标和优先级等。

var androidPlatformChannelSpecifics = AndroidNotificationDetails(
        'your channel id', 'your channel name', 'your channel description',
        importance: Importance.max,
        priority: Priority.high,
        groupKey: 'com.android.example.WORK_EMAIL',
        ticker: 'ticker',
        styleInformation: BigTextStyleInformation(''),
        autoCancel: false,
        playSound: true,
        sound: RawResourceAndroidNotificationSound('slow_spring_board'),
        largeIcon: DrawableResourceAndroidBitmap('app_icon'),
        color: const Color.fromARGB(255, 255, 0, 0),
        ledColor: const Color.fromARGB(255, 255, 0, 0),
        ledOnMs: 1000,
        ledOffMs: 500,
var platformChannelSpecifics =
    NotificationDetails(android: androidPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
        'plain title', 
        'plain body', 
        platformChannelSpecifics,
        payload: 'item x',

3.发送通知

在向Android发送状态栏通知时,需要检查设备是否已经授予通知权限。如果设备没有授予通知权限,应该请求授予权限。

Future<void> checkPermission() async {
  var status = await flutterLocalNotificationsPlugin.getNotificationAppLaunchDetails();
  if (status?.didNotificationLaunchApp ?? false) {
    // handle notification tapped logic here
  var settings = await flutterLocalNotificationsPlugin.resolvePlatformSpecificImplementation<
      AndroidFlutterLocalNotificationsPlugin>()?.getActiveNotifications();
  if (settings == null || settings.isEmpty) {
    var requestedSettings = await flutterLocalNotificationsPlugin
        .resolvePlatformSpecificImplementation<
            AndroidFlutterLocalNotificationsPlugin>()
        ?.requestPermissions();
    if (requestedSettings ?? false) {
      // handle permission granted case here
    } else {
      // handle permission denied case here
  } else {
    // handle notification already enabled case here
Future<void> sendNotification() async {
      var androidPlatformChannelSpecifics = AndroidNotificationDetails(  
            'your channel id', 'your channel name', 'your channel description',  
            importance: Importance.max,  
            priority: Priority.high,  
            groupKey: 'com.android.example.WORK_EMAIL',  
            ticker: 'ticker',  
            styleInformation: BigTextStyleInformation(''),  
            autoCancel: false,  
            playSound: true,  
            sound: RawResourceAndroidNotificationSound('slow_spring_board'),  
            largeIcon: DrawableResourceAndroidBitmap('app_icon'),  
            color: const Color.fromARGB(255, 255, 0, 0),  
            ledColor: const Color.fromARGB(255, 255, 0, 0),  
            ledOnMs: 1000,  
            ledOffMs: 500,
    var platformChannelSpecifics =  
    NotificationDetails(android: androidPlatformChannelSpecifics);  
    await flutterLocalNotificationsPlugin.show(  
        'plain title', 
        'plain body', 
        platformChannelSpecifics,  
        payload: 'item x',

在iOS上发送状态栏通知

在iOS上发送状态栏通知需要定义通知细节,包括通知的标题、内容、优先级和点击处理等,然后发送通知。下面是在iOS上发送状态栏通知的步骤:

1.添加依赖项

要在Flutter应用程序中使用flutter_local_notifications插件,需要将其添加到应用程序的pubspec.yaml文件中。下面是pubspec.yaml文件中添加依赖项的示例代码:

dependencies:
  flutter:
    sdk: flutter
  flutter_local_notifications: ^14.0.0+1

2.编写通知

在构建状态栏通知之前,需要定义通知细节,包括通知的标题、内容、优先级和点击处理等。

var iOSPlatformChannelSpecifics = IOSNotificationDetails(
        sound: 'default',
        presentAlert: true,
        presentBadge: true,
        presentSound: true,
        badgeNumber: 1,
var platformChannelSpecifics =
    NotificationDetails(iOS: iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
        'plain title', 
        'plain body', 
        platformChannelSpecifics,
        payload: 'item x',

3.发送通知

在向iOS发送状态栏通知时,和Android发送通知不同,不需要检查通知权限。

Future<void> sendNotification() async {
  var iOSPlatformChannelSpecifics = IOSNotificationDetails(
      sound: 'default',
      presentAlert: true,
      presentBadge: true,
      presentSound: true,
      badgeNumber: 1,
  var platformChannelSpecifics =
      NotificationDetails(iOS: iOSPlatformChannelSpecifics);
  await flutterLocalNotificationsPlugin.show(
      'plain title', 
      'plain body', 
      platformChannelSpecifics,
      payload: 'item x',

通过本文给大家介绍了如何在Flutter中使用flutter_local_notifications插件发送系统通知和状态栏通知,包括在Android和iOS设备上分别发送通知的步骤和注意事项。

总的来说,flutter_local_notifications插件非常方便,为Flutter开发人员提供了发送通知的标准化解决方案,可以快速实现应用程序的通知功能。但是,在使用时需要注意保护用户隐私和安全,避免滥发通知,以及遵守相关规定和政策。🐶