我使用此代码向Android设备发送通知,但它不适用于iOS设备
iOS开发人员在注册期间设置了apple的deviceId并且我已经使用它了
public string SendNotification(string message, string deviceId, bool IsiOS)
{
try
{
string SERVER_API_KEY = "AIzaS......gcw";
if (IsiOS)
{
SERVER_API_KEY = "AIza.......3k";
}
var SENDER_ID = "tok.....712";
//"850.........538";
var value = message;
WebRequest tRequest;
tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
//"https://android.googleapis.com/gcm/send");
tRequest.Method = "post";
tRequest.ContentType = " application/x-www-form-urlencoded;charset=UTF-8";
tRequest.Headers.Add(string.Format("Authorization: key={0}", SERVER_API_KEY));
tRequest.Headers.Add(string.Format("Sender: id={0}", SENDER_ID));
string postData = "collapse_key=score_update&time_to_live=604800&delay_while_idle=1&data.message=" +
System.Uri.EscapeDataString(value) + "&data.time=" + System.DateTime.Now.ToString() +
"®istration_id=" + deviceId + "";
Console.WriteLine(postData);
Byte[] byteArray = Encoding.UTF8.GetBytes(postData);
tRequest.ContentLength = byteArray.Length;
Stream dataStream = tRequest.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
WebResponse tResponse = tRequest.GetResponse();
dataStream = tResponse.GetResponseStream();
StreamReader tReader = new StreamReader(dataStream);
String sResponseFromServer = tReader.ReadToEnd();
tReader.Close();
dataStream.Close();
tResponse.Close();
return sResponseFromServer;
}
catch
{
return "";
}
}
我必须改变别的吗?
注意:我只是更改“Key”和deviceId“
我发送给苹果设备的json也是这样的:
{“to”:“cu8FBLt .................. WRFw61”,“通知”:{ “title”:“title”,“body”:“des ...”,“badge”:1}}
答案 0 :(得分:0)
您没有告诉我们 - 或者 - 您是否配置了firebase控制台。您是否已将APN证书上传到您的firebase项目?
查看详细的tutorial,特别是在本章中: "为推送通知配置Firebase"
推送推送通知比运行推送通知更加复杂。
答案 1 :(得分:0)
您是否添加了推送通知证书? 您必须为ios添加证书。
添加证书的步骤: