Flutter 簡單實作Local notification

Flutter Oct 1, 2020

首先新增庫依賴

flutter_local_notifications | Flutter Package
flutter_local_notifications - A cross platform plugin for displaying and scheduling local notifications for Flutter applications with the ability to customise for each platform.

在需要使用的地方import

import 'package:flutter_local_notifications/flutter_local_notifications.dart';

初始化

WidgetsFlutterBinding.ensureInitialized();

  var initAndroidSetting = AndroidInitializationSettings('@mipmap/ic_launcher');
  var initIosSetting = IOSInitializationSettings();
  var initSetting = InitializationSettings(initAndroidSetting, initIosSetting);
  await FlutterLocalNotificationsPlugin().initialize(initSetting);

新增一個函式方便呼叫

Future<void> showNotification(int counter) async {
    var android = AndroidNotificationDetails(
        'channelId', 'channelName', 'channelDescription');
    var iOS = IOSNotificationDetails();
    var platform = NotificationDetails(android, iOS);

    await FlutterLocalNotificationsPlugin().show(0, 'Counter', counter.toString(), platform);
  }

效果

可詳見Github

der1598c/Flutter_Exploration
This is my first Flutter exploration repo(contained basic skill for flutter code). - der1598c/Flutter_Exploration

Tags