Esempio n. 1
0
        public static bool pushMessage(string deviceID, ApplePushPayLoad aps, string certificatePath)
        {
            try
            {
                int port = 2195;
                //String hostname = "gateway.sandbox.push.apple.com";

                String hostname            = "gateway.push.apple.com";
                var    certificatePassword = ConfigurationManager.AppSettings["AppleCertificatePassword"];
                //    var certificatePassword = System.Configuration.ConfigurationManager.AppSettings["AppleCertificatePassword"].ToString();
                X509Certificate2           clientCertificate      = new X509Certificate2(System.IO.File.ReadAllBytes(certificatePath), ConfigurationManager.AppSettings["AppleCertificatePassword"]);
                X509Certificate2Collection certificatesCollection = new X509Certificate2Collection(clientCertificate);

                TcpClient client    = new TcpClient(hostname, port);
                SslStream sslStream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);

                sslStream.AuthenticateAsClient(hostname, certificatesCollection, SslProtocols.Default, false);
                MemoryStream memoryStream = new MemoryStream();
                BinaryWriter writer       = new BinaryWriter(memoryStream);
                writer.Write((byte)0);
                writer.Write((byte)0);
                writer.Write((byte)32);

                writer.Write(HexStringToByteArray(deviceID.ToUpper()));
                String payload = JsonConvert.SerializeObject(aps);
                writer.Write((byte)0);
                writer.Write((byte)payload.Length);
                byte[] b1 = System.Text.Encoding.UTF8.GetBytes(payload);
                writer.Write(b1);
                writer.Flush();
                byte[] array = memoryStream.ToArray();
                sslStream.Write(array);
                sslStream.Flush();
                client.Close();
                return(true);
            }
            catch (Exception ex)
            {
                new EventsLog().Write("Error: " + ex.Message);
                ex.Log();
                //new Exception("Push failed:- \tDeviceId:" + toDeviceId + "\tUser: "******"\tMessage:" + Message).Log();
                //throw ex;
            }
            return(false);
        }
Esempio n. 2
0
        private void SendNotification()
        {
            new Exception("Process Started").Log();

            CRMStagingEntities n = new CRMStagingEntities();
            var response         = new List <EntityNotificationUsers>();
            var repository       = new RepositoryNotification();

            // Get All Notifications which have IspushSent Status true
            response = repository.GetAllUserNotifications();

            foreach (var item in response)
            {
                // Get Details of Notification and user for pushing
                var dataNotification = repository.GetNotificationById(item.NotificationId);

                if (dataNotification.Model == null)
                {
                    break;
                }

                long   NotificationId = dataNotification.Model.NotificationId;
                int    ToUserId       = dataNotification.Model.UserId;
                int    TargetId       = dataNotification.Model.TargetId;
                int    TargetType     = dataNotification.Model.TargetTypeId;
                string Message        = dataNotification.Model.Message;
                //string Subject=null;
                //if(TargetType==(int)NoteType.Task)

                //{
                //var TaskModel = repository.GetTaskById(TargetId);
                //Subject = TaskModel.Model.Subject;
                //}
                //get Notification UnReadCount

                var unReadNotify = repository.GetUnReadNotificationCount(item.UserId);

                int unReadCount = unReadNotify.Model != null ? unReadNotify.Model.NotificationCount : 0;
                //Payload Format
                var notifyObj = new ApplePushPayLoad
                {
                    aps = new aps
                    {
                        alert = Message,
                        badge = unReadCount == 0 ? 1 : unReadCount,
                        sound = "default"
                    },
                    NotificationId = NotificationId,
                    TargetId       = TargetId,
                    TargetType     = TargetType
                };


                //  For Getting User DeviceId Details
                var repositoryUsers = new RepositoryUsers();
                var userDetails     = repositoryUsers.GetUserDeviceIdById(item.UserId);
                if (userDetails.Model != null)
                {
                    var toDeviceId = userDetails.Model.DeviceId;

                    if (toDeviceId == null)
                    {
                        continue;
                    }

                    // Push Message
                    string appPath    = Path.GetDirectoryName(Application.ExecutablePath);
                    string stringPath = Path.Combine(appPath, "AppleCertificate", "Certificates.p12");
                    new Exception("certificate path:" + stringPath).Log();
                    //NotificationHelper.pushMessage(toDeviceId, notifyObj, stringPath);

                    bool pushStatus = NotificationHelper.pushMessage(toDeviceId, notifyObj, stringPath);
                    if (pushStatus == true)
                    {
                        // Update Notification IsPushStatus
                        var updatePushStatus = repository.UpdateNotificationPushStatus(NotificationId, ToUserId);
                        new EventsLog().Write("Push success");
                        new Exception("Push success:- \tDeviceId:" + toDeviceId + "\tUser: "******"\tMessage:" + Message).Log();
                    }
                    else
                    {
                        new Exception("Push failed:- \tDeviceId:" + toDeviceId + "\tUser: "******"\tMessage:" + Message).Log();
                    }
                }
                // var updatePushStatus = repository.UpdateNotificationPushStatus(NotificationId, ToUserId);
            }
        }