3

PushSharp を使用して、さまざまなデバイスに通知を送信しようとしています。私のサーバー側アプリは、MSSQL のテーブルに送信する通知を登録し、別のアプリ (ボット) がそれらの通知を処理して 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

私はこれを解決しました。ブローカーに Apple サービスを登録する前にイベント ハンドラーを追加する必要があるため、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 に答える