[flutter] local notification Quick Start

💜Dabo (개발자 다보)·2020년 4월 14일
4

Flutter Library

목록 보기
1/3

📌 최신 버전에 맞춰 다시 작성해뒀습니다!

https://velog.io/@adbr/flutter-local-notification-Quick-Start2



Package

https://pub.dev/packages/flutter_local_notifications#-readme-tab-


00. pubspec.yaml

dependencies:
  # notification (20.04.14 기준)
  flutter_local_notifications: ^1.4.1

01. [Android] AndroidManifest.xml add Permission

<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


02. main.dart 👩‍💻init

Before Code

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  ...

  runApp(LocalizedApp(delegate, MyApp()));
}

Init Code

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()));
}


03. WidgetPage 👩‍💻showNotification

1) Notification Output Code

단순출력 구현 (Default Output Code)

Widget(view)

...
	IconButton(
          icon: Icon(Icons.add),
          onPressed: showNotification,
        ),
...

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);
  }


2) Notification Shedule Output Code

Widget(view)

...
	IconButton(
          icon: Icon(Icons.add),
          onPressed: showNotification,
        ),
...

or initState()

 
  void initState() {
    super.initState();
    showNotification();
    ...
  }

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);
  }


Result


TL;DR

dependencies local notification
👇
android permission (AndroidManifest.xml)
👇
main init code (Initialization Settings)
👇
output code (Flutter Local Notifications Plugin)



🍯Tip

  • error나면 hot reload에 의존해서 저처럼 시간낭비하지말고..😥
    실행 종료 후 다시 시작해보세요.
  • 그래도 안된다면 vscode Ctrl+Shift+P 👉 Clean Project 해보세요!
profile
𝙸 𝚊𝚖 𝚊 𝚌𝚞𝚛𝚒𝚘𝚞𝚜 𝚍𝚎𝚟𝚎𝚕𝚘𝚙𝚎𝚛 𝚠𝚑𝚘 𝚎𝚗𝚓𝚘𝚢𝚜 𝚍𝚎𝚏𝚒𝚗𝚒𝚗𝚐 𝚊 𝚙𝚛𝚘𝚋𝚕𝚎𝚖. 🇰🇷👩🏻‍💻

8개의 댓글

comment-user-thumbnail
2020년 5월 6일

오오오😮😮😮 필요하던건데 감사합니다.

1개의 답글
comment-user-thumbnail
2021년 4월 17일

근데 이거랑 firebase push notification 이랑 뭐가 다른건가요? 이걸 쓰면 굳이 FCM 안쓰고 백앤드에서 보내주기만 하면 되나요?

2개의 답글