Skip to content

Commit

Permalink
Remove stopOnNotificationOpen
Browse files Browse the repository at this point in the history
  • Loading branch information
gdelataillade committed Nov 16, 2023
1 parent f3f0f5c commit 22671f1
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 44 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ 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. 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
17 changes: 4 additions & 13 deletions lib/model/alarm_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ class AlarmSettings {
/// the user that the alarms won't ring anymore. Enabled by default.
final bool enableNotificationOnKill;

/// Stops the alarm on opened notification.
final bool stopOnNotificationOpen;

/// Whether to turn screen on when android alarm notification is triggered. Enabled by default.
final bool androidFullScreenIntent;

Expand All @@ -60,7 +57,6 @@ class AlarmSettings {
hash = hash ^ (notificationTitle.hashCode);
hash = hash ^ (notificationBody.hashCode);
hash = hash ^ enableNotificationOnKill.hashCode;
hash = hash ^ stopOnNotificationOpen.hashCode;
hash = hash & 0x3fffffff;

return hash;
Expand All @@ -82,7 +78,6 @@ class AlarmSettings {
required this.notificationTitle,
required this.notificationBody,
this.enableNotificationOnKill = true,
this.stopOnNotificationOpen = false,
this.androidFullScreenIntent = true,
});

Expand All @@ -97,9 +92,10 @@ class AlarmSettings {
fadeDuration: json['fadeDuration'] as double,
notificationTitle: json['notificationTitle'] as String,
notificationBody: json['notificationBody'] as String,
enableNotificationOnKill: json['enableNotificationOnKill'] as bool,
stopOnNotificationOpen: json['stopOnNotificationOpen'] as bool,
androidFullScreenIntent: json['androidFullScreenIntent'] as bool,
enableNotificationOnKill:
json['enableNotificationOnKill'] as bool? ?? true,
androidFullScreenIntent:
json['androidFullScreenIntent'] as bool? ?? true,
);

/// Creates a copy of `AlarmSettings` but with the given fields replaced with
Expand All @@ -115,7 +111,6 @@ class AlarmSettings {
String? notificationTitle,
String? notificationBody,
bool? enableNotificationOnKill,
bool? stopOnNotificationOpen,
bool? androidFullScreenIntent,
}) {
return AlarmSettings(
Expand All @@ -130,8 +125,6 @@ class AlarmSettings {
notificationBody: notificationBody ?? this.notificationBody,
enableNotificationOnKill:
enableNotificationOnKill ?? this.enableNotificationOnKill,
stopOnNotificationOpen:
stopOnNotificationOpen ?? this.stopOnNotificationOpen,
androidFullScreenIntent:
androidFullScreenIntent ?? this.androidFullScreenIntent,
);
Expand All @@ -149,7 +142,6 @@ class AlarmSettings {
'notificationTitle': notificationTitle,
'notificationBody': notificationBody,
'enableNotificationOnKill': enableNotificationOnKill,
'stopOnNotificationOpen': stopOnNotificationOpen,
'androidFullScreenIntent': androidFullScreenIntent,
};

Expand Down Expand Up @@ -178,6 +170,5 @@ class AlarmSettings {
notificationTitle == other.notificationTitle &&
notificationBody == other.notificationBody &&
enableNotificationOnKill == other.enableNotificationOnKill &&
stopOnNotificationOpen == other.stopOnNotificationOpen &&
androidFullScreenIntent == other.androidFullScreenIntent;
}
31 changes: 1 addition & 30 deletions lib/service/notification.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,45 +25,16 @@ class AlarmNotification {
requestAlertPermission: false,
requestSoundPermission: false,
requestBadgePermission: false,
onDidReceiveLocalNotification: onSelectNotificationOldIOS,
);
const initializationSettings = InitializationSettings(
android: initializationSettingsAndroid,
iOS: initializationSettingsIOS,
);

await localNotif.initialize(
initializationSettings,
onDidReceiveBackgroundNotificationResponse: onSelectNotification,
onDidReceiveNotificationResponse: onSelectNotification,
);
await localNotif.initialize(initializationSettings);
tz.initializeTimeZones();
}

// Callback to stop the alarm when the notification is opened.
static onSelectNotification(NotificationResponse notificationResponse) async {
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.
static onSelectNotificationOldIOS(
int? id,
String? _,
String? __,
String? ___,
) async {
if (id != null) await stopAlarm(id);
}

/// Stops the alarm.
static Future<void> stopAlarm(int id) async {
if (Alarm.getAlarm(id)?.stopOnNotificationOpen != null &&
Alarm.getAlarm(id)!.stopOnNotificationOpen) {
await Alarm.stop(id);
}
}

/// Shows notification permission request.
Future<bool> requestNotificationPermission() async {
if (defaultTargetPlatform == TargetPlatform.iOS) {
Expand Down

0 comments on commit 22671f1

Please sign in to comment.