feat(stream_chat_v1): open channel on foreground notification press

This commit is contained in:
Salvatore Giordano
2021-09-01 10:55:18 +02:00
parent c8cc0d759b
commit 14d340c6b6
2 changed files with 45 additions and 5 deletions
+10 -3
View File
@@ -12,7 +12,10 @@ class MyObserver extends NavigatorObserver {
Route? currentRoute; Route? currentRoute;
late final StreamSubscription _subscription; late final StreamSubscription _subscription;
MyObserver(StreamChatClient client, BuildContext context) { MyObserver(
StreamChatClient client,
GlobalKey<NavigatorState> navigatorKey,
) {
_subscription = client _subscription = client
.on( .on(
EventType.messageNew, EventType.messageNew,
@@ -30,7 +33,11 @@ class MyObserver extends NavigatorObserver {
} }
} }
showLocalNotification(event, client.state.currentUser!.id, context); showLocalNotification(
event,
client.state.currentUser!.id,
navigatorKey.currentState!.context,
);
}); });
} }
@@ -103,7 +110,7 @@ class _HomePageState extends State<HomePage> {
@override @override
void didChangeDependencies() { void didChangeDependencies() {
_observer?.dispose(); _observer?.dispose();
_observer = MyObserver(widget.chatClient, context); _observer = MyObserver(widget.chatClient, _navigatorKey);
super.didChangeDependencies(); super.didChangeDependencies();
} }
} }
@@ -1,11 +1,16 @@
import 'package:example/channel_page.dart';
import 'package:example/localizations.dart'; import 'package:example/localizations.dart';
import 'package:example/routes/routes.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart' import 'package:flutter_local_notifications/flutter_local_notifications.dart'
hide Message; hide Message;
import 'package:stream_chat_flutter/stream_chat_flutter.dart'; import 'package:stream_chat_flutter/stream_chat_flutter.dart';
void showLocalNotification( void showLocalNotification(
Event event, String currentUserId, BuildContext context) async { Event event,
String currentUserId,
BuildContext context,
) async {
if (![ if (![
EventType.messageNew, EventType.messageNew,
EventType.notificationMessageNew, EventType.notificationMessageNew,
@@ -22,7 +27,34 @@ void showLocalNotification(
android: initializationSettingsAndroid, android: initializationSettingsAndroid,
iOS: initializationSettingsIOS, iOS: initializationSettingsIOS,
); );
await flutterLocalNotificationsPlugin.initialize(initializationSettings); await flutterLocalNotificationsPlugin.initialize(
initializationSettings,
onSelectNotification: (channelCid) async {
if (channelCid != null) {
final client = StreamChat.of(context).client;
var channel = client.state.channels[channelCid];
if (channel == null) {
final splits = channelCid.split(':');
final type = splits[0];
final id = splits[1];
channel = client.channel(
type,
id: id,
);
await channel.watch();
}
Navigator.pushNamed(
context,
Routes.CHANNEL_PAGE,
arguments: ChannelPageArgs(
channel: channel,
),
);
}
},
);
await flutterLocalNotificationsPlugin.show( await flutterLocalNotificationsPlugin.show(
event.message!.id.hashCode, event.message!.id.hashCode,
event.message!.user!.name, event.message!.user!.name,
@@ -37,5 +69,6 @@ void showLocalNotification(
), ),
iOS: IOSNotificationDetails(), iOS: IOSNotificationDetails(),
), ),
payload: '${event.channelType}:${event.channelId}',
); );
} }