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); } }
/// <summary> /// Posts to apns. /// </summary> /// <param name="apnsToken">The apns token.</param> /// <param name="message">The message.</param> /// <param name="extraDataAsCSV">The extra data as CSV.</param> /// <param name="result">The result.</param> private void PostToAPNS(string apnsToken, string message, string extraDataAsCSV, string result) { try { var p = new List <NotificationPayload>(); foreach (var token in apnsToken.Split(new string[] { ICConstant.PushNotTokenSeperator }, StringSplitOptions.None)) { if (!string.IsNullOrEmpty(token)) { var payload = new NotificationPayload(token, message, 1, "default"); payload.AddCustom("RegionID", "IDQ10150"); if (!string.IsNullOrEmpty(extraDataAsCSV)) { payload.AddCustom("Extra", extraDataAsCSV); } p.Add(payload); } } var push = new PushNotification(!isProduction, isProduction ? ICConstant.APNSCertificatePathForProd : ICConstant.APNSCertificatePathForDev, "1234"); var rejected = push.SendToApple(p); foreach (var item in rejected) { result += "Rejected " + item; } if (string.IsNullOrEmpty(result)) { Logging.DoLog(new LogEntry() { MessageDetails = "Success Sedning notification for" + apnsToken, LogCategory = LoggingCategory.Event, LogPriority = LogPriorityID.High, LogEventType = TraceEventType.Information, ClassName = MethodBase.GetCurrentMethod().DeclaringType.Name, MethodName = MethodBase.GetCurrentMethod().Name }); } else { Logging.DoLog(new LogEntry() { MessageDetails = "Error: Tokens " + apnsToken + " Reason: " + result, LogCategory = LoggingCategory.Error, LogPriority = LogPriorityID.High, LogEventType = TraceEventType.Error, ClassName = MethodBase.GetCurrentMethod().DeclaringType.Name, MethodName = MethodBase.GetCurrentMethod().Name }); } } catch (Exception exception) { Logging.DoLog(new LogEntry() { MessageDetails = exception.Message, LogCategory = LoggingCategory.Error, LogPriority = LogPriorityID.High, LogEventType = TraceEventType.Error, ClassName = MethodBase.GetCurrentMethod().DeclaringType.Name, MethodName = MethodBase.GetCurrentMethod().Name }); } }