// GET: Notifications/Edit/5
        public async Task <ActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Notification notification = await db.Notifications.FindAsync(id);

            if (notification == null)
            {
                return(HttpNotFound());
            }
            return(View(notification));
        }
Пример #2
0
        public static async Task SendNotificationAsync(List <string> tags, Notification not)
        {
            try
            {
                // Define a Windows Store toast.
                var wnsToast = "<toast><visual><binding template=\"ToastText01\">"
                               + "<text id=\"1\">" + not.NotificationTitle
                               + "</text><text id=\"2\">" + not.NotificationMessage
                               + "</text></binding></visual></toast>";

                await hub.SendWindowsNativeNotificationAsync(wnsToast, tags);

                //// Define a Windows Phone toast.
                //var mpnsToast =
                //    "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                //    "<wp:Notification xmlns:wp=\"WPNotification\">" +
                //        "<wp:Toast>" +
                //            "<wp:Text1>Breaking " + tagExpression + " News!</wp:Text1>" +
                //        "</wp:Toast> " +
                //    "</wp:Notification>";

                //await hub.SendMpnsNativeNotificationAsync(mpnsToast, tagExpression);

                // Define an iOS alert.
                var alert = "{\"aps\":{\"alert\":\"" + not.NotificationTitle + "\"}}";

                await hub.SendAppleNativeNotificationAsync(alert, tags);

                // Define an Android notification.
                var notification = "{\"data\":{\"message\":\"" + not.NotificationTitle + "\"}}";

                await hub.SendGcmNativeNotificationAsync(notification, tags);
            }

            catch (ArgumentException ex)
            {
                // An exception is raised when the notification hub hasn't been
                // registered for the iOS, Windows Store, or Windows Phone platform.
            }
        }
        public async Task <ActionResult> Create([Bind(Include = "NotificationID,TypeOfNotification,ResourceId,ActionLink,NotificationTitle,NotificationMessage,PushDateTime")] Notification notification,
                                                List <string> TechTagNames, List <string> AudienceTypeNames, string NotificationType)

        {
            if (ModelState.IsValid)
            {
                notification.PushDateTime       = DateTime.UtcNow;
                notification.TypeOfNotification = (NotificationType)Enum.Parse(typeof(NotificationType), NotificationType, true);
                db.Notifications.Add(notification);

                notification.TechTags         = new List <PrimaryTechnology>();
                notification.AudienceTypeTags = new List <AudienceType>();
                notification.SentToUsers      = new List <AppUserNotificationAction>();

                if (TechTagNames != null)
                {
                    foreach (var techTag in TechTagNames)
                    {
                        //primary technology from client.
                        var primaryTech = db.PrimaryTechnologies.FirstOrDefault(x => x.PrimaryTech == techTag);
                        if (primaryTech != null)
                        {
                            notification.TechTags.Add(primaryTech);
                        }

                        var appUsers = db.AppUsers.Where(p => p.TechnologyTags.Any(c => primaryTech.PrimaryTechnologyID == c.PrimaryTechnologyID));
                        foreach (var user in appUsers)
                        {
                            //add app users to this notification.
                            if (notification.SentToUsers.FirstOrDefault(x => x.AppUserID == user.AppUserID) == null)
                            {
                                notification.SentToUsers.Add(
                                    new AppUserNotificationAction()
                                {
                                    AppUserID = user.AppUserID,
                                    Read      = false
                                });
                            }
                        }
                    }
                }

                if (AudienceTypeNames != null)
                {
                    //add audience type names.
                    foreach (var audTypeTag  in AudienceTypeNames)
                    {
                        var audType = db.AudienceTypes.FirstOrDefault(x => x.TypeOfAudience == audTypeTag);
                        if (audType != null)
                        {
                            notification.AudienceTypeTags.Add(audType);
                        }

                        var appUsers = db.AppUsers.Where(p => p.AudienceOrg.AudienceTypeID == audType.AudienceTypeID);
                        foreach (var user in appUsers)
                        {
                            //no app user exist to send this notification.
                            if (notification.SentToUsers.FirstOrDefault(x => x.AppUserID == user.AppUserID) == null)
                            {
                                notification.SentToUsers.Add(
                                    new AppUserNotificationAction()
                                {
                                    AppUserID = user.AppUserID,
                                    Read      = false
                                });
                            }
                        }
                    }
                }
                await db.SaveChangesAsync();

                if (TechTagNames != null)
                {
                    if (AudienceTypeNames != null)
                    {
                        await NotificationsHubHelper.SendNotificationAsync(TechTagNames.Union(AudienceTypeNames).Distinct().ToList().Select(x => x.Replace(" ", "_")).ToList(), notification);
                    }
                }

                return(RedirectToAction("Index"));
            }

            return(View(notification));
        }