【问题标题】:Push Notifications with PushSharp - the basics使用 PushSharp 推送通知 - 基础知识
【发布时间】:2012-12-22 21:49:18
【问题描述】:

我需要将通知推送到我的应用安装的数以万计的 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: 此外,我似乎无法找到一种方法来一次定位多个设备令牌,而无需遍历它们并一次将通知排队。这是唯一的方法还是我在这里也错过了什么?

提前致谢。

【问题讨论】:

  • 我在NotificationFactoryPushService 下不断收到一条红线,我是否缺少要包含的内容?
  • 您使用的是哪个 PushSharp 版本?>

标签: c# .net push-notification apple-push-notifications pushsharp


【解决方案1】:

@baramuse 解释了一切,如果您希望看到服务“处理器”,您可以在https://github.com/vmandic/DevUG-PushSharp 上浏览我的解决方案,我已经实现了您寻求的工作流程,即 win 服务、win 处理器甚至使用相同核心处理器的 web api ad hoc 处理器。

【讨论】:

    【解决方案2】:

    根据我的阅读内容和使用方式,“服务”关键字可能会误导您...

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多