Ejemplo n.º 1
0
        public void PushMessage(PushMessage pushMessage)
        {
            List <PushMessage> pushMessageList = new List <Model.PushMessage>();

            pushMessageList.Add(pushMessage);

            LogHelper.Info("_pushMessageList:  " + pushMessageList.Count);

            if (pushMessageList != null)
            {
                for (int i = 0; i < pushMessageList.Count; i++)
                {
                    PushMessage _pushMessage = pushMessageList[i];

                    if (_pushMessage.Token.Length == 64)
                    {
                        try
                        {
                            var notification = new ANPSNotification(_pushMessage.Token);
                            notification.Payload.Alert.Body = _pushMessage.Body;
                            //notification.Payload.Sound = "default";
                            if (!string.IsNullOrEmpty(_pushMessage.CustomItem))
                            {
                                var custom = JObject.Parse(_pushMessage.CustomItem);
                                notification.Payload.AddCustom("page", custom);
                            }
                            notification.Tag = _pushMessage;

                            var _result = _notificationService.QueueNotification(notification);
                            if (_result)
                            {
                                int _timeOut = 1;
                                if (i % 100 == 0)
                                {
                                    _timeOut = 2;
                                }
                                System.Threading.Thread.Sleep(_timeOut);
                            }
                            else
                            {
                                LogHelper.Info("消息[" + notification.DeviceToken + "]Push至IOS服务器的时候失败");
                            }
                        }
                        catch (Exception ex)
                        {
                            LogHelper.Info(ex.ToString());
                        }
                    }
                }
                //Stop and wait for the queues to drains
                if (_notificationService != null)
                {
                    pushMessageList.Clear();
                    _notificationService.Close();
                    _notificationService.Dispose();
                }
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// 创建一个消息
 /// </summary>
 /// <param name="pDeviceToken">设备令牌</param>
 /// <param name="pBody">消息内容</param>
 /// <returns></returns>
 public static JdSoft.Apple.Apns.Notifications.Notification CreateNotification(string pDeviceToken, string pBody)
 {
     JdSoft.Apple.Apns.Notifications.Notification notification = new JdSoft.Apple.Apns.Notifications.Notification();
     notification.DeviceToken        = pDeviceToken;
     notification.Payload.Alert.Body = pBody;
     notification.Payload.Sound      = "default";
     notification.Payload.Badge      = 1;
     return(notification);
 }
Ejemplo n.º 3
0
 /// <summary>
 /// 创建一个消息
 /// </summary>
 /// <param name="pDeviceToken">设备令牌</param>
 /// <param name="pBody">消息内容</param>
 /// <param name="pCustomers">附带数据</param>
 /// <returns></returns>
 public static JdSoft.Apple.Apns.Notifications.Notification CreateNotification(string pDeviceToken, string pBody, Dictionary <string, object> pCustomers)
 {
     JdSoft.Apple.Apns.Notifications.Notification notification = new JdSoft.Apple.Apns.Notifications.Notification();
     notification.DeviceToken        = pDeviceToken;
     notification.Payload.Alert.Body = pBody;
     notification.Payload.Sound      = "default";
     notification.Payload.Badge      = 1;
     foreach (var item in pCustomers)
     {
         notification.Payload.AddCustom(item.Key, item.Value);
     }
     return(notification);
 }
Ejemplo n.º 4
0
        public void AcceptFriendRequest(string deviceToken, int requestedBy, int acceptedBy)
        {
            //create the notification object
            var notification = new Notification(deviceToken);

            notification.Payload.Sound = "default";
            notification.Payload.Badge = 1;

            notification.Payload.AddCustom("Type", NotificationType.AcceptFriend.ToString());
            notification.Payload.AddCustom("RequestedBy", requestedBy);
            notification.Payload.AddCustom("ReceivedBy", acceptedBy);

            //queue the notification for notification
            notificationService.QueueNotification(notification);
        }
Ejemplo n.º 5
0
 public bool SendNotification(JdSoft.Apple.Apns.Notifications.Notification pNotification)
 {
     if (service.QueueNotification(pNotification))
     {
         Loggers.Debug(new DebugLogInfo()
         {
             Message = "成功发送至推送服务器:" + pNotification.ToString()
         });
         return(true);
     }
     else
     {
         Loggers.Debug(new DebugLogInfo()
         {
             Message = "发送到服务失败:" + pNotification.ToString()
         });
         return(false);
     }
 }
Ejemplo n.º 6
0
        protected virtual void Service_NotificationSuccess(object sender, ANPSNotification notification)
        {
            PushMessage _pushMessage = (PushMessage)notification.Tag;

            LogHelper.Info(notification.DeviceToken + "[" + _pushMessage.MessageID + "] {" + _p12FileName.PadRight(25) + " 密码:" + _p12FilePassword + "} success...");
        }
Ejemplo n.º 7
0
		private bool queueRandom(Notification notification)
		{
			int index = rand.Next(0, notificationConnections.Count - 1);

			if (notificationConnections[index] != null)
				return notificationConnections[index].QueueNotification(notification);

			return false;
		}
Ejemplo n.º 8
0
		void newCon_NotificationFailed(object sender, Notification failed)
		{
			if (this.NotificationFailed != null)
				this.NotificationFailed(sender, failed);
		}
Ejemplo n.º 9
0
		/// <summary>
		/// Queues a Notification to one of the Apns Connections using the DistributionType specified by the property.
		/// </summary>
		/// <param name="notification">Notification object to send</param>
		/// <returns>If true, the Notification has been successfully queued</returns>
		public bool QueueNotification(Notification notification)
		{
			bool queued = false;

			if (!disposing && !closing)
			{
				int tries = 0;
				
				while (tries < SendRetries && !queued)
				{
					if (DistributionType == NotificationServiceDistributionType.Sequential)
						queued = queueSequential(notification);
					else if (DistributionType == NotificationServiceDistributionType.Random)
						queued = queueRandom(notification);

					tries++;
				}
			}

			return queued;
		}
Ejemplo n.º 10
0
        public static void service_NotificationFailed(object sender, Notification notification)
        {
            //Console.WriteLine(string.Format("Notification Failed: {0}", notification.ToString()));

            _source.TraceEvent(TraceEventType.Critical, 3, string.Format("Notification Failed: {0} TraceEvent ", notification.ToString()));
            //_source.TraceInformation(string.Format("Notification Failed: {0}", notification.ToString()));
            _source.Flush();
        }
Ejemplo n.º 11
0
 void newCon_NotificationFailed(object sender, Notification failed)
 {
     var onNotificationFailed = NotificationFailed;
     if (onNotificationFailed != null)
         NotificationFailed(sender, failed);
 }
Ejemplo n.º 12
0
 static void service_NotificationSuccess(object sender, Notification notification)
 {
     Console.WriteLine(string.Format("Notification Success: {0}", notification.ToString()));
 }
Ejemplo n.º 13
0
 public void SendNotifications(Notification[] notifications)
 {
     NotificationBatch batch = new NotificationBatch(this, notifications);
     bool complete = false;
     while(!complete)
         complete = batch.SendMessages();
     batch.Complete();
 }
		public NotificationDeliveryError(DeliveryErrorType type, Notification notification)
		{
			this.ErrorType = type;
			this.Notification = notification;
		}
Ejemplo n.º 15
0
 void newCon_NotificationSuccess(object sender, Notification notification)
 {
     var onNotificationSuccess = NotificationSuccess;
             if (onNotificationSuccess != null)
                 try { onNotificationSuccess(sender, notification); } catch {}	// Ignore all exceptions from callback function
 }
Ejemplo n.º 16
0
 void newCon_NotificationFailed(object sender, Notification failed)
 {
     var onNotificationFailed = NotificationFailed;
             if (onNotificationFailed != null)
                 try { onNotificationFailed(sender, failed); } catch {}	// Ignore all exceptions from callback function
 }
Ejemplo n.º 17
0
        public static void SendMobileNotification(string UDID, string Message)
        {
            var notificationService = new NotificationService(true, HttpContext.Current.Server.MapPath(".") + "/ios_sandbox.p12", "sedona7289", 1);

            var notification = new Notification(UDID);
            notification.Payload.Sound = "default";
            notification.Payload.Alert.Body = Message;

            notificationService.QueueNotification(notification);

            notificationService.Close();
            notificationService.Error += notificationService_Error;
            notificationService.Dispose();

        }
Ejemplo n.º 18
0
		public static void Main(string[] args)
		{
			//Empty aps, just custom
			Notification notification = new Notification();
			notification.Payload.AddCustom("bar", 42);

			Console.WriteLine(notification.ToString());
			Console.WriteLine();
			System.Windows.Forms.Clipboard.SetText(notification.ToString());
			Console.ReadLine();


						
			//More complex notification
			Notification notification1 = new Notification();

			notification1.Payload.Alert.LocalizedKey = "GAME_PLAY_REQUEST_FORMAT";
			notification1.Payload.Alert.AddLocalizedArgs("Jenna", "Frank");

			notification1.Payload.Badge = 5;

			notification1.Payload.Sound = "chime";

			notification1.Payload.AddCustom("acme1", "bar");
			notification1.Payload.AddCustom("acme2", "bang", "whiz");


			Console.WriteLine(notification1.ToString());
			Console.WriteLine();
			System.Windows.Forms.Clipboard.SetText(notification1.ToString());
			Console.ReadLine();			

			
			
			////Simpler notification
			Notification notification2 = new Notification();

			notification2.Payload.Alert.Body = "Bob wants to play poker";
			notification2.Payload.Alert.ActionLocalizedKey = "PLAY";

			notification2.Payload.Badge = 5;

			notification2.Payload.Sound = "chime";

			notification2.Payload.AddCustom("acme1", "bar");
			notification2.Payload.AddCustom("acme2", "bang", "whiz");


			Console.WriteLine(notification2.ToString());
			Console.WriteLine();
			System.Windows.Forms.Clipboard.SetText(notification2.ToString());
			Console.ReadLine();
			
			
			////Very simple notification
			Notification notification3 = new Notification();

			notification3.Payload.Alert.Body = "Bob wants to play poker";

			notification3.Payload.Badge = 5;

			notification3.Payload.Sound = "chime";

			Console.WriteLine(notification3.ToString());
			Console.WriteLine();
			System.Windows.Forms.Clipboard.SetText(notification3.ToString());
			Console.ReadLine();
			
			
			////Badge update and sound only
			Notification notification4 = new Notification();

			notification4.Payload.Badge = 5;

			notification4.Payload.Sound = "chime";
			notification4.Payload.AddCustom("test", 4, 2, 12);

			Console.WriteLine(notification4.ToString());
			System.Windows.Forms.Clipboard.SetText(notification4.ToString());
			
			
			Console.WriteLine("Press enter to exit...");
			Console.ReadLine();
		}
Ejemplo n.º 19
0
 /// <summary>
 /// Send a notification to a connected channel immediately.  Must call EnsureConnection() before starting to send.
 /// </summary>
 /// <param name="notification">The Notification to send.</param>
 public void Send(Notification notification)
 {
     apnsStream.Write(notification.ToBytes());
 }
Ejemplo n.º 20
0
        protected virtual void Service_NotificationFailed(object sender, ANPSNotification notification)
        {
            PushMessage _pushMessage = (PushMessage)notification.Tag;

            LogHelper.Info(notification.DeviceToken + "[" + _pushMessage.MessageID + "] failed...");
        }
Ejemplo n.º 21
0
 internal NotificationBatch(NotificationChannel channel, Notification[] notifications)
 {
     this.channel = channel;
     this.notifications = notifications;
     this.errors = new List<NotificationDeliveryError>();
     this.readBuffer = new byte[6];
     this.current = 0;
 }
Ejemplo n.º 22
0
		public void NewNotificationTest()
		{
			var n = new Notification("", new NotificationPayload("This is a test", 9, null));

			Assert.IsNotNull(n);
		}
Ejemplo n.º 23
0
        public static void SendNotification(string sDeviceToken, string sMessage)
        {
            //Variables you may need to edit:
            //---------------------------------

            //True if you are using sandbox certificate, or false if using production
            bool sandbox = true;

            //Put your device token in here
            //string testDeviceToken = "fe58fc8f527c363d1b775dca133e04bff24dc5032d08836992395cc56bfa62ef";
            string testDeviceToken = sDeviceToken;

            //Put your PKCS12 .p12 or .pfx filename here.
            // Assumes it is in the same directory as your app
            string p12File = "apn_developer_identity.p12";

            //This is the password that you protected your p12File
            //  If you did not use a password, set it as null or an empty string
            string p12FilePassword = "";

            //Actual Code starts below:
            //--------------------------------

            string p12Filename = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, p12File);

            NotificationService service = new NotificationService(sandbox, p12Filename, p12FilePassword, 1);

            service.SendRetries = 5; //5 retries before generating notificationfailed event
            service.ReconnectDelay = 5000; //5 seconds

            service.Error += new NotificationService.OnError(service_Error);
            service.NotificationTooLong += new NotificationService.OnNotificationTooLong(service_NotificationTooLong);

            service.BadDeviceToken += new NotificationService.OnBadDeviceToken(service_BadDeviceToken);
            service.NotificationFailed += new NotificationService.OnNotificationFailed(service_NotificationFailed);
            service.NotificationSuccess += new NotificationService.OnNotificationSuccess(service_NotificationSuccess);
            service.Connecting += new NotificationService.OnConnecting(service_Connecting);
            service.Connected += new NotificationService.OnConnected(service_Connected);
            service.Disconnected += new NotificationService.OnDisconnected(service_Disconnected);

            //The notifications will be sent like this:

            //Create a new notification to send
            Notification alertNotification = new Notification(testDeviceToken);

            alertNotification.Payload.Alert.Body = sMessage;
            alertNotification.Payload.Sound = "default";
            alertNotification.Payload.Badge = 1;

            //Queue the notification to be sent
            if (service.QueueNotification(alertNotification))
                Console.WriteLine("Notification Queued!");
            else
                Console.WriteLine("Notification Failed to be Queued!");

            Console.WriteLine("Cleaning Up...");

            //First, close the service.
            //This ensures any queued notifications get sent befor the connections are closed
            service.Close();

            //Clean up
            service.Dispose();

            Console.WriteLine("Done!");
            Console.WriteLine("Press enter to exit...");
            Console.ReadLine();
        }
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="notification">Notification that caused the Exception</param>
 public NotificationLengthException(Notification notification, int maxPayloadSize)
     : base(string.Format("Notification Payload Length ({0}) Exceeds the maximum length of {1} characters", notification.Payload.ToJson().Length, maxPayloadSize))
 {
     this.Notification = notification;
 }
Ejemplo n.º 25
0
 void newCon_NotificationSuccess(object sender, Notification notification)
 {
     var onNotificationSuccess = NotificationSuccess;
     if (onNotificationSuccess != null)
         NotificationSuccess(sender, notification);
 }
Ejemplo n.º 26
0
        public void SendNotification(string deviceToken, long id, string content, NotificationType type)
        {
            var notification = new Notification(deviceToken);

            notification.Payload.Alert.Body = content;
            notification.Payload.Sound = "default";
            notification.Payload.Badge = 1;

            notification.Payload.AddCustom("Type", type.ToString());
            notification.Payload.AddCustom("Id", id);

            //queue the notification for notification
            notificationService.QueueNotification(notification);
        }
Ejemplo n.º 27
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="notification">Notification that caused the Exception</param>
 public NotificationLengthException(Notification notification)
     : base(string.Format("Notification Payload Length ({0}) Exceeds the maximum length of {1} characters", notification.Payload.ToJson().Length, Notification.MAX_PAYLOAD_SIZE))
 {
     this.Notification = notification;
 }
Ejemplo n.º 28
0
 static void service_NotificationSuccess(object sender, Notification notification)
 {
     log.Debug(string.Format("Notification Success: {0}", notification.ToString()));
 }
Ejemplo n.º 29
0
		void newCon_NotificationSuccess(object sender, Notification notification)
		{
			if (this.NotificationSuccess != null)
				this.NotificationSuccess(sender, notification);
		}
Ejemplo n.º 30
0
		static void Main(string[] args)
		{
			//Variables you may need to edit:
			//---------------------------------

			//True if you are using sandbox certificate, or false if using production
			bool sandbox = true;

			//Put your device token in here
            string testDeviceToken = "a10c54e8827052b77cd377db98ef739617032710b9378d95ccb847e39bf2562a";
			
			//Put your PKCS12 .p12 or .pfx filename here.
			// Assumes it is in the same directory as your app
            string p12File = "apns.p12";

			//This is the password that you protected your p12File 
			//  If you did not use a password, set it as null or an empty string
			string p12FilePassword = String.Empty;

			//Number of notifications to send
			int count = 1;

			//Number of milliseconds to wait in between sending notifications in the loop
			// This is just to demonstrate that the APNS connection stays alive between messages
			int sleepBetweenNotifications = 15000;
			

			//Actual Code starts below:
			//--------------------------------

			string p12Filename = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, p12File);
			
			NotificationService service = new NotificationService(sandbox, p12Filename, p12FilePassword, 1);

			service.SendRetries = 5; //5 retries before generating notificationfailed event
			service.ReconnectDelay = 5000; //5 seconds

			service.Error += new NotificationService.OnError(service_Error);
			service.NotificationTooLong += new NotificationService.OnNotificationTooLong(service_NotificationTooLong);

			service.BadDeviceToken += new NotificationService.OnBadDeviceToken(service_BadDeviceToken);
			service.NotificationFailed += new NotificationService.OnNotificationFailed(service_NotificationFailed);
			service.NotificationSuccess += new NotificationService.OnNotificationSuccess(service_NotificationSuccess);
			service.Connecting += new NotificationService.OnConnecting(service_Connecting);
			service.Connected += new NotificationService.OnConnected(service_Connected);
			service.Disconnected += new NotificationService.OnDisconnected(service_Disconnected);
			
			//The notifications will be sent like this:
			//		Testing: 1...
			//		Testing: 2...
			//		Testing: 3...
			// etc...
			for (int i = 1; i <= count; i++)
			{
				//Create a new notification to send
				Notification alertNotification = new Notification(testDeviceToken);
				
				alertNotification.Payload.Alert.Body = string.Format("Testing {0}...", i);
				alertNotification.Payload.Sound = "default";
				alertNotification.Payload.Badge = i;
								
				//Queue the notification to be sent
				if (service.QueueNotification(alertNotification))
					Console.WriteLine("Notification Queued!");
				else
					Console.WriteLine("Notification Failed to be Queued!");

				//Sleep in between each message
				if (i < count)
				{
					Console.WriteLine("Sleeping " + sleepBetweenNotifications + " milliseconds before next Notification...");
					System.Threading.Thread.Sleep(sleepBetweenNotifications);
				}
			}
			
			Console.WriteLine("Cleaning Up...");

			//First, close the service.  
			//This ensures any queued notifications get sent befor the connections are closed
			service.Close();

			//Clean up
			service.Dispose();

			Console.WriteLine("Done!");
			Console.WriteLine("Press enter to exit...");
			Console.ReadLine();
		}
