• 在. 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)

  • 相关阅读:
    C# IL语法
    设计模式学习笔记(1)之单例模式
    设计模式学习笔记(3)之策略设计模式(Strategy)
    ORACLE与SQL SERVER语法区别
    浅谈测试驱动开发(TDD)
    面向对象的5条基本设计原则
    干法读后感磨练灵魂 提升心志
    Linux系统管理之硬盘管理
    Linux硬件信息采集
    Linux iptables
  • 原文地址:https://www.cnblogs.com/shixudong/p/3606003.html
Copyright © 2020-2023  润新知