Exemple #1
0
        private static void SendGcmNotification(string CollapseKey, string ActionType, Dictionary <string, object> CustomItems)
        {
            List <AppSupplierTokenUI> tokens = AppSupplierTokenUI.GetAllGcmAppSupplierTokenUI();

            foreach (AppSupplierTokenUI t in tokens)
            {
                dg.Utilities.GoogleCloudMessaging.HttpNotificationPayload payload = new dg.Utilities.GoogleCloudMessaging.HttpNotificationPayload(t.Token);
                payload.CollapseKey           = CollapseKey;
                payload.RestrictedPackageName = GcmServiceSupplier.PackageName;
                payload.AddCustom("badge", t.UnreadNotificationCount);
                if (ActionType != null)
                {
                    payload.AddCustom("app-action", ActionType);
                }
                if (CustomItems != null)
                {
                    foreach (string key in CustomItems.Keys)
                    {
                        if (CustomItems[key] == null)
                        {
                            continue;
                        }
                        payload.AddCustom(key, CustomItems[key]);
                    }
                }

                HttpNotificationService service = GcmServiceSupplier.SharedInstance;
                service.SendMessage(payload, x =>
                {
                    if (x.HttpStatusCode == HttpStatusCode.OK)
                    {
                        try
                        {
                            JObject response = JObject.Parse(x.Response);
                            if (response != null)
                            {
                                if (response.Value <int>("failure") > 0 || response.Value <int>("canonical_ids") > 0)
                                {
                                    JArray results  = response["results"] as JArray;
                                    int resultIndex = 0;
                                    foreach (JObject result in results)
                                    {
                                        JToken jToken;
                                        if (result.TryGetValue("registration_id", out jToken))
                                        {
                                            Query.New <AppSupplierGcmToken>()
                                            .Update(AppSupplierGcmToken.Columns.Token, jToken.Value <string>())
                                            .Where(AppSupplierGcmToken.Columns.Token, tokens[resultIndex])
                                            .Execute();
                                        }
                                        else
                                        {
                                            if (result.TryGetValue("error", out jToken))
                                            {
                                                if (jToken.Value <string>() == "NotRegistered")
                                                {
                                                    Query.New <AppSupplierGcmToken>()
                                                    .Delete()
                                                    .Where(AppSupplierGcmToken.Columns.Token, tokens[resultIndex])
                                                    .Execute();
                                                }
                                            }
                                        }
                                        resultIndex++;
                                    }
                                }
                            }
                        }
                        catch { }
                    }
                });
            }
        }
Exemple #2
0
        private static void SendGcmNotification(Int64 SupplierId, string CollapseKey, Int32 Badge, string ActionType, Dictionary <string, object> CustomItems, string timeToLive = null)
        {
            List <string> tokens = new List <string>();

            using (ConnectorBase conn = ConnectorBase.NewInstance())
            {
                Query qry = Query.New <AppSupplierGcmToken>()
                            .Select(AppSupplierGcmToken.Columns.Token)
                            .Where(AppSupplierGcmToken.Columns.SupplierId, SupplierId);

                using (DataReaderBase reader = qry.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        tokens.Add(reader.GetString(0));
                    }
                }
            }

            if (tokens.Count > 0)
            {
                dg.Utilities.GoogleCloudMessaging.HttpNotificationPayload payload = new dg.Utilities.GoogleCloudMessaging.HttpNotificationPayload(tokens.ToArray());
                payload.CollapseKey           = CollapseKey;
                payload.RestrictedPackageName = GcmServiceSupplier.PackageName;
                payload.AddCustom("badge", Badge);
                if (timeToLive != null)
                {
                    payload.TimeToLive = Convert.ToInt32(timeToLive) * 60;
                }
                if (ActionType != null)
                {
                    payload.AddCustom("app-action", ActionType);
                }
                if (CustomItems != null)
                {
                    foreach (string key in CustomItems.Keys)
                    {
                        if (CustomItems[key] == null)
                        {
                            continue;
                        }
                        payload.AddCustom(key, CustomItems[key]);
                    }
                }



                HttpNotificationService service = GcmServiceSupplier.SharedInstance;
                service.SendMessage(payload, delegate(NotificationDeliveryResult x)
                {
                    if (x.HttpStatusCode == HttpStatusCode.OK)
                    {
                        try
                        {
                            JObject response = JObject.Parse(x.Response);
                            if (response != null)
                            {
                                if (response.Value <int>("failure") > 0 || response.Value <int>("canonical_ids") > 0)
                                {
                                    JArray results  = response["results"] as JArray;
                                    int resultIndex = 0;
                                    foreach (JObject result in results)
                                    {
                                        JToken jToken;
                                        if (result.TryGetValue("registration_id", out jToken))
                                        {
                                            Query.New <AppSupplierGcmToken>()
                                            .Update(AppSupplierGcmToken.Columns.Token, jToken.Value <string>())
                                            .Where(AppSupplierGcmToken.Columns.Token, payload.RegistrationIds[resultIndex])
                                            .Execute();
                                        }
                                        else
                                        {
                                            if (result.TryGetValue("error", out jToken))
                                            {
                                                if (jToken.Value <string>() == "NotRegistered")
                                                {
                                                    Query.New <AppSupplierGcmToken>()
                                                    .Delete()
                                                    .Where(AppSupplierGcmToken.Columns.Token, payload.RegistrationIds[resultIndex])
                                                    .Execute();
                                                }
                                            }
                                        }
                                        resultIndex++;
                                    }
                                }
                            }
                        }
                        catch (Exception) { }
                    }
                });
            }
        }