Ejemplo n.º 31
0
		private bool queueSequential(Notification notification)
		{
			if (notificationConnections.Count <= sequential && sequential > 0)
				sequential = 0;

			if (notificationConnections[sequential] != null)
				return notificationConnections[sequential].QueueNotification(notification);
			
			return false;
		}
		public NotificationDeliveryError(Exception exception, Notification notification)
		{
			this.ErrorType = DeliveryErrorType.Unknown;
			this.Exception = exception;
			this.Notification = notification;
		}
Ejemplo n.º 33
0
		/// <summary>
		/// Queue's a Notification to be Sent as soon as possible in a First in First out pattern
		/// </summary>
		/// <param name="notification">Notification object to send</param>
		/// <returns>If true, the notification was queued successfully, otherwise it was not and will not be sent</returns>
		public bool QueueNotification(Notification notification)
		{
			if (!disposing && !closing && accepting)
			{
				notifications.Enqueue(notification);
				return true;
			}

			return false;
		}
Ejemplo n.º 34
0
        public void SendMessage(string deviceToken, Message message)
        {
            //create the notification object
            var notification = new Notification(deviceToken);

            notification.Payload.Alert.Body = message.Content;
            notification.Payload.Sound = "default";
            notification.Payload.Badge = 1;

            notification.Payload.AddCustom("Type", NotificationType.Message.ToString());
            notification.Payload.AddCustom("Id", message.Id);
            notification.Payload.AddCustom("Sender", message.SenderId);
            notification.Payload.AddCustom("Receiver", message.ReceiverId);
            notification.Payload.AddCustom("Content", message.Content);
            notification.Payload.AddCustom("CreateDate", message.CreateDate);

            //queue the notification for notification
            notificationService.QueueNotification(notification);
        }