将FCM通知推送到iOS时出现问题

时间:2019-01-10 16:30:04

标签: c# ios iphone asp.net-mvc

我在C#中有将通知推送到android和ios的方法,android接收通知,但是IOS开发人员说没有收到通知,尽管输出响应成功为1,如下所示:

  

multicast_id \“:8095934738241910690,\” success \“:1,\” failure \“:0,\” canonical_ids \“:0,\” results \“:[{\” message_id \“:\” 0 :1547134100665140%046d9ccbf9fd7ecd \“}]}”

ios设备没有收到消息是什么问题?

我的代码是

var IsSuccess = false;
try
{
    string ToDevice = "XXX";
    string serverApiKey = "XXX";
    string senderId = "XXX"; 
    using (var db = new NurseryCareEntities())
    {
        WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
        tRequest.Method = "post";
        tRequest.ContentType = "application/json";
        var data = new
        {
            to = ToDevice,
            data = new
            {
                body = "My Message",
                title = "Title",
                sound = "Enabled"
            }
        };
        var serializer = new JavaScriptSerializer();
        var json = serializer.Serialize(data);
        Byte[] byteArray = Encoding.UTF8.GetBytes(json);
        tRequest.Headers.Add(string.Format("Authorization: key={0}", serverApiKey));
        tRequest.Headers.Add(string.Format("Sender: id={0}", senderId));
        tRequest.ContentLength = byteArray.Length;
        using (Stream dataStream = tRequest.GetRequestStream())
        {
            dataStream.Write(byteArray, 0, byteArray.Length);
            using (WebResponse tResponse = tRequest.GetResponse())
            {
                using (Stream dataStreamResponse = tResponse.GetResponseStream())
                {
                    using (StreamReader tReader = new StreamReader(dataStreamResponse))
                    {
                        String response = tReader.ReadToEnd();
                        if (!response.Contains("Unauthorized"))
                        {
                            var ObjArr = response.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                            if (ObjArr.Length > 0)
                            {
                                var Success = ObjArr[1].Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
                                if (Success.Length > 0)
                                {
                                    if (Success[1] == "0")  // Error occur
                                    {
                                        db.Database.ExecuteSqlCommand(string.Format("EXEC Add_IOS_error '{0}'", "Message Not sent"));
                                        db.SaveChanges();
                                    }
                                    else  // Notification Sent Successfully
                                    {
                                        IsSuccess = true;
                                        db.Database.ExecuteSqlCommand(string.Format("EXEC Add_IOS_error '{0}'", "Sent"));
                                        db.SaveChanges();
                                    }
                                }
                            }

                        }
                        return IsSuccess;
                    }
                }
            }
        }
    }
}
catch (Exception exception)
{
    using (var db = new NurseryCareEntities())
    {
        db.Database.ExecuteSqlCommand(string.Format("EXEC Add_IOS_error '{0}'", exception.Message.ToString()));
        db.SaveChanges();
    }
    return IsSuccess;
}

0 个答案:

没有答案