示例#1
0
        public async Task <IActionResult> Post([FromBody] NotificationJson NotificationMsg)
        {
            Microsoft.Azure.NotificationHubs.NotificationOutcome outcome = null;
            HttpStatusCode ret = HttpStatusCode.InternalServerError;

            int count = PlacesController.inProgress.Where(x => x.Name == NotificationMsg.Message).Count();

            if (count == 0)
            {
                PlacesController.inProgress.Add(places.GetFiltereBar(NotificationMsg.Message));
            }



            Dictionary <string, string> templateParams = new Dictionary <string, string>();

            templateParams["messageParam"] = "Breaking News!";

            GcmNotification notification = new GcmNotification("{ \"data\":{\"message\":\"" + NotificationMsg.Message + "\"} }");

            outcome = await Notification.Notification.Instance.Hub.SendNotificationAsync(notification);



            if (outcome != null)
            {
                if (!((outcome.State == Microsoft.Azure.NotificationHubs.NotificationOutcomeState.Abandoned) ||
                      (outcome.State == Microsoft.Azure.NotificationHubs.NotificationOutcomeState.Unknown)))
                {
                    ret = HttpStatusCode.OK;
                }
            }

            return(Ok("OK"));
        }
示例#2
0
        public async Task <IHttpActionResult> Enviar(ChatViewModel messageInfo)
        {
            string[] userTag = new string[2];
            userTag[0] = "username:"******"from:" + messageInfo.From;

            Microsoft.Azure.NotificationHubs.NotificationOutcome outcome = null;
            HttpStatusCode ret = HttpStatusCode.InternalServerError;

            var notif = "{ \"data\" : {\"message\":\"" + "De " + messageInfo.From + ": " + messageInfo.Message + "\"}}";

            outcome = await _hubClient.SendGcmNativeNotificationAsync(notif, userTag);

            if (outcome != null)
            {
                if (!((outcome.State == Microsoft.Azure.NotificationHubs.NotificationOutcomeState.Abandoned) ||
                      (outcome.State == Microsoft.Azure.NotificationHubs.NotificationOutcomeState.Unknown)))
                {
                    ret = HttpStatusCode.OK;
                }
            }

            var result = new { sucess = true, message = ret == HttpStatusCode.OK ? "Mensagem enviada com sucesso." : "Ocorreu algum problema ao tentar enviar a mensagem." };

            return(Ok(result));
        }
        public async Task <HttpResponseMessage> Post(string pns, [FromBody] string message, string to_tag)
        {
            var user = User.Identity.Name;

            string[] userTag = new string[2];
            userTag[0] = to_tag;
            userTag[1] = "from:" + user;

            Microsoft.Azure.NotificationHubs.NotificationOutcome outcome = null;
            HttpStatusCode ret = HttpStatusCode.InternalServerError;

            switch (pns.ToLower())
            {
            case "wns":
                // Windows 8.1 / Windows Phone 8.1 / Windows 10
                var toast = @"<toast><visual><binding template=""ToastText01""><text id=""1"">" +
                            "From " + user + ": " + message + "</text></binding></visual></toast>";
                outcome = await Notifications.Instance.Hub.SendWindowsNativeNotificationAsync(toast, userTag[0]);

                break;

            case "wns/raw":
                Notification notification = new WindowsNotification(message);
                notification.Headers.Add("X-WNS-Type", "wns/raw");
                await Notifications.Instance.Hub2.SendNotificationAsync(notification, userTag[0]);

                break;

            case "apns":
                // iOS
                var alert = "{\"aps\":{\"alert\":\"" + "From " + user + ": " + message + "\"}}";
                outcome = await Notifications.Instance.Hub.SendAppleNativeNotificationAsync(alert, userTag);

                break;

            case "gcm":
                // Android
                var notif = "{ \"data\" : {\"message\":\"" + "From " + user + ": " + message + "\"}}";
                outcome = await Notifications.Instance.Hub.SendGcmNativeNotificationAsync(notif, userTag);

                break;
            }

            if (outcome != null)
            {
                if (!((outcome.State == Microsoft.Azure.NotificationHubs.NotificationOutcomeState.Abandoned) ||
                      (outcome.State == Microsoft.Azure.NotificationHubs.NotificationOutcomeState.Unknown)))
                {
                    ret = HttpStatusCode.OK;
                }
            }

            return(Request.CreateResponse(ret));
        }
示例#4
0
        public async Task <string> Post(string pns, [FromBody] string message, string to_tag, string informacao)
        {
            //var user = HttpContext.Current.User.Identity.Name;
            var user = "******";

            string[] userTag = new string[2];
            userTag[0] = "username:"******"from:" + user;

            Microsoft.Azure.NotificationHubs.NotificationOutcome outcome = null;
            HttpStatusCode ret = HttpStatusCode.InternalServerError;

            switch (pns.ToLower())
            {
            case "wns":
                // Windows 8.1 / Windows Phone 8.1
                var toast = @"<toast><visual><binding template=""ToastText01""><text id=""1"">" +
                            "From " + user + ": " + message + "</text></binding></visual></toast>";
                outcome = await Notifications.Instance.Hub.SendWindowsNativeNotificationAsync(toast, userTag);

                break;

            case "apns":
                // iOS
                var alert = "{\"aps\":{\"alert\":\"" + "From " + user + ": " + message + "\"}}";
                outcome = await Notifications.Instance.Hub.SendAppleNativeNotificationAsync(alert, userTag);

                break;

            case "gcm":
                // Android
                //user - Usuário que envia a solicitação.
                //message - Mensagem que aparecerá no corpo da notificação.
                //informacao - Dados que aparecerão na tela do usuário ao abrir a notificação.
                var notif = "{ \"data\" : {\"message\":\"" + "From " + user + ": " + message + "\", \"informacao\": \"" + informacao + "\"}}";

                outcome = await Notifications.Instance.Hub.SendGcmNativeNotificationAsync(notif, to_tag);

                break;
            }

            if (outcome != null)
            {
                if (!((outcome.State == Microsoft.Azure.NotificationHubs.NotificationOutcomeState.Abandoned) ||
                      (outcome.State == Microsoft.Azure.NotificationHubs.NotificationOutcomeState.Unknown)))
                {
                    ret = HttpStatusCode.OK;
                }
            }

            return("Notificação enviada com sucesso!");
        }
        /// <summary>
        /// Note: Although the Apple devicetoken is stored in pass-in, but at the time the devicetoken was registered through our Account/RegisterPhoneToken,
        /// Azure Notification Hub is associated userId to the DeviceToken.  So the UserId is the real identifier (not the APNS devicetoken), userId_[12125...] is the real key.
        /// </summary>
        /// <param name="pns"></param>
        /// <param name="userId"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task <NotificationOutcome> TokenRequest(NotificationHubPNS pns, string userId, TokenRequestModel model)
        {
            string[] userTag = new string[1];
            userTag[0] = "userId_" + userId.Trim();

            Microsoft.Azure.NotificationHubs.NotificationOutcome outcome = null;
            switch (pns)
            {
            case NotificationHubPNS.APNS:
                // iOS

                string serializeModel = TokenRequestModelAPNS(model);

                string logStr = string.Format("APNS TokenRequest SerializeModel: {0}", serializeModel);
                logger.Debug(logStr);

                int n = serializeModel.IndexOf('{');
                if (n >= 0)
                {
                    serializeModel = serializeModel.Remove(n, 1);
                }

                n = serializeModel.LastIndexOf('}');
                if (n > 0)
                {
                    serializeModel = serializeModel.Remove(n, 1);
                }

                string alert = "{";
                alert += "\"aps\":{\"alert\":\"" + "vid: " + model.vid + ", tid " + model.tid + ", merchant " + model.merchantIdentifier + "\", \"sound\":\"default\"},";
                alert += serializeModel;
                alert += "}";

                logStr = string.Format("APNS TokenRequest Alert: {0}", alert);
                logger.Debug(logStr);

                //outcome = await Hub.SendAppleNativeNotificationAsync(alert);

                if (!string.IsNullOrEmpty(userId))
                {
                    outcome = await Hub.SendAppleNativeNotificationAsync(alert, userTag);
                }
                else
                {
                    outcome = await Hub.SendAppleNativeNotificationAsync(alert);
                }
                break;
            }
            return(outcome);
        }
示例#6
0
        public async Task <HttpResponseMessage> TestNotification2([FromBody] object data)
        {
            string pns  = "gcm";
            var    user = HttpContext.Current.User.Identity.Name;

            string[] userTag = new string[2];
            userTag[0] = "email:" + "*****@*****.**";
            userTag[1] = "from:" + user;
            string message = "test";

            Microsoft.Azure.NotificationHubs.NotificationOutcome outcome = null;
            HttpStatusCode ret = HttpStatusCode.InternalServerError;

            switch (pns.ToLower())
            {
            case "wns":
                // Windows 8.1 / Windows Phone 8.1
                var toast = @"<toast><visual><binding template=""ToastText01""><text id=""1"">" +
                            "From " + user + ": " + message + "</text></binding></visual></toast>";
                outcome = await Notifications.Instance.Hub.SendWindowsNativeNotificationAsync(toast, userTag);

                break;

            case "apns":
                // iOS
                var alert = "{\"aps\":{\"alert\":\"" + "From " + user + ": " + message + "\"}}";
                outcome = await Notifications.Instance.Hub.SendAppleNativeNotificationAsync(alert, userTag);

                break;

            case "gcm":
                // Android
                var notif = "{ \"data\" : {\"message\":\"" + "From " + user + ": " + message + "\"}}";
                outcome = await Notifications.Instance.Hub.SendGcmNativeNotificationAsync(notif, userTag);

                break;
            }

            if (outcome != null)
            {
                if (!((outcome.State == Microsoft.Azure.NotificationHubs.NotificationOutcomeState.Abandoned) ||
                      (outcome.State == Microsoft.Azure.NotificationHubs.NotificationOutcomeState.Unknown)))
                {
                    ret = HttpStatusCode.OK;
                }
            }

            return(Request.CreateResponse(ret));
        }
        public async Task SendNotification(string toast, string[] tags)
        {
            //var user = HttpContext.Current.User.Identity.Name;

            Microsoft.Azure.NotificationHubs.NotificationOutcome outcome = null;
            HttpStatusCode ret = HttpStatusCode.InternalServerError;

            outcome = await hub.SendWindowsNativeNotificationAsync(toast, tags);

            if (outcome != null)
            {
                if (!((outcome.State == Microsoft.Azure.NotificationHubs.NotificationOutcomeState.Abandoned) ||
                      (outcome.State == Microsoft.Azure.NotificationHubs.NotificationOutcomeState.Unknown)))
                {
                    ret = HttpStatusCode.OK;
                }
            }
        }
示例#8
0
        private async Task SendNotification(string vehicleId, string message)
        {
            Microsoft.Azure.NotificationHubs.NotificationOutcome outcome = null;
            HttpStatusCode ret = HttpStatusCode.InternalServerError;

            // Android
            var notif = "{ \"data\" : {\"message\":\"" + "From " + vehicleId + ": " + message + "\"}}";

            outcome = await Notifications.Instance.Hub.SendGcmNativeNotificationAsync(notif);

            if (outcome != null)
            {
                if (!((outcome.State == Microsoft.Azure.NotificationHubs.NotificationOutcomeState.Abandoned) ||
                      (outcome.State == Microsoft.Azure.NotificationHubs.NotificationOutcomeState.Unknown)))
                {
                    ret = HttpStatusCode.OK;
                }
            }
        }
        private static async Task <bool> sendToNotificationHub(string pns, string message, string to_tag)
        {
            Microsoft.Azure.NotificationHubs.NotificationOutcome outcome = null;
            var notif = "{ \"data\" : {\"message\":\"" + message + "\"}}";

            outcome = await Notifications.Instance.Hub.SendGcmNativeNotificationAsync(notif);

            if (outcome != null)
            {
                if (!((outcome.State == Microsoft.Azure.NotificationHubs.NotificationOutcomeState.Abandoned) ||
                      (outcome.State == Microsoft.Azure.NotificationHubs.NotificationOutcomeState.Unknown)))
                {
                    return(true);
                }
            }
            return(false);
            //var notif = "{ \"data\" : {\"message\":\"" + message + "\"}}";
            //await Notifications.Instance.Hub.SendGcmNativeNotificationAsync(notif, to_tag);
        }
        public async Task <NotificationOutcome> SendMessage(string pns, string message, string fromUser, string toUser)
        {
            string[] userTag = new string[1];
            userTag[0] = "userId_" + toUser.Trim();

            Microsoft.Azure.NotificationHubs.NotificationOutcome outcome = null;


            switch (pns.ToLower())
            {
            case "wns":
                // Windows 8.1 / Windows Phone 8.1
                var toast = @"<toast><visual><binding template=""ToastText01""><text id=""1"">" +
                            "From " + fromUser + ": " + message + "</text></binding></visual></toast>";
                outcome = await Hub.SendWindowsNativeNotificationAsync(toast, userTag);

                break;

            case "apns":
                // iOS
                var alert = "{\"aps\":{\"alert\":\"" + "From " + fromUser + ": " + message + "\", \"sound\":\"default\"}}";
                if (!string.IsNullOrEmpty(toUser))
                {
                    outcome = await Hub.SendAppleNativeNotificationAsync(alert, userTag);
                }
                else
                {
                    outcome = await Hub.SendAppleNativeNotificationAsync(alert);
                }
                break;

            case "gcm":
                // Android
                var notif = "{ \"data\" : {\"message\":\"" + "From " + fromUser + ": " + message + "\"}}";
                outcome = await Hub.SendGcmNativeNotificationAsync(notif, userTag);

                break;
            }

            return(outcome);
        }
示例#11
0
        public async Task <IActionResult> send([FromBody] DeliveryReadyEvent deliverReady)
        {
            string[] userTag = new string[1];
            userTag[0] = "rider:" + deliverReady.ryderId;


            Microsoft.Azure.NotificationHubs.NotificationOutcome outcome = null;

            switch (deliverReady.pns.ToLower())
            {
            case "apns":
                // iOS
                var alert = "{\"aps\":{\"alert\":\"New Delivery is ready\"}}";
                outcome = await hub.SendAppleNativeNotificationAsync(alert, userTag);

                break;

            case "fcm":
                // Android
                var notif = "{ \"data\" : {\"message\":\"New Delivery is ready\",\"deliveryId\":\"" + deliverReady.deliveryId + "\"}}";
                outcome = await hub.SendFcmNativeNotificationAsync(notif, userTag);

                break;
            }

            if (outcome != null)
            {
                if (!((outcome.State == Microsoft.Azure.NotificationHubs.NotificationOutcomeState.Abandoned) ||
                      (outcome.State == Microsoft.Azure.NotificationHubs.NotificationOutcomeState.Unknown)))
                {
                    return(BadRequest());
                }
            }

            return(Ok());
        }
        // POST: api/Victim
        public async System.Threading.Tasks.Task <HttpResponseMessage> Post(string pns, [FromBody] Victim value, string to_tag)
        {
            var user = value.FBID;


            string[] userTag = new string[2];
            userTag[0] = "username:"******"T"
            FriendsPersistence fp      = new FriendsPersistence();
            ArrayList          friends = fp.getFriends(value.FBID);


            foreach (Friends f in friends)
            {
                switch (pns.ToLower())
                {
                case "wns":
                    // Windows 8.1 / Windows Phone 8.1
                    var toast = @"<toast><visual><binding template=""ToastText01""><text id=""1"">" +
                                "From " + user + ": " + "wns=message" + "</text></binding></visual></toast>";
                    outcome = await Notifications.Instance.Hub.SendWindowsNativeNotificationAsync(toast, to_tag);

                    break;

                case "apns":
                    // iOS
                    var alert = "{\"aps\":{\"alert\":\"" + "From " + user + ": " + "apns=message" + "\"}}";
                    outcome = await Notifications.Instance.Hub.SendAppleNativeNotificationAsync(alert, userTag);

                    break;

                case "gcm":
                    // Android
                    //value.UserName + " Need Help from you. The User ID =" + value.FBID
                    var notif = "{ \"data\" : {\"message\":\"" + "From " + value.UserName + " Need Help from you. The User ID =" + value.FBID + "\"}}";
                    outcome = await Notifications.Instance.Hub.SendGcmNativeNotificationAsync(notif, f.FriendFBID + "T");

                    break;
                }

                if (outcome != null)
                {
                    if (!((outcome.State == Microsoft.Azure.NotificationHubs.NotificationOutcomeState.Abandoned) ||
                          (outcome.State == Microsoft.Azure.NotificationHubs.NotificationOutcomeState.Unknown)))
                    {
                        ret = HttpStatusCode.OK;
                    }
                }
            }
            // return Request.CreateResponse(ret);
            /////////////////////////////////////////////////////////here for every friend-End



            VictimPersistence pp = new VictimPersistence();
            String            id;

            id = pp.saveUser(value);

            /*
             * // Get the settings for the server project.
             * HttpConfiguration config = this.Configuration;
             * MobileAppSettingsDictionary settings =
             *  this.Configuration.GetMobileAppSettingsProvider().GetMobileAppSettings();
             *
             * // Get the Notification Hubs credentials for the Mobile App.
             * string notificationHubName = settings.NotificationHubName;
             * string notificationHubConnection = settings
             *  .Connections[MobileAppSettingsKeys.NotificationHubConnectionString].ConnectionString;
             *
             * // Create a new Notification Hub client.
             * NotificationHubClient hub = NotificationHubClient
             * .CreateClientFromConnectionString(notificationHubConnection, notificationHubName);
             *
             * // Sending the message so that all template registrations that contain "messageParam"
             * // will receive the notifications. This includes APNS, GCM, WNS, and MPNS template registrations.
             * Dictionary<string, string> templateParams = new Dictionary<string, string>();
             * templateParams["messageParam"] = value.UserName + " Need Help from you. The User ID =" + value.FBID;
             *
             * try
             * {
             *  // Send the push notification and log the results.
             *  var result = await hub.SendTemplateNotificationAsync(templateParams);
             *
             *  // Write the success result to the logs.
             *  config.Services.GetTraceWriter().Info(result.State.ToString());
             * }
             * catch (System.Exception ex)
             * {
             *  // Write the failure result to the logs.
             *  config.Services.GetTraceWriter()
             *      .Error(ex.Message, null, "Push.SendAsync Error");
             * }
             */
            value.FBID = id;
            HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created);

            response.Headers.Location = new Uri(Request.RequestUri, String.Format("/user/{0}", id));
            return(response);
        }