From f553ecdab339c4b9290e4b7a8cff466ed1181890 Mon Sep 17 00:00:00 2001 From: Kieran Date: Sat, 31 May 2025 12:02:01 +0100 Subject: [PATCH] fix: notifications icon --- android/app/src/main/AndroidManifest.xml | 4 ++ lib/notifications.dart | 85 ++++++++++++------------ lib/widgets/notifications_button.dart | 3 +- 3 files changed, 50 insertions(+), 42 deletions(-) diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 3106e1b..8fa1b2f 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -1,6 +1,10 @@ + + diff --git a/lib/notifications.dart b/lib/notifications.dart index 3341a36..e560386 100644 --- a/lib/notifications.dart +++ b/lib/notifications.dart @@ -241,7 +241,7 @@ Future _showNotification( android: AndroidNotificationDetails( notification.android!.channelId ?? "fcm", "Push Notifications", - category: AndroidNotificationCategory.social + category: AndroidNotificationCategory.social, ), ), ); @@ -270,45 +270,48 @@ Future setupNotifications() async { final signer = ndk.accounts.getLoggedAccount()?.signer; if (signer != null) { - FirebaseMessaging.onMessage.listen(_onNotification); - //FirebaseMessaging.onBackgroundMessage(_onBackgroundNotification); - FirebaseMessaging.onMessageOpenedApp.listen(_onOpenMessage); - - final settings = await FirebaseMessaging.instance.requestPermission( - provisional: true, - ); - await FirebaseMessaging.instance.setAutoInitEnabled(true); - await FirebaseMessaging.instance - .setForegroundNotificationPresentationOptions( - alert: true, - badge: true, - sound: true, - ); - - if (Platform.isIOS) { - final apnsToken = await FirebaseMessaging.instance.getAPNSToken(); - if (apnsToken == null) { - throw "APNS token not availble"; - } - } - await _initLocalNotifications(); - - final pusher = Notepush(dotenv.env["NOTEPUSH_URL"]!, signer: signer); - FirebaseMessaging.instance.onTokenRefresh.listen((token) async { - developer.log("NEW TOKEN: $token"); - await pusher.register(token); - await pusher.setNotificationSettings(token, [30_311]); - }); - - final fcmToken = await FirebaseMessaging.instance.getToken(); - if (fcmToken == null) { - throw "Push token is null"; - } - await pusher.register(fcmToken); - await pusher.setNotificationSettings(fcmToken, [30_311]); - - notifications.value = await NotificationsState.init( - settings.authorizationStatus, - ); + await configureNotifications(signer); } } + +Future configureNotifications(EventSigner signer) async { + FirebaseMessaging.onMessage.listen(_onNotification); + //FirebaseMessaging.onBackgroundMessage(_onBackgroundNotification); + FirebaseMessaging.onMessageOpenedApp.listen(_onOpenMessage); + + final settings = await FirebaseMessaging.instance.requestPermission( + provisional: true, + ); + await FirebaseMessaging.instance.setAutoInitEnabled(true); + await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions( + alert: true, + badge: true, + sound: true, + ); + + if (Platform.isIOS) { + final apnsToken = await FirebaseMessaging.instance.getAPNSToken(); + if (apnsToken == null) { + throw "APNS token not availble"; + } + } + await _initLocalNotifications(); + + final pusher = Notepush(dotenv.env["NOTEPUSH_URL"]!, signer: signer); + FirebaseMessaging.instance.onTokenRefresh.listen((token) async { + developer.log("NEW TOKEN: $token"); + await pusher.register(token); + await pusher.setNotificationSettings(token, [30_311]); + }); + + final fcmToken = await FirebaseMessaging.instance.getToken(); + if (fcmToken == null) { + throw "Push token is null"; + } + await pusher.register(fcmToken); + await pusher.setNotificationSettings(fcmToken, [30_311]); + + notifications.value = await NotificationsState.init( + settings.authorizationStatus, + ); +} diff --git a/lib/widgets/notifications_button.dart b/lib/widgets/notifications_button.dart index 7c7f055..96938b4 100644 --- a/lib/widgets/notifications_button.dart +++ b/lib/widgets/notifications_button.dart @@ -17,7 +17,8 @@ class _NotificationsButtonWidget extends State { return ValueListenableBuilder( valueListenable: notifications, builder: (context, state, _) { - final isNotified = (state?.notifyKeys ?? []).contains(widget.pubkey); + if (state == null) return SizedBox(); + final isNotified = state.notifyKeys.contains(widget.pubkey); return IconButton( iconSize: 20, onPressed: () async {