.net平台推送ios消息

1,ios应用程序中允许向客户推送消息

2,需要有苹果的证书以及密码(怎么获取,网上搜一下,需要交费的)

3,iphone手机一部,安装了该ios应用程序

4,.net 项目中引用PushSharp.Apple.dll,PushSharp.Core.dll(这两个文件在网上搜一下,有源码的)

5,开始写代码,定义全局的对象PushBroker pusher = new PushBroker();

6,注册方法

复制代码
protected void startApp()
        {
                pusher.RegisterAppleService(new ApplePushChannelSettings(File.ReadAllBytes(CertificatePath), CertificatePassword));
                pusher.OnDeviceSubscriptionChanged += pusher_OnDeviceSubscriptionChanged;
                pusher.OnDeviceSubscriptionExpired += pusher_OnDeviceSubscriptionExpired;
                pusher.OnNotificationSent += pusher_OnNotificationSent;
                pusher.OnNotificationFailed += pusher_OnNotificationFailed;

        }
static void pusher_OnNotificationFailed(object sender, INotification notification, Exception error)
        {
            var n = (AppleNotification)notification;
            //error.Message ...获取推送出错的信息
            Log.Error("推送出错的信息", error);
        }

        static void pusher_OnNotificationSent(object sender, INotification notification)
        {
            //消息推送成功后
            var n = (AppleNotification)notification;
            //n.Payload.Alert.Body  获取推送的消息内容...
            Log.Error("推送内容"+n.Payload.Alert.Body);
        }

        static void pusher_OnDeviceSubscriptionExpired(object sender, string expiredSubscriptionId, DateTime expirationDateUtc, INotification notification)
        {
            // 从数据库删除过期的expiredSubscriptionId
        }

        static void pusher_OnDeviceSubscriptionChanged(object sender, string oldSubscriptionId, string newSubscriptionId, INotification notification)
        {
            // 把数据库中的oldSubscriptionId更新为newSubscriptionId
        }
复制代码

startApp()方法中有两个参数,

CertificatePath:证书的路径
CertificatePassword:密码

7,推送代码

pusher.QueueNotification(new AppleNotification().ForDeviceToken(TokenID) 
.WithAlert("推送的内容").WithBadge(1).WithSound("default"));// 从数据库或者其他等地方获取设备的TokenID,每个iphone一个TokenID

8,准备好这些以后就可以测试,本人亲自测试通过,如果有什么不明白的地方留言沟通,希望对大家有帮助!

9,如果想在Android设备上推送,项目要引进PushSharp.Android.dll,代码的话后期为大家更新,敬请关注你!

10,源码地址:http://pan.baidu.com/s/1pJoD8IR

posted @   shixudong  阅读(5271)  评论(12编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示