Using async PushSharp web service to send notification and get response to user from try-catch

839 views Asked by At

I had an Web Service for sending notification to iOS servers. The output method return XmlDocument in try - catch block. when the sending was failed I was catching it in catch block and returns XML error message, if it was not failed so in the end of try I was returning XML message (it is synchronic).

Now I changed it to work with PushSharp - (an asynchronies) methods ,and here I can send the message in any case, (it don't throws error) and handle the result in callbacks that I registered. the callbacks are async.

My problem is that I want to give a user an option to send an message and get an response if the message failed or was sent, but after calling PushSharp method I continue in the try block and need something to return, but the callbacks are start after 2-4 second.

How can I response with an result from the callback?

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
    }

The registered events, from here I want to return a response :

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

There are 0 answers