Esempio n. 1
0
        public static List <AppUserTokenUI> GetAllAPNSAppUserTokenUI()
        {
            List <AppUserTokenUI> appUsersTokensUI = new List <AppUserTokenUI>();

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

                using (DataReaderBase reader = qry.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        AppUserTokenUI appUserTokensUI = new AppUserTokenUI();
                        appUserTokensUI.AppUserId = Convert.ToInt64(reader[AppUserAPNSToken.Columns.AppUserId]);
                        appUserTokensUI.Token     = reader[AppUserAPNSToken.Columns.Token].ToString();
                        appUserTokensUI.UnreadNotificationCount = Convert.ToInt32(reader[AppUser.Columns.UnreadNotificationCount]);
                        appUsersTokensUI.Add(appUserTokensUI);
                    }
                }
            }
            return(appUsersTokensUI);
        }
Esempio n. 2
0
        private static void SendApnsNotification(string MessageLocKey, object[] MessageLocArgs, string Sound, string ActionType, Dictionary <string, object> CustomItems)
        {
            List <AppUserTokenUI> tokens = AppUserTokenUI.GetAllAPNSAppUserTokenUI();

            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[key] == null)
                    {
                        continue;
                    }
                    if (customItems == null)
                    {
                        customItems = new Dictionary <string, object[]>();
                    }
                    customItems[key] = new object[] { CustomItems[key] };
                }
            }

            foreach (AppUserTokenUI token in tokens)
            {
                NotificationService service = APNSService.SharedInstance;
                service.SendMessage(token.Token,
                                    alert,
                                    token.UnreadNotificationCount,
                                    (Sound == null || Sound.Length == 0) ? @"default" : Sound,
                                    customItems,
                                    delegate()
                {
                    Query qry = Query.New <AppUserAPNSToken>().Delete()
                                .Where(AppUserAPNSToken.Columns.Token, token);
                });
            }
        }
Esempio n. 3
0
        private static void SendGcmNotification(string CollapseKey, string ActionType, Dictionary <string, object> CustomItems)
        {
            List <AppUserTokenUI> tokens = AppUserTokenUI.GetAllGcmAppUserTokenUI();

            foreach (AppUserTokenUI t in tokens)
            {
                dg.Utilities.GoogleCloudMessaging.HttpNotificationPayload payload = new dg.Utilities.GoogleCloudMessaging.HttpNotificationPayload(t.Token);
                payload.CollapseKey           = CollapseKey;
                payload.RestrictedPackageName = GcmService.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 = GcmService.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 <AppUserGcmToken>()
                                            .Update(AppUserGcmToken.Columns.Token, jToken.Value <string>())
                                            .Where(AppUserGcmToken.Columns.Token, payload.RegistrationIds[resultIndex])
                                            .Execute();
                                        }
                                        else
                                        {
                                            if (result.TryGetValue("error", out jToken))
                                            {
                                                if (jToken.Value <string>() == "NotRegistered")
                                                {
                                                    Query.New <AppUserGcmToken>()
                                                    .Delete()
                                                    .Where(AppUserGcmToken.Columns.Token, payload.RegistrationIds[resultIndex])
                                                    .Execute();
                                                }
                                            }
                                        }
                                        resultIndex++;
                                    }
                                }
                            }
                        }
                        catch { }
                    }
                });
            }
        }