在. net平台下,推送IOS,Android,Windows Phone消息

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)

posted @ 2014-03-17 19:50  shixudong  阅读(1593)  评论(6编辑  收藏  举报