[Flutter] flutter_local_notifications 13.0.0

박망키·2023년 1월 31일
0

Flutter 야금야금 먹기

목록 보기
91/97
post-custom-banner
<script>
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:get/get.dart';
import 'package:timezone/data/latest_all.dart' as tz;
import 'package:timezone/timezone.dart' as tz;

FlutterLocalNotificationsPlugin localNotification =
    FlutterLocalNotificationsPlugin();
const AndroidNotificationChannel channel = AndroidNotificationChannel(
  'high_importance_channel', // id
  'High Importance Notifications', // title
  description:
      'This channel is used for important notifications.', // description
  importance: Importance.max,
);

//1. 앱로드시 실행할 기본설정
initNotification() async {
  FlutterLocalNotificationsPlugin localNotification =
      FlutterLocalNotificationsPlugin();
  AndroidInitializationSettings initSettingsAndroid =
      const AndroidInitializationSettings('@mipmap/ic_launcher');
  DarwinInitializationSettings initSettingsIOS =
      const DarwinInitializationSettings(
    requestSoundPermission: false,
    requestBadgePermission: false,
    requestAlertPermission: false,
  );
  InitializationSettings initSettings = InitializationSettings(
    android: initSettingsAndroid,
    iOS: initSettingsIOS,
  );
  await localNotification.initialize(
    initSettings,
  );
}

NotificationDetails _details = const NotificationDetails(
  android: AndroidNotificationDetails('alarm 1', '1번 푸시',
      color: Color(0xff00BA49),
      // ongoing: true,
      enableLights: true,
      priority: Priority.high,
      importance: Importance.max),
  iOS: DarwinNotificationDetails(
    presentAlert: true,
    presentBadge: true,
    presentSound: true,
  ),
);

tz.TZDateTime _timeZoneSetting({
  required int hour,
  required int minute,
}) {
  tz.initializeTimeZones();
  tz.setLocalLocation(tz.getLocation('Asia/Seoul'));
  tz.TZDateTime now = tz.TZDateTime.now(tz.local);
  tz.TZDateTime scheduledDate =
      tz.TZDateTime(tz.local, now.year, now.month, now.day, hour, minute);

  return scheduledDate;
}

///알림 실행하기
Future<void> pushAlarm() async {
  // 알림 id, 제목, 내용
  await localNotification.show(
    1,
    '제목1',
    '내용1',
    _details,
  );
}

///푸시알림 실행하기
Future<void> selectedDatePushAlarm(
    {required int hour,
    required int min,
    required String title,
    required String subtitle}) async {
  await localNotification.zonedSchedule(
    2,
    title,
    subtitle,
    _timeZoneSetting(
      hour: hour,
      minute: min,
    ),
    _details,
    uiLocalNotificationDateInterpretation:
        UILocalNotificationDateInterpretation.absoluteTime,
    androidAllowWhileIdle: true,
    matchDateTimeComponents: DateTimeComponents.time,
  );
}

///해당 아이디 푸시알림 삭제
Future<void> deletePushAlarm({required int id}) async {
  localNotification.cancel(id);
}

///ios 푸시 메시지 권한설정
Future<void> _requestPermissions() async {
  await localNotification
      .resolvePlatformSpecificImplementation<
          IOSFlutterLocalNotificationsPlugin>()
      ?.requestPermissions(
        alert: true,
        badge: true,
        sound: true,
      );
}
</script>
profile
무럭무럭 자라는 망키
post-custom-banner

0개의 댓글