로컬 알림(Local Notification)은 Flutter 앱 내에서 사용자에게 알림을 표시하는 기능을 제공합니다.
이를 사용하여 앱이 백그라운드에서 실행 중이거나 화면이 꺼져 있을 때도 사용자에게 메시지를 전달할 수 있습니다.
아래는 Flutter에서 로컬 알림을 사용하는 간단한 구현 예제와 설명입니다.
※주의※ 지원되는 플랫폼
- Android
- 4.1 이상
- NotificationCompat API를 사용하여 구형 Android 장치도 실행할 수 있습니다.
- iOS
- 8.0 이상
- iOS 10 이전 버전에서는 플러그인 UILocalNotification API를 사용합니다.
iOS 10 이상에서는 UserNotification API를 (aka User Notifications Framework) 사용됩니다.
- macOS
- 10.11+ 이상
- On macOS versions older than 10.14, the plugin will use the NSUserNotification APIs. The UserNotification APIs (aka the User Notifications Framework) is used on macOS 10.14 or newer.
설치 방법
- VisualStudio Code 기준
1. Ctrl + Shift + P
2. Dart: Add Dependency -> Enter
3. flutter_local_notifications -> Enter
4. Finish
- [ANDROID] native setting
ensure the screen turns on and shows when the device is locked.( 잠김화면 알림 활성화 )
1. open AndroidManifest.xml file
2. modify xml file
<activity
android:showWhenLocked="true"
android:turnScreenOn="true">
- [IOS] native setting
1. open AppDelegate.swift file
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
}
GeneratedPluginRegistrant.register(with: self)
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
예제 코드
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Local Notifications Example'),
),
body: Center(
child: ElevatedButton(
onPressed: () {
showNotification();
},
child: Text('Show Notification'),
),
),
),
);
}
}
Future<void> showNotification() async {
final flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
const AndroidInitializationSettings initializationSettingsAndroid =
AndroidInitializationSettings('@mipmap/ic_launcher');
final InitializationSettings initializationSettings =
InitializationSettings(
android: initializationSettingsAndroid, iOS: null);
await flutterLocalNotificationsPlugin.initialize(initializationSettings);
const AndroidNotificationDetails androidPlatformChannelSpecifics =
AndroidNotificationDetails(
'your channel id',
'your channel name',
'your channel description',
importance: Importance.max,
priority: Priority.high,
);
const NotificationDetails platformChannelSpecifics =
NotificationDetails(android: androidPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0,
'Hello, Flutter',
'This is a local notification!',
platformChannelSpecifics,
);
}
'IT-FrontEnd > Flutter' 카테고리의 다른 글
flutter positional parameter constructor (0) | 2023.11.08 |
---|---|
flutter - Hive (0) | 2023.11.06 |
Flutter StateLessWidget, StatefulWidget (1) | 2023.10.29 |
flutter build.gradle (0) | 2023.10.26 |
Exception: Unable to find suitable Visual Studio toolchain. Please run `flutter doctor` for more details. (0) | 2023.10.25 |