Skip to content

Commit

Permalink
Fix failed schedule notifications (issue #79)
Browse files Browse the repository at this point in the history
  • Loading branch information
jenspfahl committed Aug 6, 2024
1 parent abc0f0d commit 555068a
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 15 deletions.
13 changes: 13 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,22 @@
<meta-data
android:name="flutterEmbedding"
android:value="2" />

<receiver android:exported="false" android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver" />
<receiver android:exported="false" android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.MY_PACKAGE_REPLACED"/>
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
</intent-filter>
</receiver>
<receiver android:exported="false" android:name="com.dexterous.flutterlocalnotifications.ActionBroadcastReceiver" />
</application>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.USE_EXACT_ALARM" />

<queries>
<intent>
Expand Down
45 changes: 31 additions & 14 deletions lib/service/LocalNotificationService.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:convert';
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:flutter_timezone/flutter_timezone.dart';
import 'package:flutter_translate/flutter_translate.dart';
import 'package:personaltasklogger/model/Schedule.dart';
import 'package:timezone/data/latest.dart' as tz;
Expand Down Expand Up @@ -118,6 +119,9 @@ class LocalNotificationService {
InitializationSettings(android: initializationSettingsAndroid);

tz.initializeTimeZones();
final String currentTimeZone = await FlutterTimezone.getLocalTimezone();
debugPrint("currentTimeZone=$currentTimeZone");
tz.setLocalLocation(tz.getLocation(currentTimeZone));

await _flutterLocalNotificationsPlugin.initialize(initializationSettings,
onDidReceiveNotificationResponse: (NotificationResponse response) async {
Expand Down Expand Up @@ -145,33 +149,45 @@ class LocalNotificationService {

Future<void> scheduleNotification(String receiverKey, int id, String title, message, Duration duration, String channelId,
[Color? color, List<AndroidNotificationAction>? actions, bool withTranslation = true, CustomRepetition? snoozePeriod]) async {
final when = tz.TZDateTime.now(tz.local).add(duration);

if (when.isBefore(DateTime.now())) {
debugPrint("Scheduled notification $id in the past, skip ($when)");
if (duration.isNegative || duration.inSeconds < 10) {
debugPrint(
"Scheduled notification $id in the past, skip");
return;
}

final now = DateTime.now().add(duration);

var zonedTime = tz.TZDateTime.local(now.year, now.month, now.day, now.hour, now.minute, now.second);
if (duration.inMinutes >= 1) {
// truncate to full minutes if duration greater than one minute
zonedTime = zonedTime.subtract(Duration(seconds: zonedTime.second));
}

final parameterMap = {
'receiverKey' : receiverKey,
'id' : id,
'title' : title,
'message' : message,
'channelId' : channelId,
'color' : color?.value,
'actions' : actions?.map((a) => a.id + "-" + a.showsUserInterface.toString() + "-" + a.title).toList(),
'snooze_period_value' : snoozePeriod?.repetitionValue,
'snooze_period_unit' : snoozePeriod?.repetitionUnit.index,
'receiverKey': receiverKey,
'id': id,
'title': title,
'message': message,
'channelId': channelId,
'color': color?.value,
'actions': actions?.map((a) =>
a.id + "-" + a.showsUserInterface.toString() + "-" + a.title).toList(),
'snooze_period_value': snoozePeriod?.repetitionValue,
'snooze_period_unit': snoozePeriod?.repetitionUnit.index,
};
final parametersAsJson = jsonEncode(parameterMap);

debugPrint("Schedule $id ($zonedTime), duration=${duration} tz=${tz.local}");


await _flutterLocalNotificationsPlugin.zonedSchedule(
id,
title,
message,
when.subtract(Duration(seconds: when.second)), // trunc seconds
zonedTime,
NotificationDetails(android: _createNotificationDetails(color, channelId, false, actions, withTranslation)),
androidAllowWhileIdle: true,
androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle,
uiLocalNotificationDateInterpretation: UILocalNotificationDateInterpretation.absoluteTime,
payload: receiverKey + "-" + id.toString() + RESCHEDULE_JSON_START_MARKER + parametersAsJson);
}
Expand Down Expand Up @@ -209,6 +225,7 @@ class LocalNotificationService {
});
}


void _handlePayload(bool isAppLaunch, String payload, String? actionId) {
debugPrint("_handlePayload=$payload $isAppLaunch");

Expand Down
2 changes: 1 addition & 1 deletion lib/service/PreferenceService.dart
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ class PreferenceService implements ITranslatePreferences {
KeyValueRepository.update(keyValue);
}
else {
KeyValueRepository.insert(KeyValue(null, key, value));
KeyValueRepository.insert(KeyValue(null, key, value.toString()));
}
});

Expand Down
2 changes: 2 additions & 0 deletions metadata/en-US/changelogs/10701.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* Fix Schedule notification bug #79
* (Upgrade target version and dependencies)
8 changes: 8 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,14 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
flutter_timezone:
dependency: "direct main"
description:
name: flutter_timezone
sha256: "0cb5498dedfaac615c779138194052f04524c31d958fab33d378f22b6cc14686"
url: "https://pub.dev"
source: hosted
version: "2.1.0"
flutter_translate:
dependency: "direct main"
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ dependencies:
flutter_localizations:
sdk: flutter
flutter_local_notifications: ^17.2.1+2
flutter_timezone: ^2.1.0
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.2
Expand Down

0 comments on commit 555068a

Please sign in to comment.