📌 최신 버전에 맞춰 다시 작성해뒀습니다!
https://velog.io/@adbr/flutter-local-notification-Quick-Start2
https://pub.dev/packages/flutter_local_notifications#-readme-tab-
dependencies:
# notification (20.04.14 기준)
flutter_local_notifications: ^1.4.1
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="test.test.example">
...
<!-- local notification 1 -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.VIBRATE" />
<!-- local notification (optional) -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
...
<application>
...
<!-- local notification 2 -->
<receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
<receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver" />
...
</application>
...
<manifest/>
공식문서에선 1,2 사항만 적으라고 나와있으나,
공식문서에 나와있는 example AndroidManifest.xml
보면 추가적으로 permission을 줬기 때문에 혹시 몰라 더 추가함.
cf. 공식문서에 나와있는
AndroidManifest.xml
예제
https://github.com/MaikuB/flutter_local_notifications/blob/master/flutter_local_notifications/example/android/app/src/main/AndroidManifest.xml
void main() async {
WidgetsFlutterBinding.ensureInitialized();
...
runApp(LocalizedApp(delegate, MyApp()));
}
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
// notification
var initAndroidSetting = AndroidInitializationSettings('@mipmap/ic_launcher');
var initIosSetting = IOSInitializationSettings();
var initSetting = InitializationSettings(initAndroidSetting, initIosSetting);
await FlutterLocalNotificationsPlugin().initialize(initSetting);
...
runApp(LocalizedApp(delegate, MyApp()));
}
단순출력 구현 (Default Output Code)
...
IconButton(
icon: Icon(Icons.add),
onPressed: showNotification,
),
...
Future<void> showNotification() async {
var android = AndroidNotificationDetails(
'channelId', 'channelName', 'channelDescription');
var iOS = IOSNotificationDetails();
var platform = NotificationDetails(android, iOS);
await FlutterLocalNotificationsPlugin().show(0, 'title', 'body', platform);
}
...
IconButton(
icon: Icon(Icons.add),
onPressed: showNotification,
),
...
void initState() {
super.initState();
showNotification();
...
}
Future<void> showNotification() async {
var android = AndroidNotificationDetails(
'channelId', 'channelName', 'channelDescription');
var iOS = IOSNotificationDetails();
var platform = NotificationDetails(android, iOS);
await FlutterLocalNotificationsPlugin().schedule(0, 'title', 'body', DateTime.parse('2020-04-15 13:02:00'), platform);
}
dependencies local notification
👇
android permission (AndroidManifest.xml)
👇
main init code (Initialization Settings)
👇
output code (Flutter Local Notifications Plugin)
Ctrl+Shift+P
👉 Clean Project
해보세요!
오오오😮😮😮 필요하던건데 감사합니다.