Esempio n. 1
0
        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);
            }
        }
Esempio n. 2
0
        public static void PushAppleNotification(string DeviceRegisteredID, string message, string mta, int notification_type, string title, int devtype = 2)
        {
            try
            {
                //system_config sc = dataContext.system_config.FirstOrDefault();

                // var payload1 = new NotificationPayload("Device token","Message",Badge,"Sound");
                var    payload1     = new NotificationPayload(DeviceRegisteredID, message, "1", 1, "default", title);
                string APNS_P12_KEY = "pushiStoneRBS.p12";
                string APNS_P12_PWD = "P@ssword1";

                payload1.AddCustom("flag", notification_type);
                payload1.AddCustom("mta", mta);

                //string path = "C:\\Users\\Programmer\\Desktop\\Moon-APNS-master\\Libraries";
                //string path = "D:\\DEV_PROJECT\\SKLIEW_LOCAL\\MOBILE_REWARDS\\MOBILE_REWARDS_SERVER\\MOBILE_REWARDS_SERVER.Lib.BizLogic\\bin";
                //string path = "C:\\Project\\PSSB_PROJECT\\MOBILE_REWARDS\\bin";
                string path2 = (System.Reflection.Assembly.GetExecutingAssembly().CodeBase).Replace("file:///", "");
                path2 = path2.Replace("/", "\\");
                path2 = path2.Substring(0, path2.LastIndexOf("bin") + 3);

                var p = new List <NotificationPayload> {
                    payload1
                };
                string responseMessage = string.Empty;

                if (devtype == 1)                                                                                     //use development cert
                {
                    var push     = new PushAppleNotification(true, path2 + "\\" + APNS_P12_KEY, APNS_P12_PWD, false); //local
                    var rejected = push.SendToApple(p);
                    foreach (var item in rejected)
                    {
                        responseMessage += (item + " | ");
                    }
                }
                else // use production
                {
                    var push     = new PushAppleNotification(false, path2 + "\\" + APNS_P12_KEY, APNS_P12_PWD, true); //production
                    var rejected = push.SendToApple(p);
                    foreach (var item in rejected)
                    {
                        responseMessage += (item + " | ");
                    }
                }
            }
            catch (Exception ex)
            {
                //ErrorLog.LogErrorDb("Error", System.Reflection.MethodBase.GetCurrentMethod().Name.ToString(),System.Threading.Thread.CurrentThread.ManagedThreadId.ToString(), ex.ToString());
            }
        }