IOS通過PushSharp開源框架發送推送


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兩處即可

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM