Exemple #1
0
        private void send(GCMMessage message)
        {
            if (!_host.HasValue())
            {
                return;
            }

            string gcmkey = _dataContext.Setting("GCMKey", ConfigurationManager.AppSettings["GCMKey"]);

            if (message.registration_ids.Count == 0 || gcmkey.Length == 0)
            {
                return;
            }

            HostingEnvironment.QueueBackgroundWorkItem(ct =>
            {
                var threadDb = CMSDataContext.Create(_host);

                string json = JsonConvert.SerializeObject(message);

                using (var webClient = new WebClient())
                {
                    webClient.Headers.Add("Authorization", "key=" + gcmkey);
                    webClient.Headers.Add("Content-Type", "application/json");
                    webClient.Encoding = Encoding.UTF8;

                    string results = webClient.UploadString(GCM_URL, json);

                    GCMResponse response = JsonConvert.DeserializeObject <GCMResponse>(results);

                    for (int iX = 0; iX < message.registration_ids.Count; iX++)
                    {
                        if (response.results.Count <= iX)
                        {
                            continue;
                        }

                        string registrationId    = message.registration_ids[iX];
                        GCMResponseResult result = response.results[iX];

                        if (!string.IsNullOrEmpty(result.error))
                        {
                            switch (result.error)
                            {
                            case "InvalidRegistration":
                            case "NotRegistered":
                                {
                                    var record = (from r in threadDb.MobileAppPushRegistrations
                                                  where r.RegistrationId == registrationId
                                                  select r).SingleOrDefault();

                                    if (record != null)
                                    {
                                        threadDb.MobileAppPushRegistrations.DeleteOnSubmit(record);
                                    }

                                    break;
                                }
                            }
                        }
                        else if (result.error != null && result.registration_id.Length > 0)
                        {
                            var record = (from r in threadDb.MobileAppPushRegistrations
                                          where r.RegistrationId == registrationId
                                          select r).SingleOrDefault();

                            if (record != null)
                            {
                                record.RegistrationId = result.registration_id;
                            }
                        }
                    }

                    threadDb.SubmitChanges();
                }
            });
        }
Exemple #2
0
        private static void send(GCMMessage message)
        {
            string gcmkey = DbUtil.Db.Setting("GCMKey", ConfigurationManager.AppSettings["GCMKey"]);

            if (message.registration_ids.Count == 0 || gcmkey.Length == 0)
            {
                return;
            }

            System.Threading.Tasks.Task.Factory.StartNew(() => {
                string json = JsonConvert.SerializeObject(message);
                string host = Util.Host;

                Thread.CurrentThread.Priority = ThreadPriority.BelowNormal;

                using (var webClient = new WebClient())
                {
                    webClient.Headers.Add("Authorization", "key=" + gcmkey);
                    webClient.Headers.Add("Content-Type", "application/json");
                    webClient.Encoding = Encoding.UTF8;

                    string results = webClient.UploadString(GCM_URL, json);

                    GCMResponse response = JsonConvert.DeserializeObject <GCMResponse>(results);

                    var Db = DbUtil.Create(host);

                    for (int iX = 0; iX < message.registration_ids.Count; iX++)
                    {
                        if (response.results.Count > iX)
                        {
                            string registrationID    = message.registration_ids[iX];
                            GCMResponseResult result = response.results[iX];

                            if (result.error != null && result.error.Length > 0)
                            {
                                switch (result.error)
                                {
                                case "InvalidRegistration":
                                case "NotRegistered":
                                    {
                                        var record = (from r in Db.MobileAppPushRegistrations
                                                      where r.RegistrationId == registrationID
                                                      select r).SingleOrDefault();

                                        if (record != null)
                                        {
                                            Db.MobileAppPushRegistrations.DeleteOnSubmit(record);
                                        }

                                        break;
                                    }
                                }
                            }
                            else if (result.error != null && result.registration_id.Length > 0)
                            {
                                var record = (from r in Db.MobileAppPushRegistrations
                                              where r.RegistrationId == registrationID
                                              select r).SingleOrDefault();

                                record.RegistrationId = result.registration_id;
                            }
                        }
                    }

                    Db.SubmitChanges();
                }
            });
        }