0

My app uses an Azure Notification Hub to send messages to both Windows Phone and iOS devices. The problem is that they work if one is called, but it doesn't work if both are called.

For example, If I send a message to an iOS device from my iOS emulator, the following code works fine and the notification appears.

var toast = PrepareMpnsToastPayload("myapp", notificationText);
var appleToast = PrepareAppleToastPayload("myapp", notificationText);

//await NotificationHelper.Instance.Hub.SendMpnsNativeNotificationAsync(toast, userTag);
await NotificationHelper.Instance.Hub.SendAppleNativeNotificationAsync(appleToast, userTag);

However, if I uncomment the SendMpns..() line, the notification never gets to the apple device. When stepping through this code with remote debugging, the call to SendMpns..() doesn't seem to return. I step over it and it just resumes.. and the breakpoint on the SendApple..() call below it never gets hit.

This works the same with the Windows Phone Mpns line (works fine without the apple call).

How am I supposed to send an alert to a user when I don't know what device they are on? I just want to send the notification to all types. Any pointers would be greatly appreciated.

EDIT: The below answers of removing await() did work. However as I now have authenticated notification hub I need to capture the return value of the call so I can see if I need to get another auth token if it has expired!

creatiive
  • 1,073
  • 21
  • 49
  • Could you provide full listing for your sample including Main() function? – efimovandr Jan 05 '15 at 21:29
  • 1
    Have you tried removing the awaits? – Simon W Jan 05 '15 at 21:47
  • 1
    If remove awaits, then call Wait() on returned Task object. It is why I am asking about full listing. – efimovandr Jan 05 '15 at 22:34
  • Thanks guys this worked! If you set this as an answer I can accept it – creatiive Jan 06 '15 at 10:25
  • 1
    The actual issue sounds like it is an async threading issue with console apps. I suspect you are calling an async void method from your Main method. This will cause the first await within that method to be executed, but Main will then continue without waiting for the result of the secondary method. This means your app can just exit without the second statement being run. See this thread for more info: http://stackoverflow.com/questions/9208921/async-on-main-method-of-console-app – lindydonna Feb 18 '15 at 08:32

1 Answers1

1

If posted code runs as trivial console application then we should wait for tasks to be completed. Easiest way to do so is removing await and calling Wait() against Task object returned by xAsync() methods:

NotificationHelper.Instance.Hub.SendMpnsNativeNotificationAsync(toast, userTag).Wait();
NotificationHelper.Instance.Hub.SendAppleNativeNotificationAsync(appleToast, userTag).Wait();
efimovandr
  • 1,594
  • 9
  • 11