c#关于iOS推送

本文介绍了使用C#进行iOS推送时遇到的问题,包括Notification Failed to be Queued!错误的解决方法,以及如何避免频繁连接APNs被禁。通过修改源码JdSoft.Apns.Notifications.dll,将SslProtocols.Ssl3更改为SslProtocols.Default。同时,文章展示了如何利用单例模式确保安全的连接管理。在源码分析部分,详细阐述了Notification.cs和NotificationService.cs中处理推送的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这个网上已经有比较好的技术了,可以下载别人的代码,稍微改动即可。
我用的是JdSoft.Apns.Notifications.dll 源码已上传
关于遇见的问题
1、Notification Failed to be Queued!
这个问题是由于源码中System.Security.Authentication.SslProtocols.Ssl3造成的
将其修改为System.Security.Authentication.SslProtocols.Default即可
如果找不到在哪,就全局搜索下
(或者可以看看这个文件中有没 NotificationChannel.cs)
2、关于频繁链接apns是否会被禁掉,这个的应该是会会禁掉的,但是此源码中已经有封装了
但是为了保险又用单例做了一遍`

class NotificationServiceManager
{
private static NotificationService service = null;
private static object objLock = new object();
private bool Sandbox;
private string FileName;
private string FilePwd;
public NotificationServiceManager(bool sandbox, string token, string fileName, string filePwd)
{
Sandbox = sandbox;
FileName = fileName;
FilePwd = filePwd;
}

    public NotificationService Service
    {
        get
        {
            if (service == null)
            {
                lock (objLock)
                {
                    if (service != null && service.Connections > 0)
                        return service;
                    service = CreateService();
                    return service;
                }
            }
            return service;
        }
    }


    private NotificationService CreateService()
    {
        service = new NotificationService(Sandbox, FileName, FilePwd, 1);

        service.SendRetries = 5; //5 retries before generating notificationfailed event
        service.ReconnectDelay = 1000; //5 seconds

        service.Error += new NotificationService.OnError(service_Error);
        service.NotificationTooLong += new NotificationService.OnNotificationTooLong(service_NotificationTooLong);

        service.BadDeviceToken += new NotificationService.OnBadDeviceToken(service_BadDeviceToken);
        service.NotificationFailed += new NotificationService.OnNotificationFailed(service_NotificationFailed);
        service.NotificationSuccess += new NotificationService.OnNotificationSuccess(service_NotificationSuccess);
        service.Connecting += new NotificationService.OnConnecting(service_Connecting);
        service.Connected += new NotificationService.OnConnected(service_Connected);
        service.Disconnected += new NotificationService.OnDisconnected(service_Disconnected);
        return service;
    }
}`

关于源码,我调试了一下是这样走的
1、Notification.cs有参构造函数,其中对于NotificationPayload、NotificationAlert进行相关处理,
之后 是 NotificationService.cs 中QueueNotification() 经过层层调用到
这是一个过程,加入到了线程队列
2、创建NotificationService对象时用到
public NotificationService( string host, int port, byte[] p12FileBytes, string p12FilePassword, int connections )
{
this.SendRetries = 1;
closing = false;
disposing = false;
Host = host;
Port = port;
P12FileBytes = p12FileBytes;
P12FilePassword = p12FilePassword;
DistributionType = NotificationServiceDistributionType.Sequential;
Connections = connections;
}
在Connections属性中创建NotificationConnection对象,创建Connection对象时,创建了NotificationChannel对证书什么的验证,并且执行了start(),在start()中进行了线程的执行workerMethod(),
apnsChannel.EnsureConnection();
apnsChannel.Send(notification);
apnsStream.Write(notification.ToBytes());

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值