Search code examples
silverlightwindows-phone-7push-notificationsilverlight-3.0mpns

Windows Phone 7 Push Notifications Not Showing Up On My Phone


UPDATE: The plot thickens. I changed my channel name and it is suddenly working (which means it wasn't a problem with my push service, since I'm getting the same HTTP response from the Microsoft Push Notification server).

To me, however, this is not a solution. How will I be able to test this and KNOW my users are getting their push notifications if I'm getting the same response when it's not working as I do when it is?

[ORIGINAL POST]

I've been trying to get push notifications sent to my Windows Phone 7 device, but I'm having very big problems that I can't find any answers for. I'll start with the c# code.

I set up push notifications using the following C# code.

private HttpNotificationChannel channel;
private static string PUSH_CHANNEL = "MySpecialPushChannel";
private Uri PushUri = null;
private bool IsPushRegistered = false;

public void StartPushSubscription()
{
    try
    {
        channel = HttpNotificationChannel.Find(PUSH_CHANNEL);
    }
    catch
    {}

    if (channel != null)
    {            
        PushUri = channel.ChannelUri;
        if (!channel.IsShellTileBound)
            channel.BindToShellTile();
    }
    else
    {
        channel = new HttpNotificationChannel(PUSH_CHANNEL);
        channel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(channel_ChannelUriUpdated);
        channel.HttpNotificationReceived += new EventHandler<HttpNotificationEventArgs>(channel_HttpNotificationReceived);
        channel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(channel_ErrorOccurred);

        try
        {
            channel.Open();
            channel.BindToShellTile();
        }
        catch (Exception err)
        {
            channel = null;
            IsPushRegistered = false;
            // Code to try again
        }
    }
}

void channel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e)
{
    PushUri = e.ChannelUri;
    IsPushRegistered = true;
}

I'm following the standard WP7 push structure:

  1. Find the HttpNotificationChannel (or start a new one)
  2. Register event handler to get the push notification uri back
  3. Open the channel
  4. Bind to the tile
  5. Handle the channel Uri (which we send to our service to await the happy day when we send the push notification

OK... so far so good. No errors, I get my Uri, send it to my service just fine. I pin my app to the start screen and my service sends a push request to the Uri (sending just the count so that I get a little push count number in the upper right hand corner). I get back an HTTP 200 status with the following:

DeviceConnectionStatus => Connected

NotificationStatus => Received

SubscriptionStatus => Active

And then... nothing. No push status shows up on my app. I've now tried it on my device, in the emulator, on another device, and with multiple servers and the result is always the same. Everything looks like it is working except for the fact that it doesn't work.


Solution

  • To me, however, this is not a solution. How will I be able to test this and KNOW my users are getting their push notifications if I'm getting the same response when it's not working as I do when it is?

    The answer is, you can't. It's a limitation of how WP7 handles notifications.

    For structured notifications like Tile and Toast, if you get the Connected/Active/Received/200 response, then you can know that MPNS accepted your notification request. However, this does not mean that you have sent a valid XML payload.

    The component that handles parsing XML is the Push Client, the process running on the phone that accepts push notifications and deals them out to appropriate applications, displays the toast, etc.

    If you have sent invalid XML, there is absolutely no indication that you've done so. At most, if you try to send the notification again to that same push channel URI, you'll get a 404 in response. Apparently getting an invalid XML payload for a specific application makes that application's push channel close, requiring you to go through the whole procedure again.

    I've discovered this while debugging with our server team, and through trying to get the phone to display an alternate live tile. The only advice I can offer you is to quadruple-check your XML.

    You will get errors in your error event handler for your push notification channel for Toast notifications that have invalid XML, since you are able to send/receive toast notifications while the application is active.


    If anyone from Microsoft is reading this, PLEASE provide more thorough documentation on possible error states in the push notification system. We also need an event handler for Tile notifications, or at least allow us to receive tile notifications while the app is in the foreground and fire the notification channel error event so that we can be aware that our XML payload is invalid.

    Especially if your web service isn't built with WCF, .NET, Azure, and whatever, working with Push Notifications on WP7 is like wandering blind.

    Documentation for an exception message reading "InvalidOperationException(Failed to open channel)" should not read: "This exception is raised when the notification channel failed to open. Try opening the notification channel again." (reference)