C#Firebase Admin SDK推送通知



我想向移动应用程序发送推送通知,所以我在我的上安装了Firebase Admin SDK。Net项目。我遵循了Firebase文档中的说明,但当我从SDK调用SendAsync时,它只是挂起。知道它为什么会挂起来吗?我是不是少了几步?

这是我的API代码(注意,这只是让它工作的演示代码(:

public async void SendPushNotification()
{
FirebaseApp.Create(new AppOptions()
{
Credential = GoogleCredential.FromFile("path to json file"),
});
var registrationToken = "fqCo_-tXKvY:APA91bGQ47Q2egnqn4Ml...";
var message = new FirebaseAdmin.Messaging.Message()
{
Token = registrationToken,
};
string response = await FirebaseMessaging.DefaultInstance.SendAsync(message);
Console.WriteLine("Successfully sent message: " + response);
}

您有一个死锁,因为SendAsync永远不会完成。如果从ui线程调用,则需要考虑使用ConfigureAwait(false)所以你的代码片段如下所示:

public async void SendPushNotification()
{
FirebaseApp.Create(new AppOptions()
{
Credential = GoogleCredential.FromFile("path to json file"),
});
var registrationToken = "fqCo_-tXKvY:APA91bGQ47Q2egnqn4Ml...";
var message = new FirebaseAdmin.Messaging.Message()
{
Token = registrationToken,
};
string response = await FirebaseMessaging.DefaultInstance.SendAsync(message).ConfigureAwait(false);
Console.WriteLine("Successfully sent message: " + response);
}

firebaseAdmin消息传递在.net库中引发了一个令牌错误,因此我用Postman进行了测试并工作,我能够发送通知,我看到Postman为Resharper库生成了一个代码,并且易于安装和使用,最好用Json序列化消息并作为参数发送

using Firebase.Auth;
using FirebaseAdmin;
using FirebaseAdmin.Auth;
using Google.Apis.Auth.OAuth2;
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using FirebaseAuth = FirebaseAdmin.Auth.FirebaseAuth;
using FirebaseAdmin.Messaging;
using RestSharp;
using Newtonsoft.Json;
namespace messaging
{
class Program
{
static async System.Threading.Tasks.Task Main(string[] args)
{
string token = "dtPMeTshqr??????????????";

var data = new
{
to = token,
notification = new
{
body = "Nueva notificacion de prueba",
title = "Notificacion de prueba",
},
priority = "high"
};
var json = JsonConvert.SerializeObject(data);
Sendmessage(json);
}
private static string Sendmessage(string json)
{
var client = new RestClient("https://fcm.googleapis.com/fcm/send");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Authorization", "key=AAAAexjc2Qg:APx??????????????");
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/json", json, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Console.WriteLine(response.Content);
return response.Content;
} 
}
}

相关内容

  • 没有找到相关文章

最新更新