1,新建項目,在項目中添加引用,dll文件已經上傳在百度網盤,點擊下載

2,引入命名空間
using PushSharp;using PushSharp.Android;using PushSharp.Apple;using PushSharp.Core;using PushSharp.Windows;using PushSharp.WindowsPhone;
3,初始化寫入下面代碼
var push = new PushBroker(); //Wire up the events for all the services that the broker registers push.OnNotificationSent += NotificationSent; push.OnChannelException += ChannelException; push.OnServiceException += ServiceException; push.OnNotificationFailed += NotificationFailed; push.OnDeviceSubscriptionExpired += DeviceSubscriptionExpired; push.OnDeviceSubscriptionChanged += DeviceSubscriptionChanged; push.OnChannelCreated += ChannelCreated; push.OnChannelDestroyed += ChannelDestroyed; //------------------------- // 蘋果推送 //------------------------- //Configure and start Apple APNS // IMPORTANT: 獲取正確的證書 var appleCert = File.ReadAllBytes(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../../Resources/PushSharp.Apns.Sandbox.p12")); //IMPORTANT: 正確的密碼 push.RegisterAppleService(new ApplePushChannelSettings(appleCert, "CERTIFICATE PASSWord HERE")); //Extension method //開始推送 push.QueueNotification(new AppleNotification() .ForDeviceToken("設備的TokenID") .WithAlert("Hello World!") .WithBadge(7) .WithSound("sound.caf")); //--------------------------- // ANDROID GCM 推送 //--------------------------- //Configure and start Android GCM //IMPORTANT: 正確的Google API's key push.RegisterGcmService(new GcmPushChannelSettings("YOUR Google API's Console API access API KEY for Server Apps HERE")); //IMPORTANT: 手機設備注冊號 push.QueueNotification(new GcmNotification().ForDeviceRegistrationId("手機設備注冊號") .WithJson("{/"alert/":/"Hello World!/",/"badge/":7,/"sound/":/"sound.caf/"}")); //----------------------------- // WINDOWS PHONE 推送 //----------------------------- //Configure and start Windows Phone Notifications push.RegisterWindowsPhoneService(); push.QueueNotification(new WindowsPhoneToastNotification() .ForEndpointUri(new Uri("設備注冊CHANNEL URI")) .ForOSVersion(WindowsPhoneDeviceOSVersion.MangoSevenPointFive) .WithBatchingInterval(BatchingInterval.Immediate) .WithNavigatePath("/MainPage.xaml") .WithText1("PushSharp") .WithText2("This is a Toast")); Console.WriteLine("Waiting for Queue to Finish..."); //停止 push.StopAllServices(); Console.WriteLine("Queue Finished, PRess return to exit..."); Console.ReadLine();
4,實現注冊方法
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) { Console.WriteLine("Sent: " + sender + " -> " + 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); }5,大功告成,每一種系統都需要不同的驗證信息,有完整的信息在使用本例子(比如ios證書,密碼,設備tokenID等),親自測過,正常!
6,有什么不懂的地方和我聯系(shixudong3@yeah.net)
新聞熱點
疑難解答