使用Firebase使用xamarin表单进行用户在线跟踪
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Firebase使用xamarin表单进行用户在线跟踪相关的知识,希望对你有一定的参考价值。
我正在使用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);
但它只在网上工作。任何人都可以帮我解决这个问题。
答案
在我的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)
以上是关于使用Firebase使用xamarin表单进行用户在线跟踪的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Xamarin 的 firebase 中更新用户显示名称?
Xamarin Firebase iOS Analytics 抛出 AOT MTOUCH ERROR MT3001 - Firebase.Analytics.dll