[94] add comments

This commit is contained in:
kanat
2023-05-11 11:43:08 -07:00
parent 3e501731fd
commit 427cd9de83
3 changed files with 52 additions and 27 deletions
@@ -30,6 +30,14 @@
android:name="io.flutter.embedding.android.NormalTheme" android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme" android:resource="@style/NormalTheme"
/> />
<!-- Action mentioned in Notification Template:
{
"title": "{{ sender.name }} @ {{ channel.name }}",
"body": "{{ truncate message.text 2000 }}",
"click_action": "FLUTTER_NOTIFICATION_CLICK",
"sound": "default"
}
-->
<intent-filter> <intent-filter>
<action android:name="FLUTTER_NOTIFICATION_CLICK" /> <action android:name="FLUTTER_NOTIFICATION_CLICK" />
<category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.DEFAULT" />
@@ -45,7 +53,7 @@
android:name="flutterEmbedding" android:name="flutterEmbedding"
android:value="2" /> android:value="2" />
<!-- Set custom default icon. This is used when no icon is set for incoming notification messages. <!-- Set custom default icon. This is used when no icon is set for background notification messages.
See README(https://goo.gl/l4GJaQ) for more. --> See README(https://goo.gl/l4GJaQ) for more. -->
<meta-data <meta-data
android:name="com.google.firebase.messaging.default_notification_icon" android:name="com.google.firebase.messaging.default_notification_icon"
+36 -1
View File
@@ -24,15 +24,29 @@ import 'package:streaming_shared_preferences/streaming_shared_preferences.dart';
import 'firebase_options.dart'; import 'firebase_options.dart';
/// Will be invoked from another Isolate, that's why it's required to
/// initialize everything again:
/// - Firebase
/// - StreamChatClient
/// - StreamChatPersistenceClient
///
/// This callback is not called on iOS
@pragma('vm:entry-point') @pragma('vm:entry-point')
Future<void> _onFirebaseBackgroundMessage(RemoteMessage message) async { Future<void> _onFirebaseBackgroundMessage(RemoteMessage message) async {
// initialize Firebase
await Firebase.initializeApp( await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform, options: DefaultFirebaseOptions.currentPlatform,
); );
final data = message.data; final data = message.data;
// ensure that Push Notification was sent by Stream.
if (data['sender'] != 'stream.chat') {
return;
}
// ensure that Push Notification relates to a new message event.
if (data['type'] != 'message.new') { if (data['type'] != 'message.new') {
return; return;
} }
// read existing user info.
String? apiKey, userId, token; String? apiKey, userId, token;
if (!kIsWeb) { if (!kIsWeb) {
const secureStorage = FlutterSecureStorage(); const secureStorage = FlutterSecureStorage();
@@ -45,16 +59,21 @@ Future<void> _onFirebaseBackgroundMessage(RemoteMessage message) async {
} }
final client = buildStreamChatClient(apiKey ?? kDefaultStreamApiKey); final client = buildStreamChatClient(apiKey ?? kDefaultStreamApiKey);
final persistenceClient = StreamChatPersistenceClient(); final persistenceClient = StreamChatPersistenceClient();
// initialize persistence with current user
await persistenceClient.connect(userId); await persistenceClient.connect(userId);
// initialize client with current user
await client.connectUser( await client.connectUser(
User(id: userId), User(id: userId),
token, token,
// do not open WS connection
connectWebSocket: false, connectWebSocket: false,
); );
final messageId = data['id']; final messageId = data['id'];
final cid = data['cid']; final cid = data['cid'];
// pre-cache the new message using client and persistence.
final response = await client.getMessage(messageId); final response = await client.getMessage(messageId);
await persistenceClient.updateMessages(cid, [response.message]); await persistenceClient.updateMessages(cid, [response.message]);
} }
@@ -133,29 +152,42 @@ class _StreamChatSampleAppState extends State<StreamChatSampleApp>
.map((it) => it?.id) .map((it) => it?.id)
.distinct() .distinct()
.listen((userId) async { .listen((userId) async {
// User logged in
if (userId != null) { if (userId != null) {
// Requests notification permission.
await FirebaseMessaging.instance.requestPermission(); await FirebaseMessaging.instance.requestPermission();
// Sets callback for background messages (it's not called on iOS)
FirebaseMessaging.onBackgroundMessage(_onFirebaseBackgroundMessage); FirebaseMessaging.onBackgroundMessage(_onFirebaseBackgroundMessage);
// Sets callback for the notification click event.
firebaseSubscriptions.add(FirebaseMessaging.onMessageOpenedApp firebaseSubscriptions.add(FirebaseMessaging.onMessageOpenedApp
.listen(_onFirebaseMessageOpenedApp(client))); .listen(_onFirebaseMessageOpenedApp(client)));
// Sets callback for the token refresh event.
firebaseSubscriptions.add(FirebaseMessaging.instance.onTokenRefresh firebaseSubscriptions.add(FirebaseMessaging.instance.onTokenRefresh
.listen(_onFirebaseTokenRefresh(client))); .listen(_onFirebaseTokenRefresh(client)));
final token = await FirebaseMessaging.instance.getToken(); final token = await FirebaseMessaging.instance.getToken();
if (token != null) { if (token != null) {
// add Token to Stream
await client.addDevice(token, PushProvider.firebase); await client.addDevice(token, PushProvider.firebase);
} }
} else { }
// User logged out
else {
firebaseSubscriptions.cancelAll(); firebaseSubscriptions.cancelAll();
final token = await FirebaseMessaging.instance.getToken(); final token = await FirebaseMessaging.instance.getToken();
if (token != null) { if (token != null) {
// remove token from Stream
await client.removeDevice(token); await client.removeDevice(token);
} }
} }
}); });
} }
/// Constructs callback for notification click event.
OnRemoteMessage _onFirebaseMessageOpenedApp(StreamChatClient client) { OnRemoteMessage _onFirebaseMessageOpenedApp(StreamChatClient client) {
return (message) async { return (message) async {
// This callback is getting invoked when the user clicks
// on the notification in case if notification was shown by OS.
final channelType = (message.data['channel_type'] as String?) ?? ''; final channelType = (message.data['channel_type'] as String?) ?? '';
final channelId = (message.data['channel_id'] as String?) ?? ''; final channelId = (message.data['channel_id'] as String?) ?? '';
final channelCid = (message.data['cid'] as String?) ?? ''; final channelCid = (message.data['cid'] as String?) ?? '';
@@ -167,6 +199,7 @@ class _StreamChatSampleAppState extends State<StreamChatSampleApp>
); );
await channel.watch(); await channel.watch();
} }
// Navigates to Channel page, which is associated with the notification.
GoRouter.of(_navigatorKey.currentContext!).pushNamed( GoRouter.of(_navigatorKey.currentContext!).pushNamed(
Routes.CHANNEL_PAGE.name, Routes.CHANNEL_PAGE.name,
params: Routes.CHANNEL_PAGE.params(channel), params: Routes.CHANNEL_PAGE.params(channel),
@@ -174,10 +207,12 @@ class _StreamChatSampleAppState extends State<StreamChatSampleApp>
}; };
} }
/// Constructs callback for notification refresh event.
Future<void> Function(String) _onFirebaseTokenRefresh( Future<void> Function(String) _onFirebaseTokenRefresh(
StreamChatClient client, StreamChatClient client,
) { ) {
return (token) async { return (token) async {
// This callback is getting invoked when the token got refreshed.
await client.addDevice(token, PushProvider.firebase); await client.addDevice(token, PushProvider.firebase);
}; };
} }
+7 -25
View File
@@ -12,20 +12,11 @@ dependencies:
flutter: flutter:
sdk: flutter sdk: flutter
stream_chat_flutter: stream_chat_flutter:
git: path: ../../../stream-chat-flutter/packages/stream_chat_flutter
url: https://github.com/GetStream/stream-chat-flutter.git
ref: develop
path: packages/stream_chat_flutter
stream_chat_persistence: stream_chat_persistence:
git: path: ../../../stream-chat-flutter/packages/stream_chat_persistence
url: https://github.com/GetStream/stream-chat-flutter.git
ref: develop
path: packages/stream_chat_persistence
stream_chat_localizations: stream_chat_localizations:
git: path: ../../../stream-chat-flutter/packages/stream_chat_localizations
url: https://github.com/GetStream/stream-chat-flutter.git
ref: develop
path: packages/stream_chat_localizations
flutter_local_notifications: ^9.0.0 flutter_local_notifications: ^9.0.0
flutter_svg: ^2.0.4 flutter_svg: ^2.0.4
flutter_secure_storage: ^6.0.0 flutter_secure_storage: ^6.0.0
@@ -46,20 +37,11 @@ dev_dependencies:
dependency_overrides: dependency_overrides:
stream_chat: stream_chat:
git: path: ../../../stream-chat-flutter/packages/stream_chat
url: https://github.com/GetStream/stream-chat-flutter.git
ref: develop
path: packages/stream_chat
stream_chat_flutter_core: stream_chat_flutter_core:
git: path: ../../../stream-chat-flutter/packages/stream_chat_flutter_core
url: https://github.com/GetStream/stream-chat-flutter.git
ref: develop
path: packages/stream_chat_flutter_core
stream_chat_flutter: stream_chat_flutter:
git: path: ../../../stream-chat-flutter/packages/stream_chat_flutter
url: https://github.com/GetStream/stream-chat-flutter.git
ref: develop
path: packages/stream_chat_flutter
flutter: flutter:
uses-material-design: true uses-material-design: true
@@ -78,4 +60,4 @@ flutter_icons:
windows: windows:
generate: true generate: true
macos: macos:
generate: true generate: true