使用PushSharp推送通知 - 基础知识

Kon*_*Kon 14 .net c# push-notification apple-push-notifications pushsharp

我需要将通知推送到我的应用安装的数万个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);
Run Code Online (Sandbox Code Playgroud)

问题#1:这很好用,我看到iPhone上的通知弹出.但是,由于它被称为推送服务,我认为它的行为就像一个服务 - 意思是,我实例化它并在Windows服务中调用_push.StartApplePushService().我想实际排队我的通知,我可以在前端做这个(管理员应用程序,让我们说):

        PushService push = new PushService();

        AppleNotification notification = NotificationFactory.Apple()
                                                            .ForDeviceToken(deviceToken)
                                                            .WithAlert(message)
                                                            .WithSound("default")
                                                            .WithBadge(badge);
        push.QueueNotification(notification);
Run Code Online (Sandbox Code Playgroud)

显然(就像我已经说过的那样),它没有用 - 最后一行不断抛出空引用异常.

我很难找到任何其他类型的文档来展示如何以服务/客户端方式设置它(而不是一次调用所有内容).是否有可能或者我错过了应该如何利用PushSharp的观点?

问题2:此外,我似乎无法找到一种方法同时定位多个设备令牌,而无需循环遍历它们并一次排队一个通知.这是唯一的方法还是我在这里错过了什么?

提前致谢.

bar*_*use 3

根据我所读到的以及我如何使用它,“服务”关键字可能会误导您......

它是一种服务,只需配置一次即可启动它。从此时起,它将等待您在其队列系统内推送新通知,并且一旦发生某些情况(传送报告、传送错误...),它将引发事件。它是异步的,您可以推送(=队列)10000 个通知,并使用事件处理程序等待结果返回。

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

在我的项目中,我创建了一个小型自托管 Web 服务(依赖于 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);
}
Run Code Online (Sandbox Code Playgroud)