Skip to content

Commit f88222d

Browse files
author
Franco Bugnano
committed
Automatically register push notifications via a Session parameter
1 parent 6d22034 commit f88222d

File tree

5 files changed

+31
-3
lines changed

5 files changed

+31
-3
lines changed

examples/push_notifications/lib/main.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ class MyApp extends StatelessWidget {
2525
// This widget is the root of your application.
2626
@override
2727
Widget build(BuildContext context) {
28-
//final session = Session(appId: 'Hku1c4Pt');
29-
final session = Session(appId: 'tuM9UrY8'); // For local testing
28+
final session = Session(appId: 'Hku1c4Pt', enablePushNotifications: true);
29+
//final session = Session(appId: 'tuM9UrY8', enablePushNotifications: true); // For local testing
3030

3131
final me = session.getUser(
3232
id: '123456',

lib/src/chatbox.dart

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

1819
typedef SendMessageHandler = void Function(SendMessageEvent event);
1920
typedef TranslationToggledHandler = void Function(TranslationToggledEvent event);
@@ -217,6 +218,17 @@ class ChatBoxState extends State<ChatBox> {
217218
execute('options["me"] = $variableName;');
218219

219220
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+
} else {
228+
if (fcmToken != null) {
229+
execute('session.unsetPushRegistration({provider: "fcm", pushRegistrationId: "$fcmToken"});');
230+
}
231+
}
220232
}
221233

222234
void _createChatBox() {

lib/src/conversationlist.dart

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

1718
typedef SelectConversationHandler = void Function(SelectConversationEvent event);
1819

@@ -176,6 +177,17 @@ class ConversationListState extends State<ConversationList> {
176177
execute('options["me"] = $variableName;');
177178

178179
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+
} else {
187+
if (fcmToken != null) {
188+
execute('session.unsetPushRegistration({provider: "fcm", pushRegistrationId: "$fcmToken"});');
189+
}
190+
}
179191
}
180192

181193
void _createConversationList() {

lib/src/notification.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,8 @@ void _onFCMTokenRefresh(String token) {
287287
print('📘 Firebase onTokenRefresh: $token');
288288

289289
fcmToken = token;
290+
291+
// TODO: Update the token on the Talkjs server once we have the data layer SDK ready
290292
}
291293

292294
Future<void> registerAndroidPushNotificationHandlers(AndroidChannel androidChannel) async {

lib/src/session.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ class Session with ChangeNotifier {
3535
/// code.
3636
final String? signature;
3737

38-
Session({required this.appId, this.signature});
38+
final bool enablePushNotifications;
39+
40+
Session({required this.appId, this.signature, this.enablePushNotifications = false});
3941

4042
User getUser({
4143
required String id,

0 commit comments

Comments
 (0)