fcm使用C#通过主题消息将通知推送到移动设备

时间:2018-10-25 08:50:04

标签: android ios firebase firebase-cloud-messaging

我们有一项重要要求,即向超过50000个移动用户(Android和IOS)推送通知。我们是通过主题消息传递来实现的。我们为每998个用户创建了一个批处理,然后使用主题推送到功能表。但是推送通知的传递是不准确的。有时,某些设备会收到通知消息,有时,则没有设备。我们仅使用了两个主题,一个主题用于Android,另一个主题用于IOS。将多个批次(每个批次包含998个令牌)发送到同一主题。您能否分享一下这背后的问题,还是请分享如何实现上述情况。

批处理推送

 public string pushBatchbasedonTopic(string Topic, List<string> RegisterIds)
            {
                _basemobile.WriteToLog("\n Log pushBatchbasedonTopic: ","");
                string topic = Topic;
                string str = null;
                try
                {
                    string senderId = WebConfigurationManager.AppSettings["FCM_SENDER_ID"].ToString();
                    string applicationkey = WebConfigurationManager.AppSettings["FCM_KEY"].ToString();
                    WebRequest tRequest = WebRequest.Create("https://iid.googleapis.com/iid/v1:batchAdd");
                    tRequest.Method = "post";
                    tRequest.ContentType = "application/json";
                    tRequest.Timeout = 600000;

                    var dataser = new
                    {
                        to = topic,
                        registration_tokens = RegisterIds.ToArray()

                    };

                    var serializer = new JavaScriptSerializer();
                    var json = serializer.Serialize(dataser);
                    Byte[] byteArray = Encoding.UTF8.GetBytes(json);
                    tRequest.Headers.Add(string.Format("Authorization: key={0}", applicationkey));
                    tRequest.Headers.Add(string.Format("Sender: id={0}", senderId));
                    tRequest.ContentLength = byteArray.Length;
                    _basemobile.WriteToLog("\n Log tRequest: ", tRequest.ToString());
                    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();
                                    str = sResponseFromServer;
                                    _basemobile.WriteToLog("\n Log sResponseFromServer: ", sResponseFromServer);
                                }
                            }
                        }
                    }
                    _basemobile.WriteToLog("\n Log TopicCreation: " + str, "");
                    return str;

                }
                catch (Exception ex)
                {
                    str = ex.Message;
                    _basemobile.WriteToLog("\n Log topic creation  error: " + ex.Message, "");
                    _basemobile.WriteToLog("\n Stack Trace topic creation: " + ex.StackTrace, "");
                    _basemobile.WriteToLog("\n InnerException topic creation: " + ex.InnerException, "");
                    return str;
                }
            }

FCM推送

   public string pushusingFCMforBatchAndroidBasedOnOneTopic(string Id, string Condition, string Message, string Type, string count)
        {
            _basemobile.WriteToLog("\n Log pushusingFCMforBatchAndroidBasedOnOneTopic: ", "");
            string setcondition = Condition;
            //string deviceId = DeviceToken;
            string str = null;
            try
            {
                string senderId = WebConfigurationManager.AppSettings["FCM_SENDER_ID"].ToString();
                string applicationkey = WebConfigurationManager.AppSettings["FCM_KEY"].ToString();
                WebRequest tRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send");
                tRequest.Method = "post";
                tRequest.ContentType = "application/json";
                tRequest.Timeout = 600000;

                var dataser = new
                {
                    //to = deviceId,
                    condition = setcondition,
                    data = new
                    {
                        Notification_Id = Id,
                        msg_type = Type,
                        Count = count,
                        sound = "Enabled",
                        body = Message,
                        badge = count
                    }
                    // count = 0,
                    //type = Type
                };

                var serializer = new JavaScriptSerializer();
                var json = serializer.Serialize(dataser);
                Byte[] byteArray = Encoding.UTF8.GetBytes(json);
                tRequest.Headers.Add(string.Format("Authorization: key={0}", applicationkey));
                tRequest.Headers.Add(string.Format("Sender: id={0}", senderId));
                tRequest.ContentLength = byteArray.Length;
                _basemobile.WriteToLog("\n Log tRequest: ", tRequest.ToString());
                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();
                                str = sResponseFromServer;
                                _basemobile.WriteToLog("\n Log sResponseFromServer: ", sResponseFromServer);
                            }
                        }
                    }
                }
                _basemobile.WriteToLog("\n Log pushfcmandroid: " + str, "");
                return str;
            }
            catch (Exception ex)
            {
                str = ex.Message;
                _basemobile.WriteToLog("\n Log errorios: " + ex.Message, "");
                _basemobile.WriteToLog("\n Stack Traceios: " + ex.StackTrace, "");
                _basemobile.WriteToLog("\n Inner Exceptionios: " + ex.InnerException, "");
                return str;
            }
        }

0 个答案:

没有答案