C# 使用PushSharp推送通知-基础

C# 使用PushSharp推送通知-基础,c#,.net,push-notification,apple-push-notifications,pushsharp,C#,.net,Push Notification,Apple Push Notifications,Pushsharp,我需要将通知推送到我的应用程序安装的数万台iOS设备上。我试着用PushSharp来做,但是我缺少一些基本的概念。起初,我试图在Windows服务中实际运行此功能,但无法使其正常工作-从_push.QueueNotification()调用中获取空引用错误。然后,我完全按照文档化的示例代码所做的做了,并且成功了: PushService _push = new PushService(); _push.Events.OnNotificationSendFailure += ne

我需要将通知推送到我的应用程序安装的数万台iOS设备上。我试着用PushSharp来做,但是我缺少一些基本的概念。起初,我试图在Windows服务中实际运行此功能,但无法使其正常工作-从_push.QueueNotification()调用中获取空引用错误。然后,我完全按照文档化的示例代码所做的做了,并且成功了:

    PushService _push = new PushService();

    _push.Events.OnNotificationSendFailure += new ChannelEvents.NotificationSendFailureDelegate(Events_OnNotificationSendFailure);
    _push.Events.OnNotificationSent += new ChannelEvents.NotificationSentDelegate(Events_OnNotificationSent);

    var cert = File.ReadAllBytes(HttpContext.Current.Server.MapPath("..pathtokeyfile.p12"));

    _push.StartApplePushService(new ApplePushChannelSettings(false, cert, "certpwd"));

    AppleNotification notification = NotificationFactory.Apple()
                                                        .ForDeviceToken(deviceToken)
                                                        .WithAlert(message)
                                                        .WithSound("default")
                                                        .WithBadge(badge);
    _push.QueueNotification(notification);

    _push.StopAllServices(true);
第1期: 这非常有效,我在iPhone上看到了弹出的通知。但是,由于它被称为推送服务,我假设它的行为类似于服务——也就是说,我实例化了它,并在Windows服务中调用了_Push.startaplepushservice()。我想,如果我真的要排队通知,我可以在前端(比如说,管理员应用程序)上这样做:

显然(正如我已经说过的),它不起作用-最后一行一直抛出空引用异常

我很难找到任何其他类型的文档来展示如何以服务/客户机的方式进行设置(而不是一次调用所有内容)。是否有可能或者我是否忽略了如何使用PushSharp

第二期: 此外,我似乎找不到一种方法一次针对多个设备令牌,而不通过它们循环并一次一个地排队通知。这是唯一的办法还是我也错过了什么


提前感谢。

从我的阅读和使用情况来看,“服务”关键字可能误导了您

它是一种服务,您只需配置一次并启动它。 从这一点开始,它将等待您在其队列系统中推送新通知,并在发生事件时立即引发事件(传递报告、传递错误…)。它是异步的,您可以使用事件处理程序推送(=队列)10000个通知并等待稍后返回结果

但它仍然是一个常规对象实例,您必须作为常规对象创建和访问它。它不公开任何“外部侦听器”(例如http/tcp/ipc连接),您必须构建它

在我的项目中,我创建了一个小型自托管webservice(依赖于ServiceStack),它负责配置和实例生存期,同时只公开SendNotification功能

关于问题#2,确实没有任何“批处理队列”,但当队列函数立即返回(排队并稍后推送)时,只需循环到设备令牌列表中

public void QueueNotification(Notification notification)
{
    if (this.cancelTokenSource.IsCancellationRequested)
    {
        Events.RaiseChannelException(new ObjectDisposedException("Service", "Service has already been signaled to stop"), this.Platform, notification);
        return;
    }

    notification.EnqueuedTimestamp = DateTime.UtcNow;

    queuedNotifications.Enqueue(notification);
}

详细说明,如果您希望看到服务“处理器”,您可以浏览我的解决方案,了解我在哪里实现了您寻求的工作流,即win服务,win处理器,甚至是使用同一核心处理器的web api即席处理器。

我在
NotificationFactory
PushService
下不断得到一条红线,我是否遗漏了要包含的内容?您使用的是哪个PushSharp版本?>
public void QueueNotification(Notification notification)
{
    if (this.cancelTokenSource.IsCancellationRequested)
    {
        Events.RaiseChannelException(new ObjectDisposedException("Service", "Service has already been signaled to stop"), this.Platform, notification);
        return;
    }

    notification.EnqueuedTimestamp = DateTime.UtcNow;

    queuedNotifications.Enqueue(notification);
}