diff --git a/lib/notifications.dart b/lib/notifications.dart index b44f94aa1c..3609ca8f4f 100644 --- a/lib/notifications.dart +++ b/lib/notifications.dart @@ -47,7 +47,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); + ZulipBinding.instance.firebaseMessagingOnBackgroundMessage(_onBackgroundMessage); NotificationDisplayManager._init(); @@ -77,8 +78,27 @@ class NotificationService { token.value = value; } - static void _onRemoteMessage(FirebaseRemoteMessage message) { + static void _onForegroundMessage(FirebaseRemoteMessage message) { assert(debugLog("notif message: ${message.data}")); + _onRemoteMessage(message); + } + + static Future _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); + } + + static void _onRemoteMessage(FirebaseRemoteMessage message) { final data = FcmMessage.fromJson(message.data); switch (data) { case MessageFcmMessage(): NotificationDisplayManager._onMessageFcmMessage(data, message.data);