From bf1ca1ba497ab936c8fb1ef02726805b4c788fbb Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Mon, 30 Aug 2021 11:20:41 +0200 Subject: [PATCH 1/5] docs(doc): add foreground push notification --- ...ions.mdx => adding_push_notifications.mdx} | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) rename docusaurus/docs/Flutter/guides/{adding_push_notifcations.mdx => adding_push_notifications.mdx} (87%) diff --git a/docusaurus/docs/Flutter/guides/adding_push_notifcations.mdx b/docusaurus/docs/Flutter/guides/adding_push_notifications.mdx similarity index 87% rename from docusaurus/docs/Flutter/guides/adding_push_notifcations.mdx rename to docusaurus/docs/Flutter/guides/adding_push_notifications.mdx index 36da1373..6e5cbf1d 100644 --- a/docusaurus/docs/Flutter/guides/adding_push_notifcations.mdx +++ b/docusaurus/docs/Flutter/guides/adding_push_notifications.mdx @@ -187,8 +187,33 @@ StreamChat( As you can see we generate a local notification whenever a message.new or notification.message_new event is received. +### Foreground notifications + +Sometimes you want to show a notification when the app is in the foreground. +For example, when you're typing a message in a channel and you receive a new message from someone in another channel, you want to get notified about it. + +Even in this case you can use the `flutter_local_notifications` package to show a notification. + +You need to listen for new events using `StreamChatClient.on` and handle them accordingly. + +Here we're checking if the event is a `message.new` or `notification.message_new` event, and if the message is from a different user than the current user. In that case we'll show a notification. + +```dart +client.on( + EventType.messageNew, + EventType.notificationMessageNew, +).listen((event) { + if (event.message?.user?.id == client.state.currentUser?.id) { + return; + } + showLocalNotification(event, client.state.currentUser!.id, context); +}); +``` + :::note -Using `flutter_local_notifications` is a great way to implement notifications while the is in foreground too! You can generate a local notification listening to events using the method `streamChatClient.on()` and react to the events you want. +You should also check that the channel of the message is different than the channel in foreground. +How you can do this depends on your app infrastructure and how you handle navigation. +Take a look at the [Stream Chat v1 sample app](https://github.com/GetStream/flutter-samples/blob/main/packages/stream_chat_v1/lib/home_page.dart#L11) to see how we're doing it over there. ::: ### Saving notification messages to the offline storage From d7c96143a598960258d3d683a5ff5ce67e68603a Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Mon, 30 Aug 2021 13:25:34 +0200 Subject: [PATCH 2/5] fix(llc): channel.show body --- packages/stream_chat/lib/src/core/api/channel_api.dart | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/stream_chat/lib/src/core/api/channel_api.dart b/packages/stream_chat/lib/src/core/api/channel_api.dart index 68f0d1c3..619e0980 100644 --- a/packages/stream_chat/lib/src/core/api/channel_api.dart +++ b/packages/stream_chat/lib/src/core/api/channel_api.dart @@ -292,6 +292,7 @@ class ChannelApi { ) async { final response = await _client.post( '${_getChannelUrl(channelId, channelType)}/show', + data: {}, ); return EmptyResponse.fromJson(response.data); } From a23b29ddc3348e55b5cd47abdcf0fc7ec88cc4f7 Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Mon, 30 Aug 2021 13:26:35 +0200 Subject: [PATCH 3/5] chore(llc): update changelog --- packages/stream_chat/CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/packages/stream_chat/CHANGELOG.md b/packages/stream_chat/CHANGELOG.md index f136ac5d..6ddea6ed 100644 --- a/packages/stream_chat/CHANGELOG.md +++ b/packages/stream_chat/CHANGELOG.md @@ -1,3 +1,9 @@ +## Upcoming + +🐞 Fixed + +- Fix `channel.show` not working because of null body + ## 2.2.0 🐞 Fixed From 1ebafb036fdbf2a744c62384f718759baecdc748 Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Mon, 30 Aug 2021 15:21:59 +0200 Subject: [PATCH 4/5] fix(llc): update tests --- .../test/src/core/api/channel_api_test.dart | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/stream_chat/test/src/core/api/channel_api_test.dart b/packages/stream_chat/test/src/core/api/channel_api_test.dart index 9110845d..e427ff2e 100644 --- a/packages/stream_chat/test/src/core/api/channel_api_test.dart +++ b/packages/stream_chat/test/src/core/api/channel_api_test.dart @@ -550,14 +550,21 @@ void main() { final path = '${_getChannelUrl(channelId, channelType)}/show'; - when(() => client.post(path)).thenAnswer( - (_) async => successResponse(path, data: {})); + when(() => client.post( + path, + data: {}, + )) + .thenAnswer( + (_) async => successResponse(path, data: {})); final res = await channelApi.showChannel(channelId, channelType); expect(res, isNotNull); - verify(() => client.post(path)).called(1); + verify(() => client.post( + path, + data: {}, + )).called(1); verifyNoMoreInteractions(client); }); From de8f0d9f938982231016e0f31fe7f092c0385334 Mon Sep 17 00:00:00 2001 From: Salvatore Giordano Date: Tue, 31 Aug 2021 09:11:27 +0200 Subject: [PATCH 5/5] Apply suggestions from code review Co-authored-by: Gordon --- .../docs/Flutter/guides/adding_push_notifications.mdx | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docusaurus/docs/Flutter/guides/adding_push_notifications.mdx b/docusaurus/docs/Flutter/guides/adding_push_notifications.mdx index 6e5cbf1d..c90f6814 100644 --- a/docusaurus/docs/Flutter/guides/adding_push_notifications.mdx +++ b/docusaurus/docs/Flutter/guides/adding_push_notifications.mdx @@ -189,10 +189,10 @@ As you can see we generate a local notification whenever a message.new or notifi ### Foreground notifications -Sometimes you want to show a notification when the app is in the foreground. -For example, when you're typing a message in a channel and you receive a new message from someone in another channel, you want to get notified about it. +Sometimes you may want to show a notification when the app is in the foreground. +For example, when you're in a channel and you receive a new message from someone in another channel. -Even in this case you can use the `flutter_local_notifications` package to show a notification. +For this scenario, you can also use the `flutter_local_notifications` package to show a notification. You need to listen for new events using `StreamChatClient.on` and handle them accordingly. @@ -211,8 +211,8 @@ client.on( ``` :::note -You should also check that the channel of the message is different than the channel in foreground. -How you can do this depends on your app infrastructure and how you handle navigation. +You should also check that the channel of the message is different than the channel in the foreground. +How you do this depends on your app infrastructure and how you handle navigation. Take a look at the [Stream Chat v1 sample app](https://github.com/GetStream/flutter-samples/blob/main/packages/stream_chat_v1/lib/home_page.dart#L11) to see how we're doing it over there. :::