Skip to content

Commit

Permalink
[flutter_local_notifications] Feature: Android - Add useAlarmClock mo…
Browse files Browse the repository at this point in the history
…de 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 <25263378+MaikuB@users.noreply.github.com>
  • Loading branch information
iballan and MaikuB committed May 20, 2023
1 parent cb07cdd commit 2225170
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,9 @@ private static void setupAlarm(
checkCanScheduleExactAlarms(alarmManager);
AlarmManagerCompat.setExact(
alarmManager, AlarmManager.RTC_WAKEUP, epochMilli, pendingIntent);
} else if (notificationDetails.scheduleMode.useAlarmClock()) {
AlarmManagerCompat.setAlarmClock(
alarmManager, epochMilli, pendingIntent, pendingIntent);
} else {
alarmManager.set(AlarmManager.RTC_WAKEUP, epochMilli, pendingIntent);
}
Expand All @@ -695,6 +698,9 @@ private static void setupAllowWhileIdleAlarm(
checkCanScheduleExactAlarms(alarmManager);
AlarmManagerCompat.setExactAndAllowWhileIdle(
alarmManager, AlarmManager.RTC_WAKEUP, epochMilli, pendingIntent);
} else if (notificationDetails.scheduleMode.useAlarmClock()) {
AlarmManagerCompat.setAlarmClock(
alarmManager, epochMilli, pendingIntent, pendingIntent);
} else {
AlarmManagerCompat.setAndAllowWhileIdle(
alarmManager, AlarmManager.RTC_WAKEUP, epochMilli, pendingIntent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

@Keep
public enum ScheduleMode {
alarmClock,
exact,
exactAllowWhileIdle,
inexact,
Expand All @@ -21,6 +22,10 @@ public boolean useExactAlarm() {
return this == exact || this == exactAllowWhileIdle;
}

public boolean useAlarmClock() {
return this == alarmClock;
}

public static class Deserializer implements JsonDeserializer<ScheduleMode> {
@Override
public ScheduleMode deserialize(
Expand Down
23 changes: 23 additions & 0 deletions flutter_local_notifications/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,14 @@ class _HomePageState extends State<HomePage> {
await _zonedScheduleNotification();
},
),
PaddedElevatedButton(
buttonText:
'Schedule notification to appear in 5 seconds '
'based on local time zone using alarm clock',
onPressed: () async {
await _zonedScheduleAlarmClockNotification();
},
),
PaddedElevatedButton(
buttonText: 'Repeat notification every minute',
onPressed: () async {
Expand Down Expand Up @@ -1383,6 +1391,21 @@ class _HomePageState extends State<HomePage> {
UILocalNotificationDateInterpretation.absoluteTime);
}

Future<void> _zonedScheduleAlarmClockNotification() async {
await flutterLocalNotificationsPlugin.zonedSchedule(
123,
'scheduled alarm clock title',
'scheduled alarm clock body',
tz.TZDateTime.now(tz.local).add(const Duration(seconds: 5)),
const NotificationDetails(
android: AndroidNotificationDetails(
'alarm_clock_channel', 'Alarm Clock Channel',
channelDescription: 'Alarm Clock Notification')),
androidScheduleMode: AndroidScheduleMode.alarmClock,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime);
}

Future<void> _showNotificationWithNoSound() async {
const AndroidNotificationDetails androidNotificationDetails =
AndroidNotificationDetails('silent channel id', 'silent channel name',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
/// This leverages the use of alarms to schedule notifications as described
/// at https://developer.android.com/training/scheduling/alarms
enum AndroidScheduleMode {
/// Used to specify that the notification should be scheduled to be shown at
/// the exact time specified AND will execute whilst device is in
/// low-power idle mode. Requires SCHEDULE_EXACT_ALARM permission.
alarmClock,

/// Used to specify that the notification should be scheduled to be shown at
/// the exact time specified but may not execute whilst device is in
/// low-power idle mode.
Expand Down

0 comments on commit 2225170

Please sign in to comment.