在应用程序被终止后,通过Firebase(FCM)使用HTTP请求将推送通知发送到iOS设备
问题描述:
我在通过HTTP请求向iOS设备发送推送通知时遇到问题。当应用程序处于前台或在后台运行时,所有功能都按预期工作。但如果我杀了应用程序,它将无法正常工作。如果应用程序被终止,我可以通过Firebase控制台将通知发送到我的应用程序,因此我相信我的代码必须出错。在应用程序被终止后,通过Firebase(FCM)使用HTTP请求将推送通知发送到iOS设备
这是我发送推送通知代码:
private void SendPushNotification(string devicetoken, string header, string content, string pushdescription)
{
var textNotification = new
{
to = devicetoken,
notification = new
{
title = header,
text = content,
content_available = true,
sound = "enabled",
priority = "high",
id = pushdescription,
},
project_id = "rrp-mobile",
};
var senderId = "212579566459";
var notificationJson = Newtonsoft.Json.JsonConvert.SerializeObject(textNotification);
using (var client = new WebClient())
{
client.Encoding = Encoding.UTF8;
client.Headers[HttpRequestHeader.ContentType] = "application/json";
client.Headers[HttpRequestHeader.Authorization] = "key=AIfrSyAtgsWCMH4s_bOyj-Us4CrdsifHv-GqElg";
client.Headers["Sender"] = $"id={senderId}";
client.Headers[HttpRequestHeader.ContentType] = "application/json";
client.UploadString("https://fcm.googleapis.com/fcm/send", "POST", notificationJson);
}
}
我忘记了一些东西在这里?这适用于在前台,后台以及应用程序被终止时向Android设备发送推送通知,以及像前面和背景中的iOS设备一样。
唯一的问题是当应用程序被终止时向iOS设备发送推送通知。有没有人有任何想法,我会如何解决这个问题?
答
我刚刚意识到我的错误,而且非常简单。我在此张贴这篇文章是因为我相信这可能是一件容易的事。
var textNotification = new
{
to = devicetoken,
notification = new
{
title = header,
text = content,
content_available = true,
sound = "enabled",
**priority = "high",**
id = pushdescription,
},
project_id = "rrp-mobile",
};
你需要确保优先级属性外的“通知”规定的范围,就像这样:
var textNotification = new
{
to = devicetoken,
**priority = "high",**
notification = new
{
title = header,
text = content,
content_available = true,
sound = "enabled",
id = pushdescription,
},
project_id = "rrp-mobile",
};
这将使即使应用程序被杀死了你的推送通知交付。
这确实是一个常见的错误:没有发送消息作为高优先级,它很可能(但不是保证)被丢弃或很晚的交付。请参阅http://*.com/questions/37332415/cant-send-push-notifications-using-the-server-api/37550067#37550067 –