使用异步PushSharp Web服务发送通知并从try-catch获取对用户的响应

时间:2014-11-29 13:15:05

标签: c# asp.net .net asynchronous pushsharp

我有一个Web Service用于向iOS服务器发送通知。输出方法在XmlDocument块中返回try - catch。当发送失败时,我在catch块中捕获它并返回XML错误消息,如果它没有失败,那么在try结束时我正在返回XML消息(它是同步的)。

现在我将其更改为使用PushSharp - (异步)方法,在这里我可以发送消息,任何情况下(它不会抛出错误)并在回调中处理结果我注册了。回调是异步的。

我的问题是我想给用户一个选项来发送消息并在消息失败或发送时得到响应,但在调用PushSharp方法后我继续在try块中需要一些东西给返回,但回调是在2-4秒后开始的。

如何回复回调结果?

var push = new PushBroker();
//Wire up the events for all the services that the broker registers
push.OnNotificationSent += NotificationSent;
push.OnChannelException += ChannelException;
push.OnServiceException += ServiceException;
push.OnNotificationFailed += NotificationFailed;
push.OnDeviceSubscriptionExpired += DeviceSubscriptionExpired;
push.OnDeviceSubscriptionChanged += DeviceSubscriptionChanged;
push.OnChannelCreated += ChannelCreated;
push.OnChannelDestroyed += ChannelDestroyed;

 try
    {
        var appleCert = File.ReadAllBytes(Server.MapPath("Resources/key.p12"));

        push.RegisterAppleService(new ApplePushChannelSettings(true, appleCert, "password")); 
        push.QueueNotification(new AppleNotification()
                                    .ForDeviceToken(row.deviceid)
                                    .WithAlert(txtNotification.Text)
                                    .WithBadge(1)
                                    );


           // if comes here so return XML of message sent - OLD version
    }
    catch (Exception ex)
    {
        throw ex;
     //  return XML with Error
    }

已注册的活动,从这里我想回复一个回复:

static void DeviceSubscriptionChanged(object sender, 
        string oldSubscriptionId, string newSubscriptionId, INotification notification)
        {
        }


        static void NotificationSent(object sender, INotification notification)
        {
        }
        static void NotificationFailed(object sender, 
        INotification notification, Exception notificationFailureException)
        {
        }

        static void ChannelException
            (object sender, IPushChannel channel, Exception exception)
        {
        }

        static void ServiceException(object sender, Exception exception)
        {
        }


        static void DeviceSubscriptionExpired(object sender, 
        string expiredDeviceSubscriptionId, 
            DateTime timestamp, INotification notification)
        {
        }

        static void ChannelDestroyed(object sender)
        {
        }

        static void ChannelCreated(object sender, IPushChannel pushChannel)
        {
        }

0 个答案:

没有答案