最近作公司的一個項目.一旦數據庫插入新的消息,就要通知服務器,將這些新的消息推送給蘋果客戶端,之前咱們的項目中有人作過這個功能,無奈作的有點複雜,並且代碼沒註釋,我壓根就沒看懂.因此本身打算從新搞一個.git
小小研究了一個,找到PushSharp這個類庫,(超級強大),而後用了下感受很不錯,推薦給你們,爲你們介紹下.github
一.首先來了解下這幾個類庫數據庫
引用PushSharp 的幾個類庫
PushSharp.Core:核心庫必須引用
PushSharp.Apple:向蘋果推送的類庫
PushSharp.Android:C2DM及GCM,用於Android設備
PushSharp.Windows:用於Windows 8
PushSharp.WindowsPhone:用於WP設備
PushSharp.Amazon.Adm:用於Amazon的設備
PushSharp.Blackberry:用於黑莓設備
PushSharp.Google.Chrome:用於Chrome
這些類庫按照本身的項目要求使用便可服務器
二.簡單的示例代碼app
注意,若是你須要給蘋果推送 你首先要給你的APP去註冊一個證書,而後在你的APP中寫一些代碼,來接受推送.測試
//建立一個推送對象 private PushBroker push=new PushBroker(); //關聯推送狀態事件 push.OnNotificationSent += NotificationSent; push.OnChannelException += ChannelException; push.OnServiceException += ServiceException; push.OnNotificationFailed += NotificationFailed; push.OnDeviceSubscriptionExpired += DeviceSubscriptionExpired; push.OnDeviceSubscriptionChanged += DeviceSubscriptionChanged; push.OnChannelCreated += ChannelCreated; push.OnChannelDestroyed += ChannelDestroyed; var appleCert = File.ReadAllBytes("證書路徑");//將證書流式讀取 push.RegisterAppleService(new ApplePushChannelSettings(appleCert, "證書密碼"));//註冊推送通道 push.QueueNotification(new AppleNotification() .ForDeviceToken(messageList[i].DeviceToken)//手機token .WithAlert(messageList[i].Message)//推送消息內容 .WithBadge(7)//設備圖標顯示的未讀數(圖標右上角的小標誌) .WithSound("sound.caf"));//提示聲音 push.StopAllServices()//中止推送服務. #region=====推送狀態事件 static void DeviceSubscriptionChanged(object sender, string oldSubscriptionId, string newSubscriptionId, INotification notification) { //Currently this event will only ever happen for Android GCM //Console.WriteLine("Device Registration Changed: Old-> " + oldSubscriptionId + " New-> " + newSubscriptionId + " -> " + notification); } // 推送成功 static void NotificationSent(object sender, INotification notification) { } // 推送失敗 static void NotificationFailed(object sender, INotification notification, Exception notificationFailureException) { //Console.WriteLine("Failure: " + sender + " -> " + notificationFailureException.Message + " -> " + notification); } static void ChannelException(object sender, IPushChannel channel, Exception exception) { //Console.WriteLine("Channel Exception: " + sender + " -> " + exception); } static void ServiceException(object sender, Exception exception) { //Console.WriteLine("Channel Exception: " + sender + " -> " + exception); } static void DeviceSubscriptionExpired(object sender, string expiredDeviceSubscriptionId, DateTime timestamp, INotification notification) { //Console.WriteLine("Device Subscription Expired: " + sender + " -> " + expiredDeviceSubscriptionId); } static void ChannelDestroyed(object sender) { //Console.WriteLine("Channel Destroyed for: " + sender); } static void ChannelCreated(object sender, IPushChannel pushChannel) { //Console.WriteLine("Channel Created for: " + sender); } #endregion
三.解決多信息推送,而且推送到不一樣的設備上.我寫了一個類,來作這些事.如今也給你們看看網站
using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Text; using System.Threading.Tasks; using PushSharp.Apple; using PushSharp.Core; using PushSharp; using System.IO; using CYPInformationSystem.Model; using System.Threading; namespace CYPInformationSystem.PushMessage { /// <summary> /// 描述:蘋果客戶端推送類 /// 做者:茹化肖 /// 時間:2014年7月4日16:19:40 /// </summary> public class ApplePushService { private static ApplePushService applePushService; private static readonly object syncObject = new object(); private PushBroker push;//建立一個推送對象 private List<MessageModel> messageList;//消息實體隊列 private readonly string appleCertpath = AppDomain.CurrentDomain.BaseDirectory +ConfigurationManager.AppSettings["appleCertpath"]; private readonly string appleCertPwd = ConfigurationManager.AppSettings["appleCertPwd"];//密碼 private ApplePushService() { //確保該對象被實例化 this.push = new PushBroker(); this.messageList = new List<MessageModel>(); //關聯推送狀態事件 push.OnNotificationSent += NotificationSent; push.OnChannelException += ChannelException; push.OnServiceException += ServiceException; push.OnNotificationFailed += NotificationFailed; push.OnDeviceSubscriptionExpired += DeviceSubscriptionExpired; push.OnDeviceSubscriptionChanged += DeviceSubscriptionChanged; push.OnChannelCreated += ChannelCreated; push.OnChannelDestroyed += ChannelDestroyed; } #region=====公佈給外接調用的方法 /// <summary> /// 獲取對象實例 /// </summary> /// <returns></returns> public static ApplePushService GetInstance() { if (applePushService == null) { lock (syncObject) { if (applePushService == null) { applePushService = new ApplePushService(); applePushService.TherdStart(); } } } return applePushService; } /// <summary> /// 添加須要推送的消息 /// </summary> /// <param name="message">消息體</param> public void AddMessage(MessageModel message) { messageList.Add(message); } /// <summary> /// 推送消息 /// </summary> /// <param name="msg">消息體</param> /// <param name="token">用戶token</param> private void SendMessage() { try { var appleCert = File.ReadAllBytes(appleCertpath); if (appleCert.Length > 0 && appleCert != null)//證書對象不爲空 { push.RegisterAppleService(new ApplePushChannelSettings(appleCert, appleCertPwd)); while (true) { if (messageList.Count > 0)//若是 消息隊列中的消息不爲零 推送 { for (int i = 0; i < messageList.Count; i++) { push.QueueNotification(new AppleNotification() .ForDeviceToken(messageList[i].DeviceToken) .WithAlert(messageList[i].Message).WithBadge(7) .WithSound("sound.caf")); } messageList.Clear();//推送成功,清除隊列 } else { //隊列中沒有須要推送的消息,線程休眠5秒 Thread.Sleep(5000); } } } } catch (Exception e) { throw e; } } /// <summary> /// 啓動推送 /// </summary> private void TherdStart() { Thread td = new Thread(SendMessage); td.IsBackground = true; td.Start(); } #endregion #region=====推送狀態事件 static void DeviceSubscriptionChanged(object sender, string oldSubscriptionId, string newSubscriptionId, INotification notification) { //Currently this event will only ever happen for Android GCM //Console.WriteLine("Device Registration Changed: Old-> " + oldSubscriptionId + " New-> " + newSubscriptionId + " -> " + notification); } /// <summary> /// 推送成功 /// </summary> /// <param name="sender"></param> /// <param name="notification"></param> static void NotificationSent(object sender, INotification notification) { } /// <summary> /// 推送失敗 /// </summary> /// <param name="sender"></param> /// <param name="notification"></param> /// <param name="notificationFailureException"></param> static void NotificationFailed(object sender, INotification notification, Exception notificationFailureException) { //Console.WriteLine("Failure: " + sender + " -> " + notificationFailureException.Message + " -> " + notification); } static void ChannelException(object sender, IPushChannel channel, Exception exception) { //Console.WriteLine("Channel Exception: " + sender + " -> " + exception); } static void ServiceException(object sender, Exception exception) { //Console.WriteLine("Channel Exception: " + sender + " -> " + exception); } static void DeviceSubscriptionExpired(object sender, string expiredDeviceSubscriptionId, DateTime timestamp, INotification notification) { //Console.WriteLine("Device Subscription Expired: " + sender + " -> " + expiredDeviceSubscriptionId); } static void ChannelDestroyed(object sender) { //Console.WriteLine("Channel Destroyed for: " + sender); } static void ChannelCreated(object sender, IPushChannel pushChannel) { //Console.WriteLine("Channel Created for: " + sender); } #endregion } }
這裏用單例來保證,整個應用程序中只會有這一個推送對象,只有一個管道來推送.this
否則的話 你屢次來註冊這個管道會報錯的.spa
還遇到過 一個問題,.Newtonsoft.Json 這個程序集有時候報錯,說版本不對. 這個程序集是類序列化消息的.線程
個人解決辦法是,去獲取PushSharp 的源碼.官方網站:https://github.com/Redth/PushSharp
而後本身編譯一下,將生成的Dll文件 拿過來 引用到你的項目中.就可使用了 ,至於其餘問題 和別的平臺沒有去測試