我正在使用Firebase跟踪我的应用程序上的设备。当我的设备上的网络断开连接时,我想在Firebase上进行通知。根据以下链接, https://firebase.google.com/docs/database/android/offline-capabilities 我正在使用以下代码:
#if __ANDROID__
FirebaseDatabase firebase = FirebaseDatabase.Instance;
FirebaseDatabase.Instance.SetPersistenceEnabled(true);
var userListRef = firebase.GetReference("USERS_ONLINE");
var myUserRef = userListRef.Push();
DatabaseReference connectedRef = firebase.GetReference(".info/connected");
connectedRef.AddValueEventListener(new ValueEventListener());
ValueEventListener类:
internal class ValueEventListener : Java.Lang.Object, IValueEventListener
{
// public IntPtr Handle => throw new NotImplementedException();
public void Dispose()
{
throw new NotImplementedException();
}
public void OnCancelled(DatabaseError error)
{
throw new NotImplementedException();
}
public void OnDataChange(DataSnapshot snapshot)
{
FirebaseDatabase firebase = FirebaseDatabase.Instance;
DatabaseReference myConnectionsRef = firebase.GetReference("users/joe/connections");
bool connected = Convert.ToBoolean(snapshot.GetValue(true));
myConnectionsRef.KeepSynced(true);
if (connected)
{
DatabaseReference con = myConnectionsRef.Push();
con.OnDisconnect().RemoveValue();
myConnectionsRef.SetValue(Boolean.TrueString);
}
else
{
DatabaseReference con = myConnectionsRef.Push();
con.OnDisconnect().RemoveValue();
myConnectionsRef.SetValue(Boolean.FalseString);
}
}
}
但是它仅在在线时才有效。谁能帮我解决这个问题。
答案 0 :(得分:0)
在我的xaml.cs文件中:
#if __ANDROID__
string myConnectionsRefNode = "users/Truckers/Joe/connections";
Constants.connectedRef = FirebaseDatabase.Instance.GetReference(".info/connected");
Constants.myConnectionsRef = FirebaseDatabase.Instance.GetReference(myConnectionsRefNode);
Constants.connectedRef.AddValueEventListener(new MyProject.Droid.Services.ValueEventListener());
#elif __IOS__
var context = AppDelegate.Instance;
if(context!=null)
{
context.FCMOfflineAccess();
}
#endif
在Constants.cs文件中:
public static bool IsFCMOffline = true;
public static DatabaseReference connectedRef;
public static DatabaseReference myConnectionsRef;
在Droid / ValueEventListener.cs文件中:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Firebase.Database;
using MyProject.Common;
namespace MyProject.Droid.Services
{
public class ValueEventListener : Java.Lang.Object, IValueEventListener
{
public void OnCancelled(DatabaseError error)
{
throw new NotImplementedException();
}
public void OnDataChange(DataSnapshot snapshot)
{
DatabaseReference con = Constants.myConnectionsRef.Ref;
con.OnDisconnect().SetValue("Offline");
con.SetValue("Online");
}
}
}
在iOS / AppDelegate.cs文件中:
public void FCMOfflineAccess()
{
var connectedRef = Database.DefaultInstance;
var MyConnectedRef = connectedRef.GetReferenceFromPath("users/Truckers/" + TruckerId);
var connectedRef1 = connectedRef.GetReferenceFromPath(".info/connected");
connectedRef1.ObserveEvent(DataEventType.Value, HandleAction);
}
private void HandleAction(DataSnapshot snapshot)
{
try
{
var connectedRef = Database.DefaultInstance;
var con = connectedRef.GetReferenceFromPath("users/Truckers/Joe");
string[] statOffline = { "Offline" };
string[] keyOffline = { "connections" };
var dataOffline = NSDictionary.FromObjectsAndKeys(statOffline, keyOffline, keyOffline.Length);
con.SetValueOnDisconnect(dataOffline);
string[] statOnline = { "Online" };
string[] keyOnline = { "connections" };
var dataOnline = NSDictionary.FromObjectsAndKeys(statOnline, keyOnline, keyOnline.Length);
con.SetValue(dataOnline);
}
catch (Exception ex)
{
}
}