英文:
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("firebase_key.json")
});
//If you use MulticastMessage() then you can send a list of tokens
var message = new Message()
{
Data = new Dictionary<string, string>()
{
{"myData", "detail sent in message" },
},
Token = "token id for the specific device",
//Topic = "my topic", // only if not targeting a specific device(s)
Notification = new Notification()
{
Title = "Test from code",
Body = "Body of message is here",
},
};
string response = FirebaseMessaging.DefaultInstance.SendAsync(message).Result;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论