diff --git a/lib/app_flavored.dart b/lib/app_flavored.dart index 9563840..907803c 100644 --- a/lib/app_flavored.dart +++ b/lib/app_flavored.dart @@ -1,7 +1,6 @@ import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:humhub/flavored/models/humhub.f.dart'; -import 'package:humhub/flavored/util/notifications/channel.dart'; import 'package:humhub/flavored/util/router.f.dart'; import 'package:humhub/util/intent/intent_plugin.dart'; import 'package:humhub/util/loading_provider.dart'; @@ -25,7 +24,6 @@ class FlavoredAppState extends ConsumerState { return IntentPlugin( child: NotificationPlugin( child: PushPlugin( - channel: NotificationChannelF(), child: OverrideLocale( builder: (overrideLocale) => Builder( builder: (context) => MaterialApp( diff --git a/lib/app_opener.dart b/lib/app_opener.dart index 6a5537f..71e7462 100644 --- a/lib/app_opener.dart +++ b/lib/app_opener.dart @@ -2,7 +2,6 @@ import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:humhub/util/intent/intent_plugin.dart'; import 'package:humhub/util/loading_provider.dart'; -import 'package:humhub/util/notifications/channel.dart'; import 'package:humhub/util/notifications/plugin.dart'; import 'package:humhub/util/override_locale.dart'; import 'package:humhub/util/push/push_plugin.dart'; @@ -24,7 +23,6 @@ class OpenerAppState extends ConsumerState { return IntentPlugin( child: NotificationPlugin( child: PushPlugin( - channel: NotificationChannel(), child: OverrideLocale( builder: (overrideLocale) => Builder( builder: (context) => FutureBuilder( diff --git a/lib/flavored/util/notifications/channel.dart b/lib/flavored/util/notifications/channel.dart index 9c98678..196d769 100644 --- a/lib/flavored/util/notifications/channel.dart +++ b/lib/flavored/util/notifications/channel.dart @@ -1,11 +1,10 @@ -import 'package:humhub/pages/web_view.dart'; +import 'package:humhub/flavored/util/router.f.dart'; +import 'package:humhub/flavored/web_view.f.dart'; import 'package:humhub/util/notifications/channel.dart'; import 'package:humhub/util/notifications/init_from_push.dart'; -import 'package:humhub/util/openers/universal_opener_controller.dart'; -import 'package:humhub/util/router.dart'; class NotificationChannelF extends NotificationChannel { - NotificationChannelF( + const NotificationChannelF( {super.id = 'redirect', super.name = 'Redirect flavored app notifications', super.description = 'These notifications redirect the user to specific url in a payload.'}); @@ -15,21 +14,19 @@ class NotificationChannelF extends NotificationChannel { /// @override Future onTap(String? payload) async { - if (payload != null && navigatorKey.currentState != null) { + if (payload != null && navigatorKeyF.currentState != null) { bool isNewRouteSameAsCurrent = false; - navigatorKey.currentState!.popUntil((route) { - if (route.settings.name == WebViewApp.path) { + navigatorKeyF.currentState!.popUntil((route) { + if (route.settings.name == WebViewF.path) { isNewRouteSameAsCurrent = true; } return true; }); - UniversalOpenerController opener = UniversalOpenerController(url: payload); - await opener.initHumHub(); if (isNewRouteSameAsCurrent) { - navigatorKey.currentState!.pushNamed(WebViewApp.path, arguments: opener); + navigatorKeyF.currentState!.pushNamed(WebViewF.path, arguments: payload); return; } - navigatorKey.currentState!.pushNamed(WebViewApp.path, arguments: opener); + navigatorKeyF.currentState!.pushNamed(WebViewF.path, arguments: payload); } else { if (payload != null) { InitFromPush.setPayload(payload); diff --git a/lib/flavored/web_view.f.dart b/lib/flavored/web_view.f.dart index c5dc827..1c8dbd4 100644 --- a/lib/flavored/web_view.f.dart +++ b/lib/flavored/web_view.f.dart @@ -75,8 +75,12 @@ class FlavoredWebViewState extends ConsumerState { } URLRequest get _initialRequest { + var payload = ModalRoute.of(context)!.settings.arguments; String? url = instance.manifest.startUrl; - String? payloadFromPush = InitFromPush.usePayload(); + String? payloadForInitFromPush = InitFromPush.usePayload(); + String? payloadFromPush; + if (payload is String) payloadFromPush = payload; + if (payloadForInitFromPush != null) url = payloadForInitFromPush; if (payloadFromPush != null) url = payloadFromPush; return URLRequest(url: Uri.parse(url), headers: instance.customHeaders); } diff --git a/lib/main.dart b/lib/main.dart index 6105b51..7174a55 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -17,6 +17,6 @@ main() async { PackageInfo packageInfo = await PackageInfo.fromPlatform(); await dotenv.load(fileName: ".env"); SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]).then((_) async { - runApp(ProviderScope(child: await HumHub.app(packageInfo.packageName))); + runApp(ProviderScope(child: HumHub.app(packageInfo.packageName))); }); } diff --git a/lib/models/hum_hub.dart b/lib/models/hum_hub.dart index 1d71358..910c9f5 100644 --- a/lib/models/hum_hub.dart +++ b/lib/models/hum_hub.dart @@ -67,7 +67,7 @@ class HumHub { 'x-humhub-app-ostate': isHideOpener ? '1' : '0' }; - static Future app(String bundleId) async { + static Widget app(String bundleId) { switch (bundleId) { case 'com.humhub.app': return const OpenerApp(); diff --git a/lib/pages/opener.dart b/lib/pages/opener.dart index ecf53e7..b0d50be 100644 --- a/lib/pages/opener.dart +++ b/lib/pages/opener.dart @@ -53,7 +53,7 @@ class OpenerState extends ConsumerState with SingleTickerProviderStateMi String? urlIntent = InitFromIntent.usePayloadForInit(); if (urlIntent != null) { - await NotificationChannel().onTap(urlIntent); + await ref.read(notificationChannelProvider).value!.onTap(urlIntent); } }); } diff --git a/lib/util/notifications/channel.dart b/lib/util/notifications/channel.dart index ec31761..923d8bc 100644 --- a/lib/util/notifications/channel.dart +++ b/lib/util/notifications/channel.dart @@ -1,14 +1,17 @@ +import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:humhub/flavored/util/notifications/channel.dart'; import 'package:humhub/pages/web_view.dart'; import 'package:humhub/util/notifications/init_from_push.dart'; import 'package:humhub/util/openers/universal_opener_controller.dart'; import 'package:humhub/util/router.dart'; +import 'package:package_info_plus/package_info_plus.dart'; class NotificationChannel { final String id; final String name; final String description; - NotificationChannel( + const NotificationChannel( {this.id = 'redirect', this.name = 'Redirect app notifications', this.description = 'These notifications are redirect the user to specific url in a payload.'}); @@ -38,4 +41,19 @@ class NotificationChannel { } } } + + static Future getChannel() async { + PackageInfo packageInfo = await PackageInfo.fromPlatform(); // Replace this with the actual condition logic + switch (packageInfo.packageName) { + case 'com.humhub.app': + return const NotificationChannel(); + default: + return const NotificationChannelF(); + } + } } + +// Providers for NotificationChannel and NotificationChannelF +final notificationChannelProvider = FutureProvider((ref) { + return NotificationChannel.getChannel(); +}); diff --git a/lib/util/notifications/service.dart b/lib/util/notifications/service.dart index 54818ae..2ced421 100644 --- a/lib/util/notifications/service.dart +++ b/lib/util/notifications/service.dart @@ -48,7 +48,8 @@ class NotificationService { static void handleNotification(NotificationResponse response) async { final parsed = response.payload != null ? json.decode(response.payload!) : {}; if (parsed["redirectUrl"] != null) { - await NotificationChannel().onTap(parsed['redirectUrl']); + var channel = await NotificationChannel.getChannel(); + channel.onTap(parsed['redirectUrl']); return; } } diff --git a/lib/util/push/push_plugin.dart b/lib/util/push/push_plugin.dart index 1044f02..8d773fe 100644 --- a/lib/util/push/push_plugin.dart +++ b/lib/util/push/push_plugin.dart @@ -13,13 +13,11 @@ import 'package:loggy/loggy.dart'; class PushPlugin extends ConsumerStatefulWidget { final Widget child; - final NotificationChannel channel; - const PushPlugin({ + const PushPlugin({ Key? key, required this.child, - required this.channel, - }) : super(key: key); + }) : super(key: key, ); @override PushPluginState createState() => PushPluginState(); @@ -44,7 +42,7 @@ class PushPluginState extends ConsumerState { FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) { logInfo("Firebase messaging onMessageOpenedApp"); final data = PushEvent(message).parsedData; - widget.channel.onTap(data.redirectUrl); + ref.read(notificationChannelProvider).value!.onTap(data.redirectUrl); }); //When the app is terminated, i.e., app is neither in foreground or background. @@ -62,6 +60,7 @@ class PushPluginState extends ConsumerState { @override void initState() { + ref.read(notificationChannelProvider); _init(); super.initState(); } @@ -69,7 +68,7 @@ class PushPluginState extends ConsumerState { _handleInitialMsg(RemoteMessage message) { final data = PushEvent(message).parsedData; if (data.redirectUrl != null) { - widget.channel.onTap(data.redirectUrl); + ref.read(notificationChannelProvider).value!.onTap(data.redirectUrl); } } @@ -81,7 +80,7 @@ class PushPluginState extends ConsumerState { final body = message.notification?.body; if (title == null || body == null) return; await notificationService.showNotification( - widget.channel, + ref.read(notificationChannelProvider).value!, title, body, payload: data.channelPayload,