我正在尝试向 Android 设备发送通知。
我正在使用这个插件CrossGeeks/PushNotificationPlugin。 我创建了 Firebase 项目并提取了用于我使用 .NET Core MVC 3.1 制作的 webapi 的参数。 当我发送通知时,事件上的断点被正确触发
CrossPushNotification.Current.OnNotificationReceived + = (s, p) =>
但是,我的设备上没有显示任何通知。 另一方面,如果我使用 Firebase 控制台发送通知,则它会在非活动应用程序和前台应用程序中正确显示。
因此,由于我的客户端应用程序正确接收了来自 Firebase 的通知,我认为问题出在服务器端。 这是我的代码:
[HttpPost, Route("Prova")]
public async Task<IActionResult> SendNotication()
{
try
{
var notification = new GoogleNotification();
notification.Data = new GoogleNotification.DataPayload();
notification.Data.Title = "Title";
notification.Data.Body = "bla bla";
using (var fcm = new FcmSender("xxxxxx", "yyyyy"))
{
await fcm.SendAsync("hhhhhhh", notification);
}
return Ok("Done");
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
public class GoogleNotification
{
public class DataPayload
{
[JsonProperty("title")]
public string Title { get; set; }
// Add your custom properties as needed
[JsonProperty("body")]
public string Body { get; set; }
[JsonProperty("tag")]
public string Tag { get; set; }
}
//[JsonProperty("priority")]
//public string Priority { get; set; } = "high";
[JsonProperty("data")]
public DataPayload Data { get; set; }
}
我对
GoogleNotification
课程有疑问,你认为正确吗?
我尝试通过 Chrome 控制台来理解生成 Firebase 控制台的 JSON。事实上,它与我的代码非常不同:
{
"basics": {},
"productAction": {
"notificationAction": {
"campaignId": "1581564767326567420",
"messageText": "ciao",
"campaignStatus": "STATUS_ENQUEUED",
"displayParameters": {
"title": "Titolo",
"priority": "HIGH"
},
"registrationIds": [
"dcdsdsffds"
],
"expiryTime": "2419200s",
"lastUpdateTime": "2020-01-23T09:15:57.079Z"
}
}
}
在 Firebase Cloud Messaging 上,您可以向客户端发送两种类型的消息,请参阅 Firebase 推送通知:
通知消息- 当应用程序处于后台时发送。仅当您的应用程序位于前台时,这些消息才会触发 onMessageReceived() 回调。 Firebase 将为 Android 设备上显示的通知提供 ui。
{
"notification" :
{
"body" : "hello",
"title": "firebase",
"sound": "default",
"tag" : "msg"
},
"registration_ids" : ["eoPr8fUIPns:APA91bEVYODiWaL9a9JidumLRcFjVrEC4iHY80mHSE1e-udmPB32RjMiYL2P2vWTIUgYCJYVOx9SGSN_R4Ksau3vFX4WvkDj4ZstxqmcBhM3K-NMrX8P6U0sdDEqDpr-lD_N5JWBLCoV"]
}
数据消息- 由客户端应用程序处理。即使您的应用程序处于前台/后台/已终止状态,这些消息也会触发 onMessageReceived() 回调。使用此类消息时,您是提供 UI 并在 Android 设备上收到推送通知时进行处理的人。
{
"data": {
"message" : "my_custom_value",
"other_key" : true,
"body":"test"
},
"priority": "high",
"condition": "'general' in topics"
}