3

我正在尝试使用 PushSharp 向各种设备发送通知。我的服务器端应用程序注册通知以发送到 MSSQL 中的表,以便另一个应用程序 (Bot) 处理这些通知并将它们发送到 Apple 服务器。

我正在使用以下代码:

static DataEntities Entities;

    static void Main(string[] args)
    {

        Entities = new DataEntities();

        var appleCert = File.ReadAllBytes(Path.GetFullPath("My_Push_Notifications.p12"));
        PushBroker broker = new PushBroker();
        broker.RegisterAppleService(new PushSharp.Apple.ApplePushChannelSettings(false, appleCert, "XXXXX"));

        broker.OnChannelCreated += broker_OnChannelCreated;
        broker.OnChannelDestroyed += broker_OnChannelDestroyed;
        broker.OnChannelException += broker_OnChannelException;
        broker.OnNotificationRequeue += broker_OnNotificationRequeue;
        broker.OnServiceException += broker_OnServiceException;
        broker.OnNotificationSent += broker_OnNotificationSent;
        broker.OnNotificationFailed += broker_OnNotificationFailed;

        while (true)
        {
            var pendingNotifications = Entities.Notifications.Include("UserDevice").Where(n => n.Status == (byte)Constants.Notifications.Status.Pending);

            if (pendingNotifications.ToList().Count > 0)
            {
                Console.WriteLine("Pending notifications: {0}", pendingNotifications.Count());
            }
            else
                Console.WriteLine("No pending notifications");

            foreach (var notification in pendingNotifications)
            {
                broker.QueueNotification<AppleNotification>(new AppleNotification()
                    .ForDeviceToken(notification.UserDevice.DeviceID)
                    .WithAlert(notification.Text)
                    .WithTag(notification.NotificationID));

                notification.Status = (byte)Constants.Notifications.Status.Sending;
            }

            Entities.SaveChanges();

            Thread.Sleep(2000);
        }
    }

如您所见,我将通知排队发送到 PushBroker,但从未调用任何事件,并且 iOS 设备什么也没有收到。我还尝试在循环结束之前使用“StopAllServices”,但没有任何变化。

这怎么可能?

谢谢你。

4

1 回答 1

9

我解决了这个问题。PushSharp 没有引发事件,因为您必须在代理上注册苹果服务之前添加事件处理程序。所以正确的代码是:

    PushBroker broker = new PushBroker();
    broker.OnChannelCreated += broker_OnChannelCreated;
    broker.OnChannelDestroyed += broker_OnChannelDestroyed;
    broker.OnChannelException += broker_OnChannelException;
    broker.OnNotificationRequeue += broker_OnNotificationRequeue;
    broker.OnServiceException += broker_OnServiceException;
    broker.OnNotificationSent += broker_OnNotificationSent;
    broker.OnNotificationFailed += broker_OnNotificationFailed;
    // Now you can register the service.
    broker.RegisterAppleService(new PushSharp.Apple.ApplePushChannelSettings(false, appleCert, "XXXXX"));
于 2014-02-06T11:48:18.777 回答