我需要使用我的服务在 WCF API 中构建的 Firebase 云消息向移动设备发送通知



我尝试使用从这个网站获得的这段代码。它只是返回(成功:假),我不知道为什么AndroidFCMPushNotificationStatus 类,用于从 FCM 服务器返回反馈。代码的链接 我试过了。

 public AndroidFCMPushNotificationStatus SendNotification(string serverApiKey, string senderId, string deviceId, string message)
    {
        AndroidFCMPushNotificationStatus result = new AndroidFCMPushNotificationStatus();
        try
        {
            result.Successful = false;
            result.Error = null;
            var value = message;
            WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
            tRequest.Method = "post";
            tRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
            tRequest.Headers.Add(string.Format("Authorization: key={0}", serverApiKey));
            tRequest.Headers.Add(string.Format("Sender: id={0}", senderId));
            string postData = "collapse_key=score_update&time_to_live=108&delay_while_idle=1&data.message=" + value
                + "&data.time=" + System.DateTime.Now.ToString() + "&registration_id=" + deviceId + "";
            Byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            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 sResponseFromServer = tReader.ReadToEnd();
                            result.Response = sResponseFromServer;
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            result.Successful = false;
            result.Response = null;
            result.Error = ex;
        }
        return result;
    }

    public class AndroidFCMPushNotificationStatus
    {
        public bool Successful
        {
            get;
            set;
        }
        public string Response
        {
            get;
            set;
        }
        public Exception Error
        {
            get;
            set;
        }
    }

我在其他问题中找到了这个问题的答案。解决方案是使用匿名类型,然后通过JavaScriptSerializer将其转换为Json。您可以在此链接中阅读它。

最新更新