Skip to content

Commit acad16d

Browse files
author
Franco Bugnano
committed
Added Talk.initPushNotificationHandlers and Talk.handleFCMBackgroundMessage
1 parent 31077eb commit acad16d

File tree

2 files changed

+113
-82
lines changed

2 files changed

+113
-82
lines changed

lib/src/notification.dart

Lines changed: 96 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -202,112 +202,114 @@ Future<ByteArrayAndroidIcon?> _androidIconFromUrl(String? url) async {
202202

203203
@pragma("vm:entry-point")
204204
Future<void> _onFCMBackgroundMessage(RemoteMessage firebaseMessage) async {
205+
await handleTalkJSFCMBackgroundMessage(firebaseMessage);
206+
}
207+
208+
Future<bool> handleTalkJSFCMBackgroundMessage(
209+
RemoteMessage firebaseMessage) async {
205210
print("📘 Handling a background message: ${firebaseMessage.messageId}");
206211

207212
print('📘 Message data: ${firebaseMessage.data}');
208213

209214
if (firebaseMessage.notification != null) {
210215
print(
211-
'📘 Message also contained a notification: ${firebaseMessage.notification}');
216+
'📘 Message also contained a notification: ${firebaseMessage.notification}',
217+
);
212218
}
213219

214220
final data = firebaseMessage.data;
215221
StyleInformation styleInformation;
216222
styleInformation = MessagingStyleInformation(Person(name: 'me'));
217223
int showId;
218-
if (data['talkjs'] is String) {
219-
print("📘 _onFCMBackgroundMessage: data['talkjs'] is String");
220-
final Map<String, dynamic> talkjsData = json.decode(data['talkjs']);
221-
final String notificationId = talkjsData['conversation']['id'];
222224

223-
if (!_showIdFromNotificationId.containsKey(notificationId)) {
224-
_showIdFromNotificationId[notificationId] = _nextId;
225-
_nextId += 1;
226-
}
225+
if (!(data['talkjs'] is String)) {
226+
print("📘 _onFCMBackgroundMessage: data['talkjs'] is NOT String");
227+
return false;
228+
}
227229

228-
showId = _showIdFromNotificationId[notificationId]!;
230+
print("📘 _onFCMBackgroundMessage: data['talkjs'] is String");
231+
final Map<String, dynamic> talkjsData = json.decode(data['talkjs']);
232+
final String notificationId = talkjsData['conversation']['id'];
229233

230-
final timestamp =
231-
DateTime.fromMillisecondsSinceEpoch(talkjsData['timestamp']);
234+
if (!_showIdFromNotificationId.containsKey(notificationId)) {
235+
_showIdFromNotificationId[notificationId] = _nextId;
236+
_nextId += 1;
237+
}
232238

233-
final activeNotifications = _activeNotifications[notificationId];
239+
showId = _showIdFromNotificationId[notificationId]!;
234240

235-
if (activeNotifications == null) {
236-
print("📘 _onFCMBackgroundMessage: activeNotifications == null");
237-
_activeNotifications[notificationId] = [data['talkjs']];
241+
final timestamp =
242+
DateTime.fromMillisecondsSinceEpoch(talkjsData['timestamp']);
238243

239-
final attachment = talkjsData['message']['attachment'];
244+
final activeNotifications = _activeNotifications[notificationId];
240245

241-
if (attachment != null) {
242-
print("📘 _onFCMBackgroundMessage: attachment != null");
243-
final picture = await _androidBitmapFromUrl(attachment['url']);
244-
if (picture != null) {
245-
print("📘 _onFCMBackgroundMessage: picture != null");
246-
styleInformation = BigPictureStyleInformation(picture);
247-
} else {
248-
print("📘 _onFCMBackgroundMessage: picture == null");
249-
}
246+
if (activeNotifications == null) {
247+
print("📘 _onFCMBackgroundMessage: activeNotifications == null");
248+
_activeNotifications[notificationId] = [data['talkjs']];
249+
250+
final attachment = talkjsData['message']['attachment'];
251+
252+
if (attachment != null) {
253+
print("📘 _onFCMBackgroundMessage: attachment != null");
254+
final picture = await _androidBitmapFromUrl(attachment['url']);
255+
if (picture != null) {
256+
print("📘 _onFCMBackgroundMessage: picture != null");
257+
styleInformation = BigPictureStyleInformation(picture);
250258
} else {
251-
print("📘 _onFCMBackgroundMessage: attachment == null");
252-
final sender = talkjsData['sender'];
253-
styleInformation = MessagingStyleInformation(
254-
Person(
255-
name: 'me',
256-
),
257-
groupConversation:
258-
talkjsData['conversation']['participants'].length > 2,
259-
messages: [
260-
Message(
261-
talkjsData['message']['text'],
262-
timestamp,
263-
Person(
264-
icon: await _androidIconFromUrl(sender['photoUrl']),
265-
key: sender['id'],
266-
name: sender['name'],
267-
),
268-
),
269-
],
270-
);
259+
print("📘 _onFCMBackgroundMessage: picture == null");
271260
}
272261
} else {
273-
print("📘 _onFCMBackgroundMessage: activeNotifications != null");
274-
activeNotifications.add(data['talkjs']);
275-
final messages = <Message>[];
276-
for (final talkjsString in activeNotifications) {
277-
final Map<String, dynamic> messageTalkjsData =
278-
json.decode(talkjsString);
279-
final messageTimestamp =
280-
DateTime.fromMillisecondsSinceEpoch(messageTalkjsData['timestamp']);
281-
final messageSender = talkjsData['sender'];
282-
283-
messages.add(
284-
Message(
285-
messageTalkjsData['message']['text'],
286-
messageTimestamp,
287-
Person(
288-
icon: await _androidIconFromUrl(messageSender['photoUrl']),
289-
key: messageSender['id'],
290-
name: messageSender['name'],
291-
),
292-
),
293-
);
294-
}
295-
262+
print("📘 _onFCMBackgroundMessage: attachment == null");
263+
final sender = talkjsData['sender'];
296264
styleInformation = MessagingStyleInformation(
297265
Person(
298266
name: 'me',
299267
),
300268
groupConversation:
301269
talkjsData['conversation']['participants'].length > 2,
302-
messages: messages,
270+
messages: [
271+
Message(
272+
talkjsData['message']['text'],
273+
timestamp,
274+
Person(
275+
icon: await _androidIconFromUrl(sender['photoUrl']),
276+
key: sender['id'],
277+
name: sender['name'],
278+
),
279+
),
280+
],
303281
);
304282
}
305283
} else {
306-
print("📘 _onFCMBackgroundMessage: data['talkjs'] is NOT String");
307-
showId = _nextId;
308-
_nextId += 1;
284+
print("📘 _onFCMBackgroundMessage: activeNotifications != null");
285+
activeNotifications.add(data['talkjs']);
286+
final messages = <Message>[];
287+
for (final talkjsString in activeNotifications) {
288+
final Map<String, dynamic> messageTalkjsData = json.decode(talkjsString);
289+
final messageTimestamp =
290+
DateTime.fromMillisecondsSinceEpoch(messageTalkjsData['timestamp']);
291+
final messageSender = talkjsData['sender'];
292+
293+
messages.add(
294+
Message(
295+
messageTalkjsData['message']['text'],
296+
messageTimestamp,
297+
Person(
298+
icon: await _androidIconFromUrl(messageSender['photoUrl']),
299+
key: messageSender['id'],
300+
name: messageSender['name'],
301+
),
302+
),
303+
);
304+
}
309305

310-
styleInformation = DefaultStyleInformation(false, false);
306+
styleInformation = MessagingStyleInformation(
307+
Person(
308+
name: 'me',
309+
),
310+
groupConversation: talkjsData['conversation']['participants'].length > 2,
311+
messages: messages,
312+
);
311313
}
312314

313315
// Fetch the push notification settings from shared preferences.
@@ -350,6 +352,8 @@ Future<void> _onFCMBackgroundMessage(RemoteMessage firebaseMessage) async {
350352
platformChannelSpecifics, // notificationDetails
351353
payload: data['talkjs'],
352354
);
355+
356+
return true;
353357
}
354358

355359
// The commented code is for when we will upgrade to flutter_local_notifications version 10
@@ -373,8 +377,9 @@ void _onFCMTokenRefresh(String token) {
373377
// TODO: Update the token on the Talkjs server once we have the data layer SDK ready
374378
}
375379

376-
Future<void> registerAndroidPushNotificationHandlers(
377-
AndroidSettings androidSettings) async {
380+
Future<void> initAndroidPushNotificationHandlers(
381+
AndroidSettings androidSettings,
382+
) async {
378383
// Get the token each time the application loads
379384
fcmToken = await FirebaseMessaging.instance.getToken();
380385
print('📘 Firebase token: $fcmToken');
@@ -433,6 +438,12 @@ Future<void> registerAndroidPushNotificationHandlers(
433438
// PlatformException is raised on Android < 6.0
434439
// Simply ignoring this part
435440
}
441+
}
442+
443+
Future<void> registerAndroidPushNotificationHandlers(
444+
AndroidSettings androidSettings,
445+
) async {
446+
await initAndroidPushNotificationHandlers(androidSettings);
436447

437448
FirebaseMessaging.onBackgroundMessage(_onFCMBackgroundMessage);
438449
}
@@ -448,8 +459,9 @@ Future<void> _onPush(String name, ApnsRemoteMessage message) async {
448459
}
449460
}
450461

451-
Future<void> registerIOSPushNotificationHandlers(
452-
IOSSettings iosSettings) async {
462+
Future<void> initIOSPushNotificationHandlers(
463+
IOSSettings iosSettings,
464+
) async {
453465
final connector = ApnsPushConnectorOnly();
454466

455467
connector.configureApns(
@@ -471,3 +483,9 @@ Future<void> registerIOSPushNotificationHandlers(
471483
});
472484
}
473485
}
486+
487+
Future<void> registerIOSPushNotificationHandlers(
488+
IOSSettings iosSettings,
489+
) async {
490+
await initIOSPushNotificationHandlers(iosSettings);
491+
}

lib/talkjs_flutter.dart

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,27 @@ class Talk {
3434
return hash.substring(0, 20);
3535
}
3636

37-
static Future<void> registerPushNotificationHandlers(
38-
{AndroidSettings? androidSettings, IOSSettings? iosSettings}) async {
37+
static Future<void> registerPushNotificationHandlers({
38+
AndroidSettings? androidSettings,
39+
IOSSettings? iosSettings,
40+
}) async {
3941
if ((Platform.isAndroid) && (androidSettings != null)) {
4042
await registerAndroidPushNotificationHandlers(androidSettings);
43+
} else if ((Platform.isIOS) && (iosSettings != null)) {
44+
await registerIOSPushNotificationHandlers(iosSettings);
4145
}
46+
}
4247

43-
if ((Platform.isIOS) && (iosSettings != null)) {
44-
await registerIOSPushNotificationHandlers(iosSettings);
48+
static Future<void> initPushNotificationHandlers({
49+
AndroidSettings? androidSettings,
50+
IOSSettings? iosSettings,
51+
}) async {
52+
if ((Platform.isAndroid) && (androidSettings != null)) {
53+
await initAndroidPushNotificationHandlers(androidSettings);
54+
} else if ((Platform.isIOS) && (iosSettings != null)) {
55+
await initIOSPushNotificationHandlers(iosSettings);
4556
}
4657
}
58+
59+
static const handleFCMBackgroundMessage = handleTalkJSFCMBackgroundMessage;
4760
}

0 commit comments

Comments
 (0)