Skip to content

Commit 2225170

Browse files
iballanMaikuB
andauthored
[flutter_local_notifications] Feature: Android - Add useAlarmClock mode to ScheduleMode (#1997)
* Add alarmClock mode to ScheduleMode Allowing exact alarm even in low power mode * Address PR CR * Address PR CR: Add example of using AlarmClock * update text for alarm clock example for consistency and casing --------- Co-authored-by: Michael Bui <[email protected]>
1 parent cb07cdd commit 2225170

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

flutter_local_notifications/android/src/main/java/com/dexterous/flutterlocalnotifications/FlutterLocalNotificationsPlugin.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,9 @@ private static void setupAlarm(
680680
checkCanScheduleExactAlarms(alarmManager);
681681
AlarmManagerCompat.setExact(
682682
alarmManager, AlarmManager.RTC_WAKEUP, epochMilli, pendingIntent);
683+
} else if (notificationDetails.scheduleMode.useAlarmClock()) {
684+
AlarmManagerCompat.setAlarmClock(
685+
alarmManager, epochMilli, pendingIntent, pendingIntent);
683686
} else {
684687
alarmManager.set(AlarmManager.RTC_WAKEUP, epochMilli, pendingIntent);
685688
}
@@ -695,6 +698,9 @@ private static void setupAllowWhileIdleAlarm(
695698
checkCanScheduleExactAlarms(alarmManager);
696699
AlarmManagerCompat.setExactAndAllowWhileIdle(
697700
alarmManager, AlarmManager.RTC_WAKEUP, epochMilli, pendingIntent);
701+
} else if (notificationDetails.scheduleMode.useAlarmClock()) {
702+
AlarmManagerCompat.setAlarmClock(
703+
alarmManager, epochMilli, pendingIntent, pendingIntent);
698704
} else {
699705
AlarmManagerCompat.setAndAllowWhileIdle(
700706
alarmManager, AlarmManager.RTC_WAKEUP, epochMilli, pendingIntent);

flutter_local_notifications/android/src/main/java/com/dexterous/flutterlocalnotifications/models/ScheduleMode.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
@Keep
1010
public enum ScheduleMode {
11+
alarmClock,
1112
exact,
1213
exactAllowWhileIdle,
1314
inexact,
@@ -21,6 +22,10 @@ public boolean useExactAlarm() {
2122
return this == exact || this == exactAllowWhileIdle;
2223
}
2324

25+
public boolean useAlarmClock() {
26+
return this == alarmClock;
27+
}
28+
2429
public static class Deserializer implements JsonDeserializer<ScheduleMode> {
2530
@Override
2631
public ScheduleMode deserialize(

flutter_local_notifications/example/lib/main.dart

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,14 @@ class _HomePageState extends State<HomePage> {
434434
await _zonedScheduleNotification();
435435
},
436436
),
437+
PaddedElevatedButton(
438+
buttonText:
439+
'Schedule notification to appear in 5 seconds '
440+
'based on local time zone using alarm clock',
441+
onPressed: () async {
442+
await _zonedScheduleAlarmClockNotification();
443+
},
444+
),
437445
PaddedElevatedButton(
438446
buttonText: 'Repeat notification every minute',
439447
onPressed: () async {
@@ -1383,6 +1391,21 @@ class _HomePageState extends State<HomePage> {
13831391
UILocalNotificationDateInterpretation.absoluteTime);
13841392
}
13851393

1394+
Future<void> _zonedScheduleAlarmClockNotification() async {
1395+
await flutterLocalNotificationsPlugin.zonedSchedule(
1396+
123,
1397+
'scheduled alarm clock title',
1398+
'scheduled alarm clock body',
1399+
tz.TZDateTime.now(tz.local).add(const Duration(seconds: 5)),
1400+
const NotificationDetails(
1401+
android: AndroidNotificationDetails(
1402+
'alarm_clock_channel', 'Alarm Clock Channel',
1403+
channelDescription: 'Alarm Clock Notification')),
1404+
androidScheduleMode: AndroidScheduleMode.alarmClock,
1405+
uiLocalNotificationDateInterpretation:
1406+
UILocalNotificationDateInterpretation.absoluteTime);
1407+
}
1408+
13861409
Future<void> _showNotificationWithNoSound() async {
13871410
const AndroidNotificationDetails androidNotificationDetails =
13881411
AndroidNotificationDetails('silent channel id', 'silent channel name',

flutter_local_notifications/lib/src/platform_specifics/android/schedule_mode.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
/// This leverages the use of alarms to schedule notifications as described
44
/// at https://developer.android.com/training/scheduling/alarms
55
enum AndroidScheduleMode {
6+
/// Used to specify that the notification should be scheduled to be shown at
7+
/// the exact time specified AND will execute whilst device is in
8+
/// low-power idle mode. Requires SCHEDULE_EXACT_ALARM permission.
9+
alarmClock,
10+
611
/// Used to specify that the notification should be scheduled to be shown at
712
/// the exact time specified but may not execute whilst device is in
813
/// low-power idle mode.

0 commit comments

Comments
 (0)