c#关于iOS推送

来源:互联网 发布:记事本编程是什么语言 编辑:程序博客网 时间:2024/05/17 07:02

这个网上已经有比较好的技术了,可以下载别人的代码,稍微改动即可。
我用的是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());

1 0
原创粉丝点击