在令牌刷新时生成刷新令牌,并在安装应用程序时存储在数据库中。
令牌生成的代码:
[Service]
[IntentFilter(new[] { "com.google.firebase.INSTANCE_ID_EVENT" })]
public class MyFirebaseIIDService : FirebaseInstanceIdService
{
const string TAG = "MyFirebaseIIDService";
//string refreshedToken;
private HttpClient client = new HttpClient();
public override void OnTokenRefresh()
{
var refreshedToken = FirebaseInstanceId.Instance.Token;
Log.Debug(TAG, "Refreshed token: " + refreshedToken);
SendRegistrationToServer(refreshedToken);
}}
可以使用SqlConnection
类SendRegistrationToServer()
方法连接到 sql 服务器。
例如:
void SendRegistrationToServer(string token)
{
using (SqlConnection connection = new SqlConnection(yourconnectionString))
{
SqlCommand command = new SqlCommand("INSERT INTO table1 (id, token) VALUES('1', '" + token + "')", connection);
command.Connection.Open();
command.ExecuteNonQuery();
}
}