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.