Skip to content

Commit

Permalink
wip notif handle when in background; TODO binding
Browse files Browse the repository at this point in the history
  • Loading branch information
gnprice committed Oct 19, 2023
1 parent d91fbfa commit cf28e64
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions lib/notifications.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:convert';

import 'package:collection/collection.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
Expand Down Expand Up @@ -47,7 +48,8 @@ class NotificationService {
// TODO(#324) defer notif setup if user not logged into any accounts
// (in order to avoid calling for permissions)

ZulipBinding.instance.firebaseMessagingOnMessage.listen(_onRemoteMessage);
ZulipBinding.instance.firebaseMessagingOnMessage.listen(_onForegroundMessage);
FirebaseMessaging.onBackgroundMessage(_onBackgroundMessage);

NotificationDisplayManager._init();

Expand Down Expand Up @@ -78,8 +80,27 @@ class NotificationService {
}
}

void _onRemoteMessage(FirebaseRemoteMessage message) {
void _onForegroundMessage(FirebaseRemoteMessage message) {
assert(debugLog("notif message: ${message.data}"));
_onRemoteMessage(message);
}

Future<void> _onBackgroundMessage(FirebaseRemoteMessage message) async {
// This callback will run in a separate isolate from the rest of the app.
// See docs:
// https://firebase.flutter.dev/docs/messaging/usage/#background-messages
assert(() {
debugLogEnabled = true;
return true;
}());
LiveZulipBinding.ensureInitialized();
NotificationDisplayManager._init();

assert(debugLog("notif message in background: ${message.data}"));
_onRemoteMessage(message);
}

void _onRemoteMessage(FirebaseRemoteMessage message) {
final data = FcmMessage.fromJson(message.data);
switch (data) {
case MessageFcmMessage(): NotificationDisplayManager._onMessageFcmMessage(data, message.data);
Expand Down

0 comments on commit cf28e64

Please sign in to comment.