0

I have a realtime chat application which uses google firebase db service. Using Xamarin.Firebase.iOS.Database plugin for this is in my Xamarin iOS project. When chat message is sent from server, if the app is in foreground in a real iPhone device, it listener listens and i am able to execute the further code. However when the app is in background, it never listens to new chat from server until the app is brought to foreground. The funny part is the listener listens both background and foreground for iOS simulators.

using System.Threading.Tasks;
using Firebase.Database;
using Foundation;
using Xamarin.Forms;

[assembly: Dependency(typeof(FirebaseDBService))]
namespace MyChatApp.iOS.Services
{
    public class FirebaseDBService : IFirebaseDBService
    {
        readonly DatabaseReference roomDataBaseReference;

        public FirebaseDBService()
        {
            roomDataBaseReference = Database.DefaultInstance.GetRootReference().GetChild(Constants.Firebase.ROOMS);
        }

       
        DatabaseReference MessageReference(string roomId)
        {
            return roomDataBaseReference.GetChild(roomId).GetChild(Constants.Firebase.MESSAGES);
        }

        public void JoinChatRoom(string roomId, string userId)
        {           
            AddEventListeners(roomId);
        }

        void AddEventListeners(string roomId)
        {
            MessageReference(roomId).ObserveEvent(DataEventType.ChildAdded, OnMessageAdded);
        }

        public async void OnMessageAdded(DataSnapshot snapshot)
        {         
            //store it in object
        }
   }
}

From my Xamarin.Forms project , I call this to activate listener

DependencyService.Get<IFirebaseDBService>().JoinChatRoom(Id, username);

Please help.. the chat listener is working in foreground only for real iPhone device. But working fine in both foreground and background for simulators.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
hashimks
  • 1,205
  • 2
  • 11
  • 29
  • This is the expected behavior: you can only keep a connection open while the user is actively using the app. When they are not, you'll need to use another mechanism to send updates to the user, typically push notifications/Firebase Cloud Messaging. See https://stackoverflow.com/q/44826275, https://stackoverflow.com/q/39011182, https://stackoverflow.com/a/40093958. – Frank van Puffelen Jun 01 '21 at 14:26
  • @FrankvanPuffelen Thanks for your input and understood. But one doubt is why in iOS simulator the chat listener is working even if app is in background? – hashimks Jun 01 '21 at 14:53

0 Answers0