Skip to content

Commit

Permalink
Add Android fullscreen intent notification (#99)
Browse files Browse the repository at this point in the history
* Add androidFullScreenIntent parameter

* Increment plugin version to 2.1.0
  • Loading branch information
gdelataillade committed Oct 3, 2023
1 parent 84c77b9 commit a2b7368
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 22 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.1.0
* [Android] Add parameter [androidFullScreenIntent] that turns screen on when alarm notification is triggered.
* Disable [stopOnNotificationOpen] by default.

## 2.0.1
* Update README.
* Fix example app's ring now button.
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
1 change: 1 addition & 0 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +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"/>
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT"/>
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"/>
<application>
<service
Expand Down
3 changes: 3 additions & 0 deletions example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +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"/>
<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
2 changes: 1 addition & 1 deletion example/lib/screens/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class _ExampleAlarmHomeScreenState extends State<ExampleAlarmHomeScreen> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('alarm 2.0.1')),
appBar: AppBar(title: const Text('alarm 2.1.0')),
body: SafeArea(
child: alarms.isNotEmpty
? ListView.separated(
Expand Down
14 changes: 7 additions & 7 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ packages:
path: ".."
relative: true
source: path
version: "2.0.1"
version: "2.1.0"
android_alarm_manager_plus:
dependency: transitive
description:
Expand Down Expand Up @@ -472,10 +472,10 @@ packages:
dependency: transitive
description:
name: uuid
sha256: e03928880bdbcbf496fb415573f5ab7b1ea99b9b04f669c01104d085893c3134
sha256: b715b8d3858b6fa9f68f87d20d98830283628014750c2b09b6f516c1da4af2a7
url: "https://pub.dev"
source: hosted
version: "4.0.0"
version: "4.1.0"
vector_math:
dependency: transitive
description:
Expand All @@ -488,10 +488,10 @@ packages:
dependency: transitive
description:
name: vibration
sha256: d81f665bcb201f586c295a21f3fe8f1cb6dc32c81a213a99e9c714ec8e811ce5
sha256: ab6d26f6694ae0cf702b6d3d1b399570f2911eddb1132c8f82eeacb71a08ece2
url: "https://pub.dev"
source: hosted
version: "1.8.1"
version: "1.8.2"
volume_controller:
dependency: transitive
description:
Expand All @@ -512,10 +512,10 @@ packages:
dependency: transitive
description:
name: win32
sha256: c97defd418eef4ec88c0d1652cdce84b9f7b63dd7198e266d06ac1710d527067
sha256: "350a11abd2d1d97e0cc7a28a81b781c08002aa2864d9e3f192ca0ffa18b06ed3"
url: "https://pub.dev"
source: hosted
version: "5.0.8"
version: "5.0.9"
win32_registry:
dependency: transitive
description:
Expand Down
11 changes: 10 additions & 1 deletion help/INSTALL-ANDROID.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ Then, add the following to your `AndroidManifest.xml` within the `<manifest></ma
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<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 @@ -44,6 +44,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 @@ -81,7 +86,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 @@ -97,6 +103,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 @@ -113,6 +120,7 @@ class AlarmSettings {
String? notificationBody,
bool? enableNotificationOnKill,
bool? stopOnNotificationOpen,
bool? androidFullScreenIntent,
}) {
return AlarmSettings(
id: id ?? this.id,
Expand All @@ -128,6 +136,8 @@ class AlarmSettings {
enableNotificationOnKill ?? this.enableNotificationOnKill,
stopOnNotificationOpen:
stopOnNotificationOpen ?? this.stopOnNotificationOpen,
androidFullScreenIntent:
androidFullScreenIntent ?? this.androidFullScreenIntent,
);
}

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

/// Returns all the properties of `AlarmSettings` for debug purposes.
Expand Down Expand Up @@ -171,5 +182,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
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: alarm
description: A simple Flutter alarm manager plugin for both iOS and Android.
version: 2.0.1
version: 2.1.0
homepage: https://github.com/gdelataillade/alarm

environment:
Expand Down

0 comments on commit a2b7368

Please sign in to comment.