推送通知与PushSharp -基础

本文关键字:基础 PushSharp 通知 | 更新日期: 2023-09-27 17:50:59

我需要向安装了我的应用的数以万计的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.StartApplePushService()。我想让通知排队,我可以在前端(比如admin app)做这个:

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

显然(就像我已经说过的),它没有工作-最后一行一直抛出空引用异常。

我很难找到任何其他类型的文档来展示如何以服务/客户端方式设置这个(而不仅仅是一次调用所有内容)。这是可能的还是我错过了如何使用PushSharp的要点?

问题# 2 :此外,我似乎找不到一种方法来一次瞄准许多设备令牌,而不是循环它们并一次排队通知一个。这是唯一的办法吗,还是我也错过了什么?

推送通知与PushSharp -基础

@baramuse解释了这一切,如果你想看到一个服务"处理器",你可以浏览我的解决方案https://github.com/vmandic/DevUG-PushSharp我已经实现了你所寻求的工作流,即一个赢的服务,赢的处理器,甚至是一个web api特设处理器使用相同的核心处理器。

从我所读到的和我如何使用它,'Service'关键字可能误导了你…

它是一个服务,你只需要配置一次就可以启动它。从这一点开始,它将等待你在它的队列系统中推送新的通知,一旦有事情发生(交付报告、交付错误……),它就会引发事件。它是异步的,您可以推送(=queue) 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);
}