<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',
'High Importance Notifications',
description:
'This channel is used for important notifications.',
importance: Importance.max,
);
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),
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 {
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);
}
Future<void> _requestPermissions() async {
await localNotification
.resolvePlatformSpecificImplementation<
IOSFlutterLocalNotificationsPlugin>()
?.requestPermissions(
alert: true,
badge: true,
sound: true,
);
}
</script>