Firebase Cloud Messaging内容未发送。

huangapple go评论68阅读模式
英文:

Firebase Cloud Messaging content is not sending

问题

I'm trying to send a notification via a back-end server that I made, and I think my content that I send with the call is incorrect.

var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, "https://fcm.googleapis.com/fcm/send?project_id=bespaargroen-370414");
request.Headers.Add("Authorization", "Bearer AAAA4u_fn_I:MyKey");
var content = JsonConvert.SerializeObject(not, Formatting.Indented);
request.Content = new StringContent(content);

var response = client.Send(request);
response.EnsureSuccessStatusCode();
var repsononej = response.Content.ReadAsStringAsync();

return new BoolResponse()
{
    Success = true,
    Message = "Bericht verzenden gelukt"
};

The content variable leads to a model I made:

public class NotificationData
{
    [JsonPropertyName("title")]
    public string Title { get; set; }
    [JsonPropertyName("body")]

    public string Body { get; set; }

    public string from { get; set; }
    public bool read { get; set; }
    public string to { get; set; }
    public bool mutable_content { get; set; }
    public string sound { get; set; }
}

Now the question is: how should I do it properly? Because I want to connect an easy UI to this so I can send messages when I want and how I want it.

I do not get any errors back. The message I send just goes to the void.

英文:

I'm trying to send a notification via an back-end server that I made and I think my content that I send with the call is incorrect.

var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, "https://fcm.googleapis.com/fcm/send?project_id=bespaargroen-370414");
request.Headers.Add("Authorization", "Bearer AAAA4u_fn_I:MyKey");
var content = JsonConvert.SerializeObject(not, Formatting.Indented);
request.Content = new StringContent(content);

var response = client.Send(request);
response.EnsureSuccessStatusCode();
var repsononej = response.Content.ReadAsStringAsync();

return new BoolResponse()
{
    Success = true,
    Message = "Bericht verzenden gelukt"
};

the content variable leads to a model i made:

public class NotificationData
{
    [JsonPropertyName("title")]
    public string Title { get; set; }
    [JsonPropertyName("body")]

    public string Body { get; set; }

    public string from { get; set; }
    public bool read { get; set; }
    public string to { get; set; }
    public bool mutable_content { get; set; }
    public string sound { get; set; }
}

now the question is: how should i do it properly. because I want to connect an easy UI to this so i can send messages when i want it and how i want it.

I do not get any errors back. the message i send just goes to the void.

答案1

得分: 1

以下是已翻译的内容:

"可能存在使用的设备令牌有问题。它可能已经不再有效或不正确。

注意:您可能需要关闭手机应用才能接收通知。
此外,您必须确保手机上的应用实际上可以接收通知。当我本地安装我的测试应用(而不是通过Google Play安装)时,默认情况下会阻止通知。

但我建议您使用FirebaseAdmin nuget API来简化与API的交互。然后您可以像这样做:

var instance = FirebaseApp.Create(new AppOptions
{
    //或者根据您的需求加载
    Credential = GoogleCredential.FromFile("firebase_key.json")
});

//如果您使用MulticastMessage(),则可以发送一组令牌
var message = new Message()
{
    Data = new Dictionary<string, string>()
    {
        {"myData", "消息中发送的详细信息" },
    },
    Token = "特定设备的令牌ID",
    //Topic = "我的主题", // 仅当不针对特定设备时使用
    Notification = new Notification()
    {
        Title = "来自代码的测试",
        Body = "消息正文在这里",
    },            
};

string response = FirebaseMessaging.DefaultInstance.SendAsync(message).Result;

希望这对您有所帮助。

英文:

It is probable that there is a problem with the device token that you are using.
It could be no longer valid, or incorrect.

NOTE: You might find that the phone app must be closed for it to receive the notification.
Also, you must ensure that the app on the phone can actually receive notifications. When I install my test apps locally (not via Google play), then by default notifications are blocked.

But I would suggest to use the FirebaseAdmin nuget API to simplify you interaction with the API.
Then you can do something like this.

var instance = FirebaseApp.Create(new AppOptions
    {
         //or however you want to load it
        Credential = GoogleCredential.FromFile(&quot;firebase_key.json&quot;)
    });

//If you use MulticastMessage() then you can send a list of tokens
var message = new Message()
{
    Data = new Dictionary&lt;string, string&gt;()
    {
        {&quot;myData&quot;, &quot;detail sent in message&quot; },
    },
    Token = &quot;token id for the specific device&quot;,
    //Topic = &quot;my topic&quot;, // only if not targeting a specific device(s)
    Notification = new Notification()
    {
        Title = &quot;Test from code&quot;,
        Body = &quot;Body of message is here&quot;,
    },            
};

string response = FirebaseMessaging.DefaultInstance.SendAsync(message).Result;

huangapple
  • 本文由 发表于 2023年4月11日 15:42:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75983527.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定