Skip to content

Commit

Permalink
chore: upgrade flutter_local_notifications dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
Merrit committed Apr 9, 2024
1 parent 29c6534 commit 70c18fd
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 42 deletions.
2 changes: 1 addition & 1 deletion android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ flutter {

dependencies {
// flutter_local_notifications plugin requires desugaring
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.5'
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.2.2'

// Desugaring causes crashes on Android 12+, adding these `window`
// dependencies fixes the issue until it is resolved in Flutter.
Expand Down
25 changes: 25 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,30 @@
<meta-data
android:name="flutterEmbedding"
android:value="2" />

<!-- Required to show scheduled notifications. -->
<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>
<!-- /end Required to show scheduled notifications. -->

<!-- Required to handle the result of an action on a notification. -->
<receiver android:exported="false"
android:name="com.dexterous.flutterlocalnotifications.ActionBroadcastReceiver" />
</application>

<!-- Required to know when the device is rebooted. This is required to be
able to reschedule notifications upon a reboot. -->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<!-- Required to schedule exact alarms. -->
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
</manifest>
2 changes: 1 addition & 1 deletion android/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pluginManagement {

plugins {
id "dev.flutter.flutter-plugin-loader" version "1.0.0"
id "com.android.application" version "7.3.0" apply false
id "com.android.application" version "7.3.1" apply false
id "org.jetbrains.kotlin.android" version "1.7.10" apply false
}

Expand Down
40 changes: 31 additions & 9 deletions lib/src/notifications/cubit/notifications_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,9 @@ class NotificationsCubit extends Cubit<NotificationsState> {
return;
}

if (!state.permissionGranted) {
if (!state.notificationPermissionGranted) {
await _requestPermission();
if (!state.permissionGranted) {
if (!state.notificationPermissionGranted) {
log.t(
'Notifications permission not granted. Not scheduling notification.',
);
Expand Down Expand Up @@ -213,9 +213,9 @@ class NotificationsCubit extends Cubit<NotificationsState> {
return;
}

if (!state.permissionGranted) {
if (!state.notificationPermissionGranted) {
await _requestPermission();
if (!state.permissionGranted) {
if (!state.notificationPermissionGranted) {
log.t(
'Notifications permission not granted. Not showing notification.',
);
Expand Down Expand Up @@ -287,7 +287,7 @@ class NotificationsCubit extends Cubit<NotificationsState> {
Future<void> _requestPermission() async {
// Currently only Android requires permission.
if (defaultTargetPlatform != TargetPlatform.android) {
emit(state.copyWith(permissionGranted: true));
emit(state.copyWith(notificationPermissionGranted: true));
return;
}

Expand All @@ -297,16 +297,38 @@ class NotificationsCubit extends Cubit<NotificationsState> {

if (androidPlugin == null) return;

final bool? permissionGranted = await androidPlugin.requestPermission();
if (permissionGranted == null) return;
// final bool? notificationsPermissionGranted =
// await androidPlugin.requestNotificationsPermission();
// if (notificationsPermissionGranted == null) return;

if (permissionGranted) {
// if (notificationsPermissionGranted) {
// log.i('Notifications permission granted');
// } else {
// log.i('Notifications permission denied');
// }

// emit(state.copyWith(permissionGranted: notificationsPermissionGranted));

final bool notificationsPermissionGranted =
await androidPlugin.requestNotificationsPermission() ?? false;
final bool exactNotificationsPermissionGranted =
await androidPlugin.requestExactAlarmsPermission() ?? false;

if (notificationsPermissionGranted) {
log.i('Notifications permission granted');
} else {
log.i('Notifications permission denied');
}

emit(state.copyWith(permissionGranted: permissionGranted));
if (exactNotificationsPermissionGranted) {
log.i('Exact notifications permission granted');
} else {
log.i('Exact notifications permission denied');
}

emit(state.copyWith(
notificationPermissionGranted: notificationsPermissionGranted,
));
}

/// Schedule a notification on desktop.
Expand Down
10 changes: 8 additions & 2 deletions lib/src/notifications/cubit/notifications_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,24 @@ class NotificationsState with _$NotificationsState {
required bool enabled,
NotificationResponse? notificationResponse,

/// Whether the app has permission to show exact notifications.
required bool notificationExactPermissionGranted,

/// Whether the app has permission to show notifications.
required bool notificationPermissionGranted,

/// The number of overdue tasks.
///
/// Tracked so we don't update the icons if the number hasn't changed.
required int overdueTasksCount,
required bool permissionGranted,
}) = _NotificationsState;

factory NotificationsState.initial() {
return const NotificationsState(
enabled: true,
notificationExactPermissionGranted: false,
notificationPermissionGranted: false,
overdueTasksCount: 0,
permissionGranted: false,
);
}
}
22 changes: 10 additions & 12 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -410,20 +410,18 @@ packages:
flutter_local_notifications:
dependency: "direct main"
description:
path: flutter_local_notifications
ref: "868f15213458bcb65a70508691f091e9a37aa480"
resolved-ref: "868f15213458bcb65a70508691f091e9a37aa480"
url: "https://github.com/Merrit/flutter_local_notifications.git"
source: git
version: "14.1.3+1"
name: flutter_local_notifications
sha256: f9a05409385b77b06c18f200a41c7c2711ebf7415669350bb0f8474c07bd40d1
url: "https://pub.dev"
source: hosted
version: "17.0.0"
flutter_local_notifications_linux:
dependency: "direct main"
dependency: transitive
description:
path: flutter_local_notifications_linux
ref: afd8cb7a740e8bec638803867c1cee4c92b60967
resolved-ref: afd8cb7a740e8bec638803867c1cee4c92b60967
url: "https://github.com/Merrit/flutter_local_notifications.git"
source: git
name: flutter_local_notifications_linux
sha256: "33f741ef47b5f63cc7f78fe75eeeac7e19f171ff3c3df054d84c1e38bedb6a03"
url: "https://pub.dev"
source: hosted
version: "4.0.0+1"
flutter_local_notifications_platform_interface:
dependency: transitive
Expand Down
18 changes: 1 addition & 17 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,7 @@ dependencies:
sdk: flutter
flutter_adaptive_scaffold: ^0.1.2
flutter_bloc: ^8.0.1

# ---------------- Begin flutter_local_notifications override -------------- #
# Override flutter_local_notifications to use the fork with the fix for
# Flatpak until the fix is merged.
# See: https://github.com/MaikuB/flutter_local_notifications/issues/1757
flutter_local_notifications:
git:
url: https://github.com/Merrit/flutter_local_notifications.git
ref: 868f15213458bcb65a70508691f091e9a37aa480
path: flutter_local_notifications
flutter_local_notifications_linux:
git:
url: https://github.com/Merrit/flutter_local_notifications.git
ref: afd8cb7a740e8bec638803867c1cee4c92b60967
path: flutter_local_notifications_linux
# ----------------- End flutter_local_notifications override --------------- #

flutter_local_notifications: ^17.0.0
flutter_signin_button: ^2.0.0
flutter_slidable: ^3.0.0
flutter_window_close: ^0.2.2
Expand Down

0 comments on commit 70c18fd

Please sign in to comment.