From 3c049211ca9c789c5861a4f81483f6d053ad0002 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Ku=C3=9Fowski?= Date: Wed, 12 Nov 2025 08:51:21 +0100 Subject: [PATCH] chore: Follow up notification action isolate handling --- lib/config/app_config.dart | 3 +++ lib/main.dart | 7 +++++-- .../notification_background_handler.dart | 20 ++++++++++++++++++- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/lib/config/app_config.dart b/lib/config/app_config.dart index 4f5d42c51..709ecc946 100644 --- a/lib/config/app_config.dart +++ b/lib/config/app_config.dart @@ -56,4 +56,7 @@ abstract class AppConfig { host: 'fluffy.chat', path: '/en/privacy', ); + + static const String mainIsolatePortName = 'main_isolate'; + static const String pushIsolatePortName = 'push_isolate'; } diff --git a/lib/main.dart b/lib/main.dart index 00fe37474..b09375b8a 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,6 +1,8 @@ import 'dart:isolate'; import 'dart:ui'; +import 'package:fluffychat/config/app_config.dart'; +import 'package:fluffychat/utils/notification_background_handler.dart'; import 'package:flutter/material.dart'; import 'package:collection/collection.dart'; @@ -20,11 +22,12 @@ ReceivePort? mainIsolateReceivePort; void main() async { if (PlatformInfos.isAndroid) { final port = mainIsolateReceivePort = ReceivePort(); - IsolateNameServer.removePortNameMapping('main_isolate'); + IsolateNameServer.removePortNameMapping(AppConfig.mainIsolatePortName); IsolateNameServer.registerPortWithName( port.sendPort, - 'main_isolate', + AppConfig.mainIsolatePortName, ); + await waitForPushIsolateDone(); } // Our background push shared isolate accesses flutter-internal things very early in the startup proccess diff --git a/lib/utils/notification_background_handler.dart b/lib/utils/notification_background_handler.dart index a0fcd4792..1a694615a 100644 --- a/lib/utils/notification_background_handler.dart +++ b/lib/utils/notification_background_handler.dart @@ -1,4 +1,5 @@ import 'dart:convert'; +import 'dart:isolate'; import 'dart:ui'; import 'package:collection/collection.dart'; @@ -42,11 +43,20 @@ extension NotificationResponseJson on NotificationResponse { } } +Future waitForPushIsolateDone() async { + if (IsolateNameServer.lookupPortByName(AppConfig.pushIsolatePortName) != + null) { + Logs().i('Wait for Push Isolate to be done...'); + await Future.delayed(const Duration(milliseconds: 300)); + } +} + @pragma('vm:entry-point') void notificationTapBackground( NotificationResponse notificationResponse, ) async { - final sendPort = IsolateNameServer.lookupPortByName('main_isolate'); + final sendPort = + IsolateNameServer.lookupPortByName(AppConfig.mainIsolatePortName); if (sendPort != null) { sendPort.send(notificationResponse.toJsonString()); Logs().i('Notification tap sent to main isolate!'); @@ -56,6 +66,12 @@ void notificationTapBackground( 'Main isolate no up - Create temporary client for notification tap intend!', ); + final pushIsolateReceivePort = ReceivePort(); + IsolateNameServer.registerPortWithName( + pushIsolateReceivePort.sendPort, + AppConfig.pushIsolatePortName, + ); + if (!_vodInitialized) { await vod.init(); _vodInitialized = true; @@ -79,6 +95,8 @@ void notificationTapBackground( await notificationTap(notificationResponse, client: client); } finally { await client.dispose(closeDatabase: false); + pushIsolateReceivePort.sendPort.send('DONE'); + IsolateNameServer.removePortNameMapping(AppConfig.pushIsolatePortName); } return; }