반응형
특정 시간 알림 & 주기적인 알림 띄우기
- 우선 설치해야하는 패키지가 있다.
- 만약 이전에 local_notification을 설치했다면 패키지 설치는 생략 가능하며
- 아래의 코드만 가져와주기 (현재는 notification.dart에서 알림을 보내기에 여기서 작성)
import 'package:timezone/data/latest_all.dart' as tz; import 'package:timezone/timezone.dart' as tz;
- 그리고 아래의 코드를 작성해주기
- 갑자기 뭐가 에러가 잔뜩 생기면서 알람도 안켜지고, 앱이 꺼지면서 뭐라뭐라 하는데
- 첫 번째 이유
- 기존에 저장해둔 아이콘이 사라져있었던게 . (사라진 이유는 모르겠음…)
- 두 번째 이유
- 아이콘이 사라지면서 기존에 셋팅했던 아이콘 이름을 변경해줘야 적용이 되는 것 같음. (또한 이유를 모름 그냥 안되던데..)
showNotification2() async { // 시간 관련 함수를 사용하겠다는 함수를 실행 tz.initializeTimeZones(); // 아래 코드는 위의 showNotification 와 같다. var androidDetails = const AndroidNotificationDetails( '유니크한 알림 ID', '알림종류 설명', priority: Priority.high, importance: Importance.max, color: Color.fromARGB(255, 255, 0, 0), ); var iosDetails = const IOSNotificationDetails( presentAlert: true, presentBadge: true, presentSound: true, ); // 여기가 좀 다름 // tz.TZDateTime.now(tz.local) 는 현재 시간을 의미하고 // .add(Duration()) 으로 얼마나 뒤에 알람을 보낼 것인지를 조절할 수 있다. notifications.zonedSchedule( 2, '제목2', '내용2', tz.TZDateTime.now(tz.local).add(Duration(seconds: 5)), NotificationDetails(android: androidDetails, iOS: iosDetails), androidAllowWhileIdle: true, uiLocalNotificationDateInterpretation: UILocalNotificationDateInterpretation.absoluteTime ); }
- 이제 주기적으로 알림을 띄우고 싶다면
- zonedSchedule 에서 periodicallyShow 를 사용해야 함.
- // 주기적인 알림 코드 notifications.periodicallyShow( 2, '제목2', '내용2', RepeatInterval.everyMinute, NotificationDetails(android: androidDetails, iOS: iosDetails), androidAllowWhileIdle: true, );
- 다만 여기서도 매일 몇 시 몇 분에 알림을 보내는 기능은 periodicallyShow 에서 안되기에
- 해당 기능을 적용하기 위해선 zonedSchedule 로 변경 후 아래와 같이 코드 작성
- 아래의 코드를 본다면 현재 시각이 오후 5시의 경우 매일 5시마다 알림을 보내달라는 의미의 코드가 됨.
notifications.zonedSchedule( 2, '제목2', '내용2', tz.TZDateTime.now(tz.local), NotificationDetails(android: androidDetails, iOS: iosDetails), androidAllowWhileIdle: true, uiLocalNotificationDateInterpretation: UILocalNotificationDateInterpretation.absoluteTime, matchDateTimeComponents: DateTimeComponents.time // <- 해당 코드 추가 );
- 만약 matchDateTimeComponents: DateTimeComponents.dayOfWeekAndTime 이라면 현재 월요일 오후 5시라면 매주 월요일 오후 5시에 알림을 보내달라는 의미가 됨.
- 또 여기서 현재 시간에 대한 것이 아닌, 특정 시간에 알림을 계속 보내고 싶다면
- 아래와 같이 코드 작성하기
- makeDate 함수로 특정 시간을 기입해주기
showNotification2() async { // 시간 관련 함수를 사용하겠다는 함수를 실행 tz.initializeTimeZones(); // 아래 코드는 위의 showNotification 와 같다. var androidDetails = const AndroidNotificationDetails( '유니크한 알림 ID', '알림종류 설명', priority: Priority.high, importance: Importance.max, color: Color.fromARGB(255, 255, 0, 0), ); var iosDetails = const IOSNotificationDetails( presentAlert: true, presentBadge: true, presentSound: true, ); // 여기가 좀 다름 // tz.TZDateTime.now(tz.local) 는 현재 시간을 의미하고 // .add(Duration()) 으로 얼마나 뒤에 알람을 보낼 것인지를 조절할 수 있다. notifications.zonedSchedule( 2, '제목2', '내용2', makeDate(7,20,0), NotificationDetails(android: androidDetails, iOS: iosDetails), androidAllowWhileIdle: true, uiLocalNotificationDateInterpretation: UILocalNotificationDateInterpretation.absoluteTime, matchDateTimeComponents: DateTimeComponents.time ); } makeDate(hour, min, sec){ var now = tz.TZDateTime.now(tz.local); var when = tz.TZDateTime(tz.local, now.year, now.month, now.day, hour, min, sec); if (when.isBefore(now)) { return when.add(Duration(days: 1)); } else { return when; } }
'Flutter' 카테고리의 다른 글
[Flutter] Firebase에 데이터 저장 & 조회 & 삭제 & 수정 (0) | 2023.08.15 |
---|---|
[Flutter] Firebase 셋팅 (다수 에러 해결 과정) (0) | 2023.08.15 |
[Flutter] Local notification 알림 주는 법 (0) | 2023.08.14 |
[Flutter] GridView, CustomScrollView 프로필 페이지 만들기 (0) | 2023.08.14 |
[Flutter] Store 여러개 & GET 요청 (Provider) (0) | 2023.08.14 |