コード例 #1
0
        public static List <AppSupplierTokenUI> GetAllAPNSAppSupplierTokenUI()
        {
            List <AppSupplierTokenUI> AppSuppliersTokensUI = new List <AppSupplierTokenUI>();

            using (ConnectorBase conn = ConnectorBase.NewInstance())
            {
                Query qry = Query.New <AppSupplierAPNSToken>()
                            .Select(AppSupplierAPNSToken.Columns.Token)
                            .AddSelect(AppSupplierAPNSToken.Columns.SupplierId)
                            .AddSelect(AppSupplier.TableSchema.SchemaName, AppSupplier.Columns.UnreadNotificationCount, AppSupplier.Columns.UnreadNotificationCount)
                            .Join(JoinType.InnerJoin, AppSupplier.TableSchema, AppSupplier.TableSchema.SchemaName, new JoinColumnPair(AppSupplierAPNSToken.TableSchema, AppSupplierAPNSToken.Columns.SupplierId, AppSupplier.Columns.SupplierId));

                using (DataReaderBase reader = qry.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        AppSupplierTokenUI AppSupplierTokensUI = new AppSupplierTokenUI();
                        AppSupplierTokensUI.SupplierId = Convert.ToInt64(reader[AppSupplierAPNSToken.Columns.SupplierId]);
                        AppSupplierTokensUI.Token      = reader[AppSupplierAPNSToken.Columns.Token].ToString();
                        AppSupplierTokensUI.UnreadNotificationCount = Convert.ToInt32(reader[AppSupplier.Columns.UnreadNotificationCount]);
                        AppSuppliersTokensUI.Add(AppSupplierTokensUI);
                    }
                }
            }
            return(AppSuppliersTokensUI);
        }
コード例 #2
0
        private static void SendApnsNotification(string MessageLocKey, object[] MessageLocArgs, string Sound, string ActionType, Dictionary <string, object> CustomItems)
        {
            List <AppSupplierTokenUI> tokens = AppSupplierTokenUI.GetAllAPNSAppSupplierTokenUI();

            NotificationAlert alert = new NotificationAlert();

            alert.LocalizedKey = MessageLocKey;
            if (MessageLocArgs != null)
            {
                alert.LocalizedArgs.AddRange(MessageLocArgs);
            }

            Dictionary <string, object[]> customItems = null;

            if (ActionType != null)
            {
                customItems = new Dictionary <string, object[]>();
                customItems[@"app-action"] = new object[] { ActionType };
            }
            if (CustomItems != null)
            {
                foreach (string key in CustomItems.Keys)
                {
                    if (customItems == null)
                    {
                        customItems = new Dictionary <string, object[]>();
                    }
                    customItems[key] = new object[] { CustomItems[key] };
                }
            }

            foreach (AppSupplierTokenUI token in tokens)
            {
                NotificationService service = APNSServiceSupplier.SharedInstance;
                service.SendMessage(token.Token,
                                    alert,
                                    token.UnreadNotificationCount,
                                    (Sound == null || Sound.Length == 0) ? @"default" : Sound,
                                    customItems,
                                    delegate()
                {
                    Query qry = Query.New <AppSupplierAPNSToken>().Delete()
                                .Where(AppSupplierAPNSToken.Columns.Token, token);
                });
            }
        }
コード例 #3
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 { }
                    }
                });
            }
        }