Skip to content

Commit f12bc4e

Browse files
authored
Fix notifications on Android (#1700)
* bump flutter_local_notification_plugin to 17.2.3 * [CeremonyNotifications] ignore if registration is in the past, and log an error. * [NotificationPlugin] allow inexact notifications as android >13 doesn't allow exact without the proper permissions set and we don't need it. * [android] bump desugaring dep * [AndroidManifest] add proper permissions for scheduled notifications * [NotificationPlugin] also ask for notif permissions on android * better wording in comment
1 parent 1cb8497 commit f12bc4e

File tree

6 files changed

+41
-10
lines changed

6 files changed

+41
-10
lines changed

app/android/app/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,5 +106,5 @@ dependencies {
106106
// Bug: Failed to transform window-java-1.0.0-beta04.jar (androidx.window:window-java:1.0.0-beta04)
107107
implementation 'androidx.window:window:1.0.0'
108108
implementation 'androidx.window:window-java:1.0.0'
109-
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
109+
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.2.2'
110110
}

app/android/app/src/main/AndroidManifest.xml

+13
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,23 @@
4242
<meta-data
4343
android:name="flutterEmbedding"
4444
android:value="2" />
45+
46+
<!-- Enable display of schedule notifications -->
47+
<receiver android:exported="false" android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver" />
48+
<receiver android:exported="false" android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver">
49+
<intent-filter>
50+
<action android:name="android.intent.action.BOOT_COMPLETED"/>
51+
<action android:name="android.intent.action.MY_PACKAGE_REPLACED"/>
52+
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
53+
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
54+
</intent-filter>
55+
</receiver>
4556
</application>
4657
<uses-permission android:name="android.permission.INTERNET" />
4758
<!-- local_auth -->
4859
<uses-permission android:name="android.permission.USE_BIOMETRIC" />
60+
<!-- Needed to persist scheduled notifications after reboot -->
61+
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
4962
<queries>
5063
<!-- Needed to open URLs as of Android 11 -->
5164
<intent>

app/lib/service/notification/lib/src/ceremony_notifications.dart

+9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:encointer_wallet/l10n/l10.dart';
2+
import 'package:encointer_wallet/service/log/log_service.dart';
23
import 'package:encointer_wallet/service/notification/lib/notification.dart';
34

45
/// Manages meetups reminder notifications.
@@ -47,6 +48,14 @@ class CeremonyNotifications {
4748
int numberOfCyclesToSchedule = 5,
4849
String? cid,
4950
}) async {
51+
if (DateTime.now().isAfter(DateTime.fromMillisecondsSinceEpoch(nextRegisteringPhase))) {
52+
// Doesn't happen except occasionally on first app startup. So we don't care about it.
53+
Log.e(
54+
'[CeremonyNotifications] nextRegisteringPhase is in the past: ${DateTime.fromMillisecondsSinceEpoch(nextRegisteringPhase)}',
55+
);
56+
return;
57+
}
58+
5059
for (var i = 0; i < numberOfCyclesToSchedule; i++) {
5160
// calculate the scheduled date by adding i*ceremonyCycleDuration to nextRegisteringPhase
5261
final scheduledDate = DateTime.fromMillisecondsSinceEpoch(nextRegisteringPhase + i * ceremonyCycleDuration);

app/lib/service/notification/lib/src/notification_plugin.dart

+11-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ class NotificationPlugin {
3939
Log.d('notification_plugin initialised: $initialised', 'main.dart');
4040
}
4141

42-
static void init(BuildContext context) => _requestIOSPermissions();
42+
static void init(BuildContext context) {
43+
_requestAndroidPermissions();
44+
_requestIOSPermissions();
45+
}
4346

4447
static void _requestIOSPermissions() {
4548
flutterLocalNotificationsPlugin
@@ -51,6 +54,12 @@ class NotificationPlugin {
5154
);
5255
}
5356

57+
static void _requestAndroidPermissions() {
58+
flutterLocalNotificationsPlugin
59+
.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()
60+
?.requestNotificationsPermission();
61+
}
62+
5463
static AndroidNotificationDetails _androidPlatformChannelSpecifics(String body, String sound) {
5564
return AndroidNotificationDetails(
5665
'$sound channel id',
@@ -118,7 +127,7 @@ class NotificationPlugin {
118127
tz.TZDateTime.from(scheduledDate, tz.local),
119128
_platformChannelSpecifics(body, cid: cid),
120129
uiLocalNotificationDateInterpretation: UILocalNotificationDateInterpretation.absoluteTime,
121-
androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle,
130+
androidScheduleMode: AndroidScheduleMode.inexact,
122131
);
123132
}
124133
}

app/pubspec.lock

+6-6
Original file line numberDiff line numberDiff line change
@@ -542,26 +542,26 @@ packages:
542542
dependency: "direct main"
543543
description:
544544
name: flutter_local_notifications
545-
sha256: bb5cd63ff7c91d6efe452e41d0d0ae6348925c82eafd10ce170ef585ea04776e
545+
sha256: "49eeef364fddb71515bc78d5a8c51435a68bccd6e4d68e25a942c5e47761ae71"
546546
url: "https://pub.dev"
547547
source: hosted
548-
version: "16.2.0"
548+
version: "17.2.3"
549549
flutter_local_notifications_linux:
550550
dependency: transitive
551551
description:
552552
name: flutter_local_notifications_linux
553-
sha256: "33f741ef47b5f63cc7f78fe75eeeac7e19f171ff3c3df054d84c1e38bedb6a03"
553+
sha256: c49bd06165cad9beeb79090b18cd1eb0296f4bf4b23b84426e37dd7c027fc3af
554554
url: "https://pub.dev"
555555
source: hosted
556-
version: "4.0.0+1"
556+
version: "4.0.1"
557557
flutter_local_notifications_platform_interface:
558558
dependency: transitive
559559
description:
560560
name: flutter_local_notifications_platform_interface
561-
sha256: "7cf643d6d5022f3baed0be777b0662cce5919c0a7b86e700299f22dc4ae660ef"
561+
sha256: "85f8d07fe708c1bdcf45037f2c0109753b26ae077e9d9e899d55971711a4ea66"
562562
url: "https://pub.dev"
563563
source: hosted
564-
version: "7.0.0+1"
564+
version: "7.2.0"
565565
flutter_localizations:
566566
dependency: "direct main"
567567
description: flutter

app/pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ dependencies:
3333
mobx: ^2.3.0+1
3434
flutter_mobx: ^2.2.0+2
3535
shared_preferences: ^2.2.2
36-
flutter_local_notifications: ^16.2.0
36+
flutter_local_notifications: ^17.2.3
3737
flutter:
3838
sdk: flutter
3939
flutter_localizations:

0 commit comments

Comments
 (0)