博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
IOS通过PushSharp开源框架发送推送
阅读量:6546 次
发布时间:2019-06-24

本文共 4440 字,大约阅读时间需要 14 分钟。

1,首先生成推送证书:

openssl x509 -in aps_developer_identity.cer -inform DER -out aps_developer_identity.pem -outform PEM 

 

openssl pkcs12 -nocerts -out PushChat_Noenc.pem -in PushChatKey.p12

 (cat aps_developer_identity.pem PushChat_Noenc.pem > ck.pem)

openssl pkcs12 -export -in aps_developer_identity.pem -inkey PushChat_Noenc.pem -certfile PushChat.certSigningRequest -name "aps_developer_identity" -out aps_developer_identity.p12 (C#必须)

 

2.下载框架http://pan.baidu.com/s/1pJoD8IR或者https://github.com/Redth/PushSharp

3.调用方法

#region        ///         /// 发送消息        ///         /// 设备令牌        /// 消息内容        private void SendMessage(List
deviceToken, string message) { //创建推送对象 var pusher = new PushBroker(); pusher.OnNotificationSent += pusher_OnNotificationSent;//发送成功事件 pusher.OnNotificationFailed += pusher_OnNotificationFailed;//发送失败事件 pusher.OnChannelCreated += pusher_OnChannelCreated; pusher.OnChannelDestroyed += pusher_OnChannelDestroyed; pusher.OnChannelException += pusher_OnChannelException; pusher.OnDeviceSubscriptionChanged += pusher_OnDeviceSubscriptionChanged; pusher.OnDeviceSubscriptionExpired += pusher_OnDeviceSubscriptionExpired; pusher.OnNotificationRequeue += pusher_OnNotificationRequeue; pusher.OnServiceException += pusher_OnServiceException; //注册推送服务 byte[] certificateData = File.ReadAllBytes(CertificatePath); pusher.RegisterAppleService(new ApplePushChannelSettings(false, certificateData, CertificatePassword)); foreach (string token in deviceToken) { //给指定设备发送消息 pusher.QueueNotification(new AppleNotification() .ForDeviceToken(token) .WithAlert(message) .WithBadge(1) .WithSound("default")); } } void pusher_OnServiceException(object sender, Exception error) { msg = error.ToString(); Console.WriteLine("消息发送失败,错误详情:" + error.ToString()); } void pusher_OnNotificationRequeue(object sender, PushSharp.Core.NotificationRequeueEventArgs e) { msg = "pusher_OnNotificationRequeue"; Console.WriteLine("pusher_OnNotificationRequeue"); } void pusher_OnDeviceSubscriptionExpired(object sender, string expiredSubscriptionId, DateTime expirationDateUtc, PushSharp.Core.INotification notification) { msg = "pusher_OnDeviceSubscriptionChanged"; Console.WriteLine("pusher_OnDeviceSubscriptionChanged"); } void pusher_OnDeviceSubscriptionChanged(object sender, string oldSubscriptionId, string newSubscriptionId, PushSharp.Core.INotification notification) { msg = "pusher_OnDeviceSubscriptionChanged"; Console.WriteLine("pusher_OnDeviceSubscriptionChanged"); } void pusher_OnChannelException(object sender, PushSharp.Core.IPushChannel pushChannel, Exception error) { msg = error.ToString(); Console.WriteLine("消息发送失败,错误详情:" + error.ToString()); } void pusher_OnChannelDestroyed(object sender) { msg = "pusher_OnChannelDestroyed"; //JScript.alert("pusher_OnNotificationRequeue"); Console.WriteLine("pusher_OnChannelDestroyed"); } void pusher_OnChannelCreated(object sender, PushSharp.Core.IPushChannel pushChannel) { msg = "pusher_OnChannelCreated"; Console.WriteLine("pusher_OnChannelCreated"); } void pusher_OnNotificationFailed(object sender, PushSharp.Core.INotification notification, Exception error) { msg = error.ToString(); Console.WriteLine("消息发送失败,错误详情:" + error.ToString()); } void pusher_OnNotificationSent(object sender, PushSharp.Core.INotification notification) { msg = "消息发送成功"; Console.WriteLine("消息发送成功!"); } #endregion

 

4,我遇到的异常The maximum number of Send attempts to send the notification was reached!

可以修改

FeedbackService.cs和 ApplePushChannel.cs中的System.Security.Authentication.SslProtocols.Ssl3为System.Security.Authentication.SslProtocols.Tls两处即可

 

转载于:https://www.cnblogs.com/jasonzeng/p/5216245.html

你可能感兴趣的文章
数据的加密和解密
查看>>
使用 dell openmanage server administrator 管理 服务器硬盘
查看>>
克隆虚拟机后-没有eth0 网卡的问题
查看>>
线性表的顺序存储
查看>>
SaltStack学习(四)在SaltStack中选择目标主机
查看>>
php 简单的文件上传功能
查看>>
网络安全十大注意
查看>>
cisco虚拟局域网VLAN路由----待补充
查看>>
join命令实现文件内容拼接
查看>>
-bash:wget command not found的解决方法
查看>>
yara规则
查看>>
我的个人简历
查看>>
我的友情链接
查看>>
图片的copy,从一个目录复制到另一个目录
查看>>
我的友情链接
查看>>
KVM组件bug报告方法
查看>>
linux内核实现中的小工具
查看>>
xtrabackup备份、恢复
查看>>
jboss端口配置
查看>>
格式化输出数字字符串
查看>>