private static byte[] GeneratePayload(NotificationPayload payload) { try { //convert Devide token to HEX value. byte[] deviceToken = new byte[payload.DeviceToken.Length / 2]; for (int i = 0; i < deviceToken.Length; i++) { deviceToken[i] = byte.Parse(payload.DeviceToken.Substring(i * 2, 2), System.Globalization.NumberStyles.HexNumber); } var memoryStream = new MemoryStream(); // Command memoryStream.WriteByte(1); // Changed command Type //Adding ID to Payload memoryStream.Write(Encoding.ASCII.GetBytes(payload.PayloadId.ToString()), 0, payload.PayloadId.ToString().Length); //Adding ExpiryDate to Payload int epoch = (int)(DateTime.UtcNow.AddMinutes(300) - new DateTime(1970, 1, 1)).TotalSeconds; byte[] timeStamp = BitConverter.GetBytes(epoch); memoryStream.Write(timeStamp, 0, timeStamp.Length); byte[] tokenLength = BitConverter.GetBytes((Int16)32); Array.Reverse(tokenLength); // device token length memoryStream.Write(tokenLength, 0, 2); // Token memoryStream.Write(deviceToken, 0, 32); // String length string apnMessage = payload.ToJson(); Logger.Info("Payload generated for " + payload.DeviceToken + " : " + apnMessage); byte[] apnMessageLength = BitConverter.GetBytes((Int16)apnMessage.Length); Array.Reverse(apnMessageLength); // message length memoryStream.Write(apnMessageLength, 0, 2); // Write the message memoryStream.Write(Encoding.ASCII.GetBytes(apnMessage), 0, apnMessage.Length); return(memoryStream.ToArray()); } catch (Exception ex) { Logger.Error("Unable to generate payload - " + ex.Message); return(null); } }
public static bool SendPushNotification(string ApplicationId, string UserMessage, string Flag, string JsonMessage, Dictionary <string, object> Dictionary, int BadgeCount = 1, bool IsSoundOn = true) { UserMessage = (IsSoundOn == false ? "" : UserMessage); string Sound = (IsSoundOn == false ? "" : "default"); var payload1 = new NotificationPayload(ApplicationId, UserMessage, BadgeCount, Sound, "1"); //payload1.AddCustom("flag", Flag); //payload1.AddCustom("message2", JsonMessage); //payload1.AddCustom("message1", Dictionary); payload1.AddCustom("flag", Flag); payload1.AddCustomJson("message2", Dictionary); payload1.AddCustom("message1", JsonMessage); var p = new List <NotificationPayload> { payload1 }; var FileName = ConfigurationManager.AppSettings["PushFileName"].ToString(); //Pick from Web Config var password = ConfigurationManager.AppSettings["PushPassword"].ToString(); //Pick From Web Config var P12File = System.Web.HttpContext.Current.Server.MapPath("/Infrastructure/PushNotificationFile/" + FileName); var push = new PushNotification(true, P12File, password); var rejected = push.SendToApple(p); var feedback = push.GetFeedBack(); if (feedback.Count() > 0) { return(false); } else { return(true); } //foreach (var item in rejected) //{ // Console.WriteLine(item); //} //Console.ReadLine(); }