From cf28e6468a4c16deb8713c11a9d7b41906abca57 Mon Sep 17 00:00:00 2001 From: Greg Price Date: Wed, 18 Oct 2023 17:38:16 -0700 Subject: [PATCH] wip notif handle when in background; TODO binding --- lib/notifications.dart | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/notifications.dart b/lib/notifications.dart index df20886bbe..5e435537b2 100644 --- a/lib/notifications.dart +++ b/lib/notifications.dart @@ -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'; @@ -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(); @@ -78,8 +80,27 @@ class NotificationService { } } -void _onRemoteMessage(FirebaseRemoteMessage message) { +void _onForegroundMessage(FirebaseRemoteMessage message) { assert(debugLog("notif message: ${message.data}")); + _onRemoteMessage(message); +} + +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); +} + +void _onRemoteMessage(FirebaseRemoteMessage message) { final data = FcmMessage.fromJson(message.data); switch (data) { case MessageFcmMessage(): NotificationDisplayManager._onMessageFcmMessage(data, message.data);