Skip to content

Commit

Permalink
wip start sketch getting token, sending to server; TODO listen for ch…
Browse files Browse the repository at this point in the history
…anges, retry/save, iOS
  • Loading branch information
gnprice committed Oct 13, 2023
1 parent 6c1f87a commit ecb3ca9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:flutter/material.dart';
import 'licenses.dart';
import 'log.dart';
import 'model/binding.dart';
import 'notif.dart';
import 'widgets/app.dart';

void main() {
Expand All @@ -13,5 +14,7 @@ void main() {
}());
LicenseRegistry.addLicense(additionalLicenses);
LiveZulipBinding.ensureInitialized();
WidgetsFlutterBinding.ensureInitialized();
NotificationService.instance.start();
runApp(const ZulipApp());
}
14 changes: 14 additions & 0 deletions lib/model/store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import '../api/model/initial_snapshot.dart';
import '../api/model/model.dart';
import '../api/route/events.dart';
import '../api/route/messages.dart';
import '../api/route/notifications.dart';
import '../log.dart';
import '../notif.dart';
import 'autocomplete.dart';
import 'database.dart';
import 'message_list.dart';
Expand Down Expand Up @@ -451,6 +453,7 @@ class LivePerAccountStore extends PerAccountStore {
initialSnapshot: initialSnapshot,
);
store.poll();
store.registerNotificationToken();
return store;
}

Expand All @@ -472,4 +475,15 @@ class LivePerAccountStore extends PerAccountStore {
}
}
}

void registerNotificationToken() { // TODO retry; TODO save acked to dedupe
_registerNotificationToken();
NotificationService.instance.token.addListener(_registerNotificationToken);
}

Future<void> _registerNotificationToken() async {
final token = NotificationService.instance.token.value;
if (token == null) return;
await registerFcmToken(connection, token: token); // TODO iOS/APNs
}
}
24 changes: 24 additions & 0 deletions lib/notif.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/foundation.dart';

import 'model/binding.dart';

class NotificationService {
static NotificationService get instance => (_instance ??= NotificationService._());
static NotificationService? _instance;

NotificationService._();

void start() async {
await ZulipBinding.instance.firebaseInitializeApp();
_getToken();
}

ValueNotifier<String?> token = ValueNotifier(null);

Future<void> _getToken() async {
final result = await ZulipBinding.instance.firebaseMessaging.getToken(); // TODO(log) if null
print("notif token: $result");
token.value = result;
}
}

0 comments on commit ecb3ca9

Please sign in to comment.