首页 > 技术文章 > IOS通过PushSharp开源框架发送推送

jasonzeng 2016-02-25 11:05 原文

1,首先生成推送证书:

openssl x509 -in aps_developer_identity.cer -inform DER -out aps_developer_identity.pem -outform PEM 

 

openssl pkcs12 -nocerts -out PushChat_Noenc.pem -in PushChatKey.p12

 (cat aps_developer_identity.pem PushChat_Noenc.pem > ck.pem)

openssl pkcs12 -export -in aps_developer_identity.pem -inkey PushChat_Noenc.pem -certfile PushChat.certSigningRequest -name "aps_developer_identity" -out aps_developer_identity.p12 (C#必须)

 

2.下载框架http://pan.baidu.com/s/1pJoD8IR或者https://github.com/Redth/PushSharp

3.调用方法

 #region
        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="deviceToken">设备令牌</param>
        /// <param name="message">消息内容</param>
        private void SendMessage(List<string> deviceToken, string message)
        {
            //创建推送对象
            var pusher = new PushBroker();
            pusher.OnNotificationSent += pusher_OnNotificationSent;//发送成功事件
            pusher.OnNotificationFailed += pusher_OnNotificationFailed;//发送失败事件
            pusher.OnChannelCreated += pusher_OnChannelCreated;
            pusher.OnChannelDestroyed += pusher_OnChannelDestroyed;
            pusher.OnChannelException += pusher_OnChannelException;
            pusher.OnDeviceSubscriptionChanged += pusher_OnDeviceSubscriptionChanged;
            pusher.OnDeviceSubscriptionExpired += pusher_OnDeviceSubscriptionExpired;
            pusher.OnNotificationRequeue += pusher_OnNotificationRequeue;
            pusher.OnServiceException += pusher_OnServiceException;
            //注册推送服务
            byte[] certificateData = File.ReadAllBytes(CertificatePath);
            pusher.RegisterAppleService(new ApplePushChannelSettings(false, certificateData, CertificatePassword));
            foreach (string token in deviceToken)
            {

                //给指定设备发送消息
                pusher.QueueNotification(new AppleNotification()
                    .ForDeviceToken(token)
                    .WithAlert(message)
                    .WithBadge(1)
                    .WithSound("default"));
            }
        }

        void pusher_OnServiceException(object sender, Exception error)
        {
            msg = error.ToString();
            
            Console.WriteLine("消息发送失败,错误详情:" + error.ToString());
        }

        void pusher_OnNotificationRequeue(object sender, PushSharp.Core.NotificationRequeueEventArgs e)
        {
            msg = "pusher_OnNotificationRequeue";
            
            Console.WriteLine("pusher_OnNotificationRequeue");
        }

        void pusher_OnDeviceSubscriptionExpired(object sender, string expiredSubscriptionId, DateTime expirationDateUtc, PushSharp.Core.INotification notification)
        {
            msg = "pusher_OnDeviceSubscriptionChanged";
           
            Console.WriteLine("pusher_OnDeviceSubscriptionChanged");
        }

        void pusher_OnDeviceSubscriptionChanged(object sender, string oldSubscriptionId, string newSubscriptionId, PushSharp.Core.INotification notification)
        {
            msg = "pusher_OnDeviceSubscriptionChanged";
            
            Console.WriteLine("pusher_OnDeviceSubscriptionChanged");
        }

        void pusher_OnChannelException(object sender, PushSharp.Core.IPushChannel pushChannel, Exception error)
        {
            msg = error.ToString();
            
            Console.WriteLine("消息发送失败,错误详情:" + error.ToString());
        }

        void pusher_OnChannelDestroyed(object sender)
        {
            msg = "pusher_OnChannelDestroyed";
            //JScript.alert("pusher_OnNotificationRequeue");
            Console.WriteLine("pusher_OnChannelDestroyed");
        }

        void pusher_OnChannelCreated(object sender, PushSharp.Core.IPushChannel pushChannel)
        {


            msg = "pusher_OnChannelCreated";
            Console.WriteLine("pusher_OnChannelCreated");
        }

        void pusher_OnNotificationFailed(object sender, PushSharp.Core.INotification notification, Exception error)
        {
            msg = error.ToString();
            
            Console.WriteLine("消息发送失败,错误详情:" + error.ToString());
        }

        void pusher_OnNotificationSent(object sender, PushSharp.Core.INotification notification)
        {
            msg = "消息发送成功";
            
            Console.WriteLine("消息发送成功!");
        }
        #endregion

 

4,我遇到的异常The maximum number of Send attempts to send the notification was reached!

可以修改

FeedbackService.cs和 ApplePushChannel.cs中的System.Security.Authentication.SslProtocols.Ssl3为System.Security.Authentication.SslProtocols.Tls两处即可

 

推荐阅读