|
1 | | -# Flutter Alarm Manager POC |
| 1 | +WIP |
2 | 2 |
|
3 | | - |
4 | | - |
5 | | -## Overview |
6 | | - |
7 | | -This project demonstrates the implementation of a robust background alarm scheduling system in a Flutter application. It utilizes method channels to communicate with native Android code, Jetpack Compose for the alarm notification UI, and a custom notification service for managing high-priority alarm notifications and display these notification full screen even from app-killed state and lock screen. |
8 | | - |
9 | | -### Key Features |
10 | | - |
11 | | -- Schedule alarms that run in the background. |
12 | | -- Display full screen notifications in Lock Screen and handling actions even in app-killed state |
13 | | -- Handle notification permissions. |
14 | | -- Use method channels for native code integration. |
15 | | -- Leverage Jetpack Compose for modern Android UI. |
16 | | -- Implement a persistent alarm system that works even when the app is closed. |
17 | | -- Custom notification service for creating and managing high-priority alarm notifications. |
18 | | - |
19 | | ---- |
20 | | - |
21 | | -## Project Structure |
22 | | - |
23 | | -### Flutter (Dart) Side |
24 | | - |
25 | | -- `lib/main.dart`: The entry point of the Flutter application. |
26 | | -- `lib/alarm_manager_screen.dart`: The main screen for scheduling alarms. |
27 | | -- `lib/alarm_actions_screen.dart`: A screen to view and manage scheduled alarms. |
28 | | -- `lib/utils/alarm_method_channel.dart`: Dart side of the method channel for communicating with native Android code. |
29 | | -- `lib/hive/models/alarm_action.dart`: Data Model to store the alarm action selected with its timestamp. |
30 | | -- `lib/hive/service/database_service.dart`: Contains code related to Hive DB for Crud Operations |
31 | | - |
32 | | -### Native Android (Kotlin) Side |
33 | | - |
34 | | -- `android/app/src/main/kotlin/.../MainActivity.kt`: Main activity for the Flutter app, handles method channel communication. |
35 | | -- `android/app/src/main/kotlin/.../AlarmActivity.kt`: Activity for displaying and handling alarms. |
36 | | -- `android/app/src/main/kotlin/.../AlarmScreen.kt`: Jetpack Compose Screen hosted in AlarmActivity |
37 | | -- `android/app/src/main/kotlin/.../model/AlarmItem.kt`: Data model for alarm items. |
38 | | -- `android/app/src/main/kotlin/.../alarmScheduler/AlarmScheduler.kt`: Interface for alarm scheduling. |
39 | | -- `android/app/src/main/kotlin/.../alarmScheduler/AlarmSchedulerImpl.kt`: Implementation of the alarm scheduling system. |
40 | | - |
41 | | -- `android/app/src/main/kotlin/.../AlarmNotificationService.kt`: Interface for the alarm notification service. |
42 | | -- `android/app/src/main/kotlin/.../AlarmNotificationServiceImpl.kt`: Implementation of the alarm notification service. |
43 | | - |
44 | | ---- |
45 | | - |
46 | | -## Key Components |
47 | | - |
48 | | -### `MainActivity` |
49 | | - |
50 | | -Handles Flutter-native method channel communication. This is the entry point for all native-side interactions from Flutter. |
51 | | - |
52 | | -### `AlarmActivity` |
53 | | - |
54 | | -Displays and handles the alarm UI using Jetpack Compose. This is where the user interacts with the alarm (snooze, accept). |
55 | | - |
56 | | -### `AlarmNotificationService` |
57 | | - |
58 | | -Responsible for creating and managing high-priority notifications for alarms. It ensures that alarm notifications are persistent, even on the lock screen. |
59 | | - |
60 | | -#### Key Responsibilities: |
61 | | - |
62 | | -- **Notification Channel Creation**: Creates a high-importance channel for alarms. Sets channel properties to bypass "Do Not Disturb," show on the lock screen, and enable lights and vibration. |
63 | | -- **Showing Notifications**: Creates a full-screen intent that launches the `AlarmActivity`. Builds a high-priority notification with a full-screen intent. The notification is ongoing and non-cancelable to ensure it persists until user interaction. |
64 | | - |
65 | | -```kotlin |
66 | | - |
67 | | - val notificationService: AlarmNotificationService = AlarmNotificationServiceImpl(context) |
68 | | - |
69 | | - val alarmItem = AlarmItem( |
70 | | - id = 1, |
71 | | - message = "Alarm has been ringing" |
72 | | - ) |
73 | | -// To display notification |
74 | | -notificationService.showNotification(AlarmItem(alarmId, message)) |
75 | | - |
76 | | -``` |
77 | | - |
78 | | -- **Cancelling Notifications**: Cancels a notification by its ID when alarms are dismissed or snoozed. |
79 | | - |
80 | | -```kotlin |
81 | | - |
82 | | - val notificationService: AlarmNotificationService = AlarmNotificationServiceImpl(context) |
83 | | - |
84 | | - val alarmItem = AlarmItem( |
85 | | - id = 1, |
86 | | - message = "Alarm has been ringing" |
87 | | - ) |
88 | | -// To cancel notification |
89 | | -notificationService.showNotification(AlarmItem(alarmId, message)) |
90 | | - |
91 | | -``` |
92 | | - |
93 | | ---- |
94 | | - |
95 | | -## Alarm Scheduler |
96 | | - |
97 | | -The `AlarmScheduler` interface and its implementation, `AlarmSchedulerImpl`, provide a simple and effective way to schedule and cancel alarms in an Android app. |
98 | | - |
99 | | -### Key Responsibilities: |
100 | | - |
101 | | -- **Scheduling and Canceling Alarms**: The `AlarmScheduler` is responsible for scheduling alarms to trigger at a specific time. It ensures that the necessary system components, such as `AlarmManager`, are used correctly to wake the device and trigger the alarm on time. |
102 | | - |
103 | | -```kotlin |
104 | | - |
105 | | - val alarmScheduler = AlarmSchedulerImpl(context) |
106 | | - val alarmItem = AlarmItem( |
107 | | - id = 1, |
108 | | - message = "Alarm has been ringing" |
109 | | - ) |
110 | | - // To schedule full screen notification alarms |
111 | | - alarmScheduler.schedule(alarmItem) |
112 | | - |
113 | | -``` |
114 | | - |
115 | | -- **Handling Exact Timings**: The scheduler is designed to handle precise alarm timings using Android's `AlarmManager.setExact()` method, which is suitable for alarms requiring exact triggers. |
116 | | - |
117 | | -- **Canceling Alarms**: The `AlarmScheduler` also provides functionality to cancel scheduled alarms. It ensures that any pending intents related to the alarm are properly canceled, preventing unnecessary alarms from firing. |
118 | | - |
119 | | -```kotlin |
120 | | - |
121 | | - val alarmScheduler = AlarmSchedulerImpl(context) |
122 | | - val alarmItem = AlarmItem( |
123 | | - id = 1, |
124 | | - message = "Alarm has been ringing" |
125 | | - ) |
126 | | - |
127 | | - // To cancel alarm |
128 | | - alarmScheduler.cancel(alarmItem) |
129 | | - |
130 | | -``` |
131 | | - |
132 | | -- **Managing Alarm Intents**: The `AlarmScheduler` is responsible for creating and managing `PendingIntent` objects that represent the scheduled alarms. These intents are used by the `AlarmManager` to trigger the alarm at the specified time. |
133 | | - |
134 | | -## AlarmReceiver |
135 | | - |
136 | | -The `AlarmReceiver` is a crucial component in the alarm management system. It extends `BroadcastReceiver`, enabling the app to respond to system-wide broadcast events, specifically alarms that are triggered by the `AlarmManager`. |
137 | | - |
138 | | -### Key Responsibilities |
139 | | - |
140 | | -**Receiving Broadcasts:** |
141 | | - |
142 | | -- The primary role of `AlarmReceiver` is to listen for broadcasts triggered when an alarm goes off, even if the app is in the background or closed. |
143 | | -- It receives `Intents` with alarm details such as the `ALARM_ID` and the `ALARM_MESSAGE`. |
144 | | - |
145 | | -**Handling the Alarm:** |
146 | | - |
147 | | -- Once the alarm is received, it extracts the `ALARM_ID` and `ALARM_MESSAGE` from the `Intent`. If no values are provided, defaults are used (`-1` for `ALARM_ID` and `"Alarm!"` for the message). |
148 | | -- It then creates an `AlarmItem` object to represent the triggered alarm. |
149 | | - |
150 | | -**Displaying Notifications:** |
151 | | - |
152 | | -- After receiving the alarm, the receiver creates an instance of `AlarmNotificationService` and invokes its `showNotification` method, which displays a notification to the user about the alarm. |
153 | | - |
154 | | -```kotlin |
155 | | -class AlarmReceiver : BroadcastReceiver() { |
156 | | - |
157 | | - |
158 | | - override fun onReceive(context: Context, intent: Intent?) { |
159 | | - val alarmId = intent?.getIntExtra("ALARM_ID", -1) ?: -1 |
160 | | - val message = intent?.getStringExtra("ALARM_MESSAGE") ?: "Alarm!" |
161 | | - val notificationService: AlarmNotificationService = AlarmNotificationServiceImpl(context) |
162 | | - notificationService.showNotification(AlarmItem(alarmId, message)) |
163 | | - } |
164 | | -} |
165 | | - |
166 | | -``` |
167 | | - |
168 | | ---- |
169 | | - |
170 | | -## Alarm Notification Flow |
171 | | - |
172 | | - |
173 | | - |
174 | | -<p style="text-align:center; font-weight:bold;">Flow of Notification Handling</p> |
175 | | - |
176 | | -### 1. **Alarm Set from Flutter** |
177 | | - |
178 | | -- The alarm is initially scheduled from the Flutter side using a **Method Channel**. |
179 | | -- Flutter sends a command to the native Android layer to schedule the alarm using the `AlarmScheduler`. |
180 | | - |
181 | | -### 2. **Alarm Trigger and Notification Creation** |
182 | | - |
183 | | -- When the scheduled time arrives, the alarm triggers, and the `AlarmReceiver` captures the broadcast. |
184 | | -- The receiver then creates an alarm notification via `AlarmNotificationService`, which shows a high-priority, full-screen notification on the device. |
185 | | -- The notification appears even if the device is locked. |
186 | | - |
187 | | -### 3. **User Interaction with the Alarm** |
188 | | - |
189 | | -- When the user interacts with the notification (e.g., accepting or snoozing), the `AlarmActivity` is launched. |
190 | | -- Inside `AlarmActivity`, the notification content is displayed via a Jetpack Compose `AlarmScreen` interface. |
191 | | -- The user can either **accept** or **snooze** the alarm, and these actions are captured within the `AlarmActivity`. |
192 | | - |
193 | | -### 4. **Handling Accept or Snooze Action** |
194 | | - |
195 | | -- Based on the user's action: |
196 | | - - **Accept**: The `alarmAccepted` method is invoked via the Method Channel, and the alarm is marked as accepted. |
197 | | - - **Snooze**: The `alarmSnoozed` method is called, which reschedules the alarm for a later time using the `AlarmScheduler`. |
198 | | -- In both cases, the notification is cancelled after the action is completed. |
199 | | - |
200 | | -### 5. **Sending Action Back to Flutter** |
201 | | - |
202 | | -- After the user selects either **accept** or **snooze**, the result of the action is sent back to the Flutter layer through the Method Channel. |
203 | | -- The Method Channel passes the action result (accept or snooze) back to Flutter, where it can be processed for further logic. |
204 | | - |
205 | | -### 6. **Storing the Action in Local Storage (Hive DB)** |
206 | | - |
207 | | -- Once the action result is received in Flutter, it is stored locally in the **Hive DB**. |
208 | | -- Hive is used to store key details like: |
209 | | - - Whether the alarm was accepted or snoozed. |
210 | | - - Timestamps of the actions for future reference or history tracking. |
211 | | -- This ensures that the user's interaction with the alarm is saved persistently, even if the app was closed or in a killed state when the alarm was triggered. |
212 | | - |
213 | | -### Key Points: |
214 | | - |
215 | | -- **Persistent Storage**: Using Hive for local storage ensures that even in cases where the app was closed, the alarm actions are reliably saved. |
216 | | -- **Flutter-Android Communication**: The **Method Channel** serves as the bridge for communication between the Flutter and native Android sides, ensuring seamless action handling and data transmission. |
217 | | -- **State Handling**: The system handles alarms gracefully, whether the app is in a running state or was previously killed, ensuring that alarms work in all scenarios. |
218 | | - |
219 | | ---- |
220 | | - |
221 | | -## Permissions |
222 | | - |
223 | | -### The app requires the following permissions to function correctly: |
224 | | - |
225 | | -```xml |
226 | | - <uses-permission android:name="android.permission.POST_NOTIFICATIONS" /> |
227 | | - <uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" /> |
228 | | - <uses-permission android:name="android.permission.USE_EXACT_ALARM" /> |
229 | | - <uses-permission android:name="android.permission.WAKE_LOCK" /> |
230 | | - <uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" /> |
231 | | -``` |
232 | | - |
233 | | -### Also add the following in the Notification Activity in your Manifest |
234 | | - |
235 | | -```xml |
236 | | - <activity |
237 | | - android:name=".activity.AlarmActivity" |
238 | | - android:showWhenLocked="true" |
239 | | - android:turnScreenOn="true" |
240 | | - android:exported="false" /> |
241 | | - |
242 | | -``` |
243 | | - |
244 | | -# Contributing |
245 | | - |
246 | | -### Contributions are welcome! Please feel free to submit a Pull Request. |
247 | | - |
248 | | ----- |
| 3 | +Forked from https://github.com/Applinx-Tech/Flutter-Alarm-Manager-POC. |
0 commit comments