Skip to content

Commit ded34ce

Browse files
author
Franco Bugnano
committed
Fixes requested in the PR
1 parent 47b30aa commit ded34ce

File tree

8 files changed

+75
-189
lines changed

8 files changed

+75
-189
lines changed

examples/push_notifications/android/app/google-services.json

Lines changed: 0 additions & 39 deletions
This file was deleted.

examples/push_notifications/lib/firebase_options.dart

Lines changed: 0 additions & 52 deletions
This file was deleted.

examples/push_notifications/lib/main.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ class MyApp extends StatelessWidget {
3030
// This widget is the root of your application.
3131
@override
3232
Widget build(BuildContext context) {
33-
final session = Session(appId: 'Hku1c4Pt', enablePushNotifications: true);
34-
//final session = Session(appId: 'tuM9UrY8', enablePushNotifications: true); // For local testing
33+
final session = Session(appId: 'YOUR_APP_ID', enablePushNotifications: true);
3534

3635
final me = session.getUser(
3736
id: '123456',

lib/src/chatbox.dart

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import './chatoptions.dart';
1414
import './user.dart';
1515
import './message.dart';
1616
import './predicate.dart';
17-
import './notification.dart';
17+
import './webview_common.dart';
1818

1919
typedef SendMessageHandler = void Function(SendMessageEvent event);
2020
typedef TranslationToggledHandler = void Function(TranslationToggledEvent event);
@@ -153,7 +153,7 @@ class ChatBoxState extends State<ChatBox> {
153153
}
154154
''');
155155

156-
_createSession();
156+
createSession(execute: execute, session: widget.session, variableName: getUserVariableName(widget.session.me));
157157
_createChatBox();
158158
// messageFilter and highlightedWords are set as options for the chatbox
159159
_createConversation();
@@ -202,43 +202,6 @@ class ChatBoxState extends State<ChatBox> {
202202
);
203203
}
204204

205-
void _createSession() {
206-
// Initialize Session object
207-
final options = <String, dynamic>{};
208-
209-
options['appId'] = widget.session.appId;
210-
211-
if (widget.session.signature != null) {
212-
options["signature"] = widget.session.signature;
213-
}
214-
215-
execute('const options = ${json.encode(options)};');
216-
217-
final variableName = getUserVariableName(widget.session.me);
218-
execute('options["me"] = $variableName;');
219-
220-
execute('const session = new Talk.Session(options);');
221-
222-
// TODO: This part has to be moved in the Session once we have the data layer SDK ready
223-
if (widget.session.enablePushNotifications) {
224-
if (fcmToken != null) {
225-
execute('session.setPushRegistration({provider: "fcm", pushRegistrationId: "$fcmToken"});');
226-
}
227-
228-
if (apnsToken != null) {
229-
execute('session.setPushRegistration({provider: "apns", pushRegistrationId: "$apnsToken"});');
230-
}
231-
} else {
232-
if (fcmToken != null) {
233-
execute('session.unsetPushRegistration({provider: "fcm", pushRegistrationId: "$fcmToken"});');
234-
}
235-
236-
if (apnsToken != null) {
237-
execute('session.unsetPushRegistration({provider: "apns", pushRegistrationId: "$apnsToken"});');
238-
}
239-
}
240-
}
241-
242205
void _createChatBox() {
243206
_oldOptions = ChatBoxOptions(
244207
dir: widget.dir,

lib/src/conversationlist.dart

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import './conversation.dart';
1313
import './user.dart';
1414
import './predicate.dart';
1515
import './chatbox.dart';
16-
import './notification.dart';
16+
import './webview_common.dart';
1717

1818
typedef SelectConversationHandler = void Function(SelectConversationEvent event);
1919

@@ -131,7 +131,7 @@ class ConversationListState extends State<ConversationList> {
131131
// is being constructed, and the callback may very possibly change the state
132132
Timer.run(() => widget.onLoadingStateChanged?.call(LoadingState.loading));
133133

134-
_createSession();
134+
createSession(execute: execute, session: widget.session, variableName: getUserVariableName(widget.session.me));
135135
_createConversationList();
136136
// feedFilter is set as an option for the inbox
137137

@@ -161,43 +161,6 @@ class ConversationListState extends State<ConversationList> {
161161
);
162162
}
163163

164-
void _createSession() {
165-
// Initialize Session object
166-
final options = <String, dynamic>{};
167-
168-
options['appId'] = widget.session.appId;
169-
170-
if (widget.session.signature != null) {
171-
options["signature"] = widget.session.signature;
172-
}
173-
174-
execute('const options = ${json.encode(options)};');
175-
176-
final variableName = getUserVariableName(widget.session.me);
177-
execute('options["me"] = $variableName;');
178-
179-
execute('const session = new Talk.Session(options);');
180-
181-
// TODO: This part has to be moved in the Session once we have the data layer SDK ready
182-
if (widget.session.enablePushNotifications) {
183-
if (fcmToken != null) {
184-
execute('session.setPushRegistration({provider: "fcm", pushRegistrationId: "$fcmToken"});');
185-
}
186-
187-
if (apnsToken != null) {
188-
execute('session.setPushRegistration({provider: "apns", pushRegistrationId: "$apnsToken"});');
189-
}
190-
} else {
191-
if (fcmToken != null) {
192-
execute('session.unsetPushRegistration({provider: "fcm", pushRegistrationId: "$fcmToken"});');
193-
}
194-
195-
if (apnsToken != null) {
196-
execute('session.unsetPushRegistration({provider: "apns", pushRegistrationId: "$apnsToken"});');
197-
}
198-
}
199-
}
200-
201164
void _createConversationList() {
202165
final options = ConversationListOptions(
203166
showFeedHeader: widget.showFeedHeader,

lib/src/notification.dart

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,29 +106,40 @@ final _activeNotifications = <String, List<String>>{};
106106
int _nextId = 0;
107107
final _showIdFromNotificationId = <String, int>{};
108108
final _receivePort = ReceivePort();
109+
final _imageCache = <String, Uint8List>{};
110+
111+
Future<Uint8List> _imageDataFromUrl(String url) async {
112+
// Use the cached image
113+
if (_imageCache.containsKey(url)) {
114+
print("📘 _imageDataFromUrl ($url): Using cached image");
115+
return _imageCache[url]!;
116+
}
117+
118+
final response = await http.get(Uri.parse(url));
119+
print("📘 _imageDataFromUrl ($url): $response");
120+
121+
// Cache the response only if the HTTP request was successful
122+
if (response.statusCode < 400) {
123+
_imageCache[url] = response.bodyBytes;
124+
}
125+
126+
return response.bodyBytes;
127+
}
109128

110129
Future<ByteArrayAndroidBitmap?> _androidBitmapFromUrl(String? url) async {
111130
if (url == null) {
112131
return null;
113132
}
114133

115-
// TODO: maybe we can keep a cache in case _androidBitmapFromUrl gets called
116-
// multiple times with the same URL
117-
final response = await http.get(Uri.parse(url));
118-
print("📘 _androidBitmapFromUrl ($url): $response");
119-
return ByteArrayAndroidBitmap(response.bodyBytes);
134+
return ByteArrayAndroidBitmap(await _imageDataFromUrl(url));
120135
}
121136

122137
Future<ByteArrayAndroidIcon?> _androidIconFromUrl(String? url) async {
123138
if (url == null) {
124139
return null;
125140
}
126141

127-
// TODO: maybe we can keep a cache in case _androidIconFromUrl gets called
128-
// multiple times with the same URL
129-
final response = await http.get(Uri.parse(url));
130-
print("📘 _androidIconFromUrl ($url): $response");
131-
return ByteArrayAndroidIcon(response.bodyBytes);
142+
return ByteArrayAndroidIcon(await _imageDataFromUrl(url));
132143
}
133144

134145
Future<void> _onFCMBackgroundMessage(RemoteMessage firebaseMessage) async {

lib/src/webview_common.dart

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import 'dart:convert';
2+
3+
import './session.dart';
4+
import './notification.dart';
5+
6+
typedef FnExecute = void Function(String statement);
7+
8+
void createSession({
9+
required FnExecute execute,
10+
required Session session,
11+
required String variableName,
12+
}) {
13+
// Initialize Session object
14+
final options = <String, dynamic>{};
15+
16+
options['appId'] = session.appId;
17+
18+
if (session.signature != null) {
19+
options["signature"] = session.signature;
20+
}
21+
22+
execute('const options = ${json.encode(options)};');
23+
24+
execute('options["me"] = $variableName;');
25+
26+
execute('const session = new Talk.Session(options);');
27+
28+
if (session.enablePushNotifications) {
29+
if (fcmToken != null) {
30+
execute('session.setPushRegistration({provider: "fcm", pushRegistrationId: "$fcmToken"});');
31+
}
32+
33+
if (apnsToken != null) {
34+
execute('session.setPushRegistration({provider: "apns", pushRegistrationId: "$apnsToken"});');
35+
}
36+
} else {
37+
if (fcmToken != null) {
38+
execute('session.unsetPushRegistration({provider: "fcm", pushRegistrationId: "$fcmToken"});');
39+
}
40+
41+
if (apnsToken != null) {
42+
execute('session.unsetPushRegistration({provider: "apns", pushRegistrationId: "$apnsToken"});');
43+
}
44+
}
45+
}

lib/talkjs_flutter.dart

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,11 @@ class Talk {
3535
}
3636

3737
Future<void> registerPushNotificationHandlers({AndroidChannel? androidChannel, IOSPermissions? iosPermissions}) async {
38-
if (Platform.isAndroid) {
39-
if (androidChannel != null) {
40-
await registerAndroidPushNotificationHandlers(androidChannel);
41-
}
38+
if ((Platform.isAndroid) && (androidChannel != null)) {
39+
await registerAndroidPushNotificationHandlers(androidChannel);
4240
}
4341

44-
if (Platform.isIOS) {
45-
if (iosPermissions != null) {
46-
await registerIOSPushNotificationHandlers(iosPermissions);
47-
}
42+
if ((Platform.isIOS) && (iosPermissions != null)) {
43+
await registerIOSPushNotificationHandlers(iosPermissions);
4844
}
4945
}

0 commit comments

Comments
 (0)