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

14

我需要向成千上万安装了我的应用的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()。而且我认为要真正排队我的通知,我可以在前端(管理应用程序,例如)上执行此操作:

        PushService push = new PushService();

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

显然(就像我之前说的),它没有起作用 - 最后一行始终抛出空引用异常。

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

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

提前感谢。


1
我一直在NotificationFactoryPushService下面看到红线,是不是我漏掉了什么需要包含的内容? - user2754681
你使用的是哪个PushSharp版本? - Lakshay Dulani
2个回答

4

@baramuse已经进行了完整的解释。如果您想查看一个服务“processor”,您可以浏览我的解决方案,地址为https://github.com/vmandic/DevUG-PushSharp,其中我已经实现了您需要的工作流程,即使用相同的核心处理器实现一个win服务、win处理器或者甚至是web api临时处理器。


3
根据我所阅读的和使用的经验,'Service'关键字可能会让你误解......它在某种程度上是一种服务,你只需要配置一次并启动它。从这个点开始,它将等待您将新的通知推入其队列系统,并在发生某些事件(传递报告、传递错误等)时立即引发事件。它是异步的,您可以推送(排队)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);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接