Skip to content

Commit

Permalink
Add androidFullScreenIntent parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
gdelataillade committed Sep 29, 2023
1 parent 533ee98 commit 3f6e75d
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 16 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 3.0.0
**Breaking Changes**
* [Android] Add parameter [androidFullScreenIntent] that turns screen on when alarm notification is triggered.
* Disable [stopOnNotificationOpen] by default.

## 2.0.0
**Breaking Changes**
* Installation steps were updated in the README. Please make sure to follow them.
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ fadeDuration | `double` | Duration, in seconds, over which to fade the ala
notificationTitle | `String` | The title of the notification triggered when alarm rings if app is on background.
notificationBody | `String` | The body of the notification.
enableNotificationOnKill | `bool` | Whether to show a notification when application is killed to warn the user that the alarm he set may not ring. Enabled by default.
stopOnNotificationOpen | `bool` | Whether to stop the alarm when opening the received notification. Enabled by default.
stopOnNotificationOpen | `bool` | Whether to stop the alarm when opening the received notification. Disabled by default.
androidFullScreenIntent | `bool` | Whether to turn screen on when android alarm notification is triggered. Enabled by default.

The notification shown on alarm ring can be disabled simply by ignoring the parameters `notificationTitle` and `notificationBody`. However, if you want a notification to be triggered, you will have to provide **both of them**.

Expand Down
2 changes: 1 addition & 1 deletion android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<!-- For apps with targetSDK=31 (Android 12) -->
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT"/>
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/>
<application>
<service
Expand Down
5 changes: 4 additions & 1 deletion example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
package="com.gdelataillade.alarm.alarm_example">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<!-- For apps with targetSDK=31 (Android 12) -->
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT"/>
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/>
<application
android:label="alarm_example"
Expand Down Expand Up @@ -30,6 +31,8 @@
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:showWhenLocked="true"
android:turnScreenOn="true"
android:windowSoftInputMode="adjustResize">
<!-- Specifies an Android theme to apply to this Activity as soon as
the Android process has started. This theme is visible to the user
Expand Down
1 change: 0 additions & 1 deletion example/lib/screens/edit_alarm.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ class _ExampleAlarmEditScreenState extends State<ExampleAlarmEditScreen> {
notificationTitle: showNotification ? 'Alarm example' : null,
notificationBody: showNotification ? 'Your alarm ($id) is ringing' : null,
assetAudioPath: assetAudio,
stopOnNotificationOpen: false,
);
return alarmSettings;
}
Expand Down
13 changes: 11 additions & 2 deletions help/INSTALL-ANDROID.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ Then, add the following to your `AndroidManifest.xml` within the `<manifest></ma
```xml
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<!-- For apps with targetSDK=31 (Android 12) -->
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT"/>
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/>
```

## Step 3
Finally, within the `<application></application>` tags, add:
Now, within the `<application></application>` tags, add:

```xml
<service
Expand All @@ -43,3 +44,11 @@ Finally, within the `<application></application>` tags, add:
</intent-filter>
</receiver>
```

Finally, if you want your notifications to show in full screen even when the device is locked, add these attributes in `<activity>`:

```xml
<activity
android:showWhenLocked="true"
android:turnScreenOn="true">
```
1 change: 1 addition & 0 deletions lib/alarm.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class Alarm {
dateTime: alarmSettings.dateTime,
title: alarmSettings.notificationTitle!,
body: alarmSettings.notificationBody!,
fullScreenIntent: alarmSettings.androidFullScreenIntent,
);
}
}
Expand Down
16 changes: 14 additions & 2 deletions lib/model/alarm_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ class AlarmSettings {
/// Stops the alarm on opened notification.
final bool stopOnNotificationOpen;

/// Whether to turn screen on when android alarm notification is triggered. Enabled by default.
///
/// [notificationTitle] and [notificationBody] must not be null nor empty.
final bool androidFullScreenIntent;

/// Returns a hash code for this `AlarmSettings` instance using Jenkins hash function.
@override
int get hashCode {
Expand Down Expand Up @@ -86,7 +91,8 @@ class AlarmSettings {
this.notificationTitle,
this.notificationBody,
this.enableNotificationOnKill = true,
this.stopOnNotificationOpen = true,
this.stopOnNotificationOpen = false,
this.androidFullScreenIntent = true,
});

/// Constructs an `AlarmSettings` instance from the given JSON data.
Expand All @@ -102,6 +108,7 @@ class AlarmSettings {
notificationBody: json['notificationBody'] as String?,
enableNotificationOnKill: json['enableNotificationOnKill'] as bool,
stopOnNotificationOpen: json['stopOnNotificationOpen'] as bool,
androidFullScreenIntent: json['androidFullScreenIntent'] as bool,
);

/// Creates a copy of `AlarmSettings` but with the given fields replaced with
Expand All @@ -118,6 +125,7 @@ class AlarmSettings {
String? notificationBody,
bool? enableNotificationOnKill,
bool? stopOnNotificationOpen,
bool? androidFullScreenIntent,
}) {
return AlarmSettings(
id: id ?? this.id,
Expand All @@ -133,6 +141,8 @@ class AlarmSettings {
enableNotificationOnKill ?? this.enableNotificationOnKill,
stopOnNotificationOpen:
stopOnNotificationOpen ?? this.stopOnNotificationOpen,
androidFullScreenIntent:
androidFullScreenIntent ?? this.androidFullScreenIntent,
);
}

Expand All @@ -149,6 +159,7 @@ class AlarmSettings {
'notificationBody': notificationBody,
'enableNotificationOnKill': enableNotificationOnKill,
'stopOnNotificationOpen': stopOnNotificationOpen,
'androidFullScreenIntent': androidFullScreenIntent,
};

/// Returns all the properties of `AlarmSettings` for debug purposes.
Expand Down Expand Up @@ -176,5 +187,6 @@ class AlarmSettings {
notificationTitle == other.notificationTitle &&
notificationBody == other.notificationBody &&
enableNotificationOnKill == other.enableNotificationOnKill &&
stopOnNotificationOpen == other.stopOnNotificationOpen;
stopOnNotificationOpen == other.stopOnNotificationOpen &&
androidFullScreenIntent == other.androidFullScreenIntent;
}
21 changes: 13 additions & 8 deletions lib/service/notification.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:alarm/alarm.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
Expand Down Expand Up @@ -40,7 +42,8 @@ class AlarmNotification {

// Callback to stop the alarm when the notification is opened.
static onSelectNotification(NotificationResponse notificationResponse) async {
await stopAlarm(notificationResponse.id);
if (notificationResponse.id == null) return;
await stopAlarm(notificationResponse.id!);
}

// Callback to stop the alarm when the notification is opened for iOS versions older than 10.
Expand All @@ -49,13 +52,13 @@ class AlarmNotification {
String? _,
String? __,
String? ___,
) async =>
await stopAlarm(id);
) async {
if (id != null) await stopAlarm(id);
}

/// Stops the alarm.
static Future<void> stopAlarm(int? id) async {
if (id != null &&
Alarm.getAlarm(id)?.stopOnNotificationOpen != null &&
static Future<void> stopAlarm(int id) async {
if (Alarm.getAlarm(id)?.stopOnNotificationOpen != null &&
Alarm.getAlarm(id)!.stopOnNotificationOpen) {
await Alarm.stop(id);
}
Expand Down Expand Up @@ -94,24 +97,26 @@ class AlarmNotification {
required DateTime dateTime,
required String title,
required String body,
required bool fullScreenIntent,
}) async {
const iOSPlatformChannelSpecifics = DarwinNotificationDetails(
presentAlert: true,
presentBadge: false,
presentSound: false,
);

const androidPlatformChannelSpecifics = AndroidNotificationDetails(
final androidPlatformChannelSpecifics = AndroidNotificationDetails(
'alarm',
'alarm_plugin',
channelDescription: 'Alarm plugin',
importance: Importance.max,
priority: Priority.max,
playSound: false,
enableLights: true,
fullScreenIntent: fullScreenIntent,
);

const platformChannelSpecifics = NotificationDetails(
final platformChannelSpecifics = NotificationDetails(
android: androidPlatformChannelSpecifics,
iOS: iOSPlatformChannelSpecifics,
);
Expand Down

0 comments on commit 3f6e75d

Please sign in to comment.