public void SendIOSPushNotification(List <UserDevice> devices, AdminNotifications AdminNotification = null, Notification OtherNotification = null, int Type = 0) { try { // Configuration (NOTE: .pfx can also be used here) if (devices.Count() == 0) //it means there is no device no need to run the below code { return; } if (ApnsConfig != null) { IosPush pushModel = new IosPush(); foreach (var device in devices.Where(x => x.IsActive)) { try { if (AdminNotification != null) { pushModel.aps.alert.title = AdminNotification.Title; pushModel.aps.alert.body = AdminNotification.Description; pushModel.notification.NotificationId = device.User.Notifications.FirstOrDefault(x => x.AdminNotification_Id == AdminNotification.Id).Id; pushModel.notification.Type = (int)PushNotificationType.Announcement; } else { pushModel.aps.alert.title = OtherNotification.Title; pushModel.aps.alert.body = OtherNotification.Text; pushModel.notification.NotificationId = OtherNotification.Id; pushModel.notification.Type = Type; } ApnsServiceBroker apnsBroker; if (device.ApplicationType == UserDevice.ApplicationTypes.Enterprise) { if (device.EnvironmentType == UserDevice.ApnsEnvironmentTypes.Production) { apnsBroker = new ApnsServiceBroker(Enterprise.IOS.ProductionConfig); } else // Sandbox/Development { apnsBroker = new ApnsServiceBroker(Enterprise.IOS.SandboxConfig); } } else //PlayStore { if (device.EnvironmentType == UserDevice.ApnsEnvironmentTypes.Production) { apnsBroker = new ApnsServiceBroker(PlayStore.IOS.ProductionConfig); } else // Sandbox/Development { apnsBroker = new ApnsServiceBroker(device.iOSPushConfiguration = PlayStore.IOS.SandboxConfig); } } apnsBroker.OnNotificationFailed += IOSPushNotificationFailed; apnsBroker.OnNotificationSucceeded += IOSNotificationSuccess; // Start the broker apnsBroker.Start(); apnsBroker.QueueNotification(new ApnsNotification { DeviceToken = device.AuthToken, Payload = JObject.Parse(Newtonsoft.Json.JsonConvert.SerializeObject(pushModel)) }); apnsBroker.Stop(); apnsBroker.OnNotificationFailed -= IOSPushNotificationFailed; apnsBroker.OnNotificationSucceeded -= IOSNotificationSuccess; } catch (Exception ex) { Utility.LogError(ex); } } } } catch (Exception ex) { Utility.LogError(ex); } }
//public void SendIOSPushNotification(List<UserDevice> devices, AdminNotifications AdminNotification = null, Notification OtherNotification = null, int EntityType = 0, int EntityId = 0) //{ // string serverKey = GCMWebAPIKey; // var result = "-1"; // var notificationid = 0; // try // { // var webAddr = "https://fcm.googleapis.com/fcm/send"; // foreach (var device in devices.Where(x => x.IsActive)) // { // var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr); // httpWebRequest.ContentType = "application/json"; // httpWebRequest.Headers.Add(HttpRequestHeader.Authorization, "key=" + serverKey); // httpWebRequest.Method = "POST"; // NotificationMessage pushModel = new NotificationMessage(); // if (AdminNotification != null) // { // pushModel.notification.title = AdminNotification.Title; // pushModel.notification.text= AdminNotification.Description; // //pushModel.data.contentavailable = 1; // } // else // { // pushModel.notification.title = OtherNotification.Title; // pushModel.notification.text = OtherNotification.Text; // //pushModel.notification.NotificationId = OtherNotification.Id; // //pushModel.notification.DeliveryMan_Id = OtherNotification.DeliveryMan_ID; // // pushModel.notification.EntityType = EntityType; // // pushModel.notification.EntityId = OtherNotification.EntityId; // //pushModel.aps.contentavailable = 1; // } // using (StreamWriter streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) // { // //var dta = new DynamicValuesModel // //{ // // entityid = pushModel.notification.EntityId, // // entitytype = pushModel.notification.EntityType, // // notificationid = pushModel.notification.NotificationId, // // isread = true // //}; // var messageInformation = new NotificationMessage(); // //string[] authTokens = new string[1]; // //authTokens[0]=new string // string[] authTokens = { device.AuthToken }; // messageInformation.registration_ids = authTokens; // // messageInformation.data = dta; // //Object to JSON STRUCTURE => using Newtonsoft.Json; // string jsonMessage = JsonConvert.SerializeObject(messageInformation); // streamWriter.Write(jsonMessage); // streamWriter.Flush(); // } // var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); // using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) // { // result = streamReader.ReadToEnd(); // if (result.Contains("success") && result.Contains("failure")) // { // dynamic token = JObject.Parse(result); // string success = token.success.ToString(); // //return success == "1" ? true : false; // } // else // { // } // } // } // // return result; // } // catch (Exception ex) // { // Utility.LogError(ex); // } //} public void SendIOSPushNotification(List <UserDevice> devices, AdminNotifications AdminNotification = null, Notification OtherNotification = null, int EntityType = 0, int EntityId = 0) { string serverKey = GCMWebAPIKey; var result = "-1"; var notificationid = 0; try { var webAddr = "https://fcm.googleapis.com/fcm/send"; foreach (var device in devices.Where(x => x.IsActive)) { var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr); httpWebRequest.ContentType = "application/json"; httpWebRequest.Headers.Add(HttpRequestHeader.Authorization, "key=" + serverKey); httpWebRequest.Method = "POST"; IosPush pushModel = new IosPush(); if (AdminNotification != null) { pushModel.aps.alert.title = AdminNotification.Title; pushModel.aps.alert.body = AdminNotification.Description; //using (RiscoContext ctx=new RiscoContext()) //{ // ctx.Notifications.Count(); //} //if (device.User != null) // pushModel.notification.NotificationId = device.User.SendingUserNotifications.FirstOrDefault(x => x.AdminNotification_Id == AdminNotification.Id).Id; //else //pushModel.notification.NotificationId = device.DeliveryMan.Notifications.FirstOrDefault(x => x.AdminNotification_Id == AdminNotification.Id).Id; pushModel.notification.EntityType = (int)PushNotificationType.Announcement; //pushModel.notification.EntityId = OtherNotification.Entity_ID.Value; pushModel.aps.contentavailable = 1; } else { pushModel.aps.alert.title = OtherNotification.Title; pushModel.aps.alert.body = OtherNotification.Text; pushModel.notification.NotificationId = OtherNotification.Id; //pushModel.notification.DeliveryMan_Id = OtherNotification.DeliveryMan_ID; pushModel.notification.EntityType = EntityType; pushModel.notification.EntityId = 1; pushModel.aps.contentavailable = 1; } using (StreamWriter streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) { var dta = new DynamicValuesModel { entityid = pushModel.notification.EntityId, entitytype = pushModel.notification.EntityType, notificationid = pushModel.notification.NotificationId, isread = true }; var messageInformation = new NotificationMessage(); //string[] authTokens = new string[1]; //authTokens[0]=new string string[] authTokens = { device.AuthToken }; messageInformation.notification = new SendNotification() { title = pushModel.aps.alert.title, text = pushModel.aps.alert.body }; messageInformation.registration_ids = authTokens; messageInformation.data = dta; //Object to JSON STRUCTURE => using Newtonsoft.Json; string jsonMessage = JsonConvert.SerializeObject(messageInformation); //string json = ""; //json = "{\"to\": \"" + device.AuthToken + "\",\"notification\": {\"body\": \"" + pushModel.aps.alert.body + "\",\"title\": \"" + pushModel.aps.alert.title + "\",\"notificationid\": \"" + pushModel.notification.NotificationId + "\",\"entitytype\": \"" + pushModel.notification.EntityType + "\",\"entityid\": \"" + pushModel.notification.EntityId + "\",\"isread\": \"" + true + "\",}}"; //json = "{\"to\": \"" + device.AuthToken + "\",\"notification\": {\"text\": \"" + pushModel.aps.alert.body + "\",\"title\": \"" + pushModel.aps.alert.title + "\",\"notificationid\": \"" + notificationid + "\",\"isread\": \"" + true + "\",},\"data\":{\"entitytype\": \"" + pushModel.notification.EntityType + "\",\"entityid\": \"" + pushModel.notification.EntityId + "\",}}"; streamWriter.Write(jsonMessage); streamWriter.Flush(); } var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) { result = streamReader.ReadToEnd(); if (result.Contains("success") && result.Contains("failure")) { dynamic token = JObject.Parse(result); string success = token.success.ToString(); //return success == "1" ? true : false; } else { } } } // return result; } catch (Exception ex) { } }
//public async Task SendIosPushNotification(List<UserDevice> devices, string Text) //{ // string serverKey = GCMApiKey; // var result = "-1"; // var notificationid = 0; // try // { // var webAddr = "https://fcm.googleapis.com/fcm/send"; // foreach (var device in devices) // { // var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr); // httpWebRequest.ContentType = "application/json"; // httpWebRequest.Headers.Add(HttpRequestHeader.Authorization, "key=" + serverKey); // //httpWebRequest.Headers.Add("Authorization:key=" + serverKey); // httpWebRequest.Method = "POST"; // using (StreamWriter streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) // { // string json = ""; // json = "{\"to\": \"" + device.AuthToken + "\",\"data\": {\"message\": \"" + Text + "\",\"title\": \"" + Text + "\",\"notificationid\": \"" + notificationid + "\",\"isread\": \"" + true + "\",}}"; // streamWriter.Write(json); // streamWriter.Flush(); // } // var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); // using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) // { // result = streamReader.ReadToEnd(); // if (result.Contains("success") && result.Contains("failure")) // { // dynamic token = JObject.Parse(result); // string success = token.success.ToString(); // //return success == "1" ? true : false; // } // else // { // } // } // } // // return result; // } // catch (Exception ex) // { // } //} //public void SendIOSPushNotification(List<UserDevice> devices, AdminNotifications AdminNotification = null, Notification OtherNotification = null, int Type = 0) public static async Task SendIOSPushNotification(List <string> devices, bool isIOS, NotificationModel OtherNotification = null, int Type = 0) { string serverKey = ServerKeyDriver; var result = "-1"; var notificationid = 0; try { var webAddr = "https://fcm.googleapis.com/fcm/send"; foreach (var device in devices) { var httpWebRequest = (HttpWebRequest)WebRequest.Create(webAddr); httpWebRequest.ContentType = "application/json"; httpWebRequest.Headers.Add(HttpRequestHeader.Authorization, "key=" + serverKey); httpWebRequest.Headers.Add(string.Format("Sender: id={0}", SenderId)); //httpWebRequest.Headers.Add("Authorization:key=" + serverKey); httpWebRequest.Method = "POST"; IosPush pushModel = new IosPush(); pushModel.aps.alert.title = OtherNotification.Title; pushModel.aps.alert.body = OtherNotification.Text; pushModel.notification.Type = Type; pushModel.aps.contentavailable = 1; using (StreamWriter streamWriter = new StreamWriter(httpWebRequest.GetRequestStream())) { string json = ""; if (isIOS) { json = "{\"to\": \"" + devices + "\",\"notification\": {\"body\": \"" + pushModel.aps.alert.body + "\",\"title\": \"" + pushModel.aps.alert.title + "\",\"notificationid\": \"" + pushModel.notification.NotificationId + "\",\"orderid\": \"" + pushModel.notification.EntityId + "\",\"isread\": \"" + true + "\",}}"; } else { json = "{\"to\": \"" + devices + "\",\"data\": {\"body\": \"" + pushModel.aps.alert.body + "\",\"title\": \"" + pushModel.aps.alert.title + "\",\"notificationid\": \"" + pushModel.notification.NotificationId + "\",\"orderid\": \"" + pushModel.notification.EntityId + "\",\"isread\": \"" + true + "\",}}"; } //json = "{\"to\": \"" + device.AuthToken + "\",\"notification\": {\"body\": \"" + Text + "\",\"title\": \"" + Text + "\",}}"; streamWriter.Write(json); streamWriter.Flush(); } var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) { result = streamReader.ReadToEnd(); if (result.Contains("success") && result.Contains("failure")) { dynamic token = JObject.Parse(result); string success = token.success.ToString(); //return success == "1" ? true : false; } else { } } } // return result; } catch (Exception ex) { } }