Export (0) Print
Expand All

How to: Receive Push Notifications in an Application for Windows Phone

1/28/2011

To receive push notifications to your application, you must set up a notification channel and bind it to the correct type of notification.

On the application, when creating an HttpNotificationChannel, use the fully qualified domain name (FQDN) as the service name. While this is not required for unauthenticated web services, it is a best practice. For authenticated web services, this FQDN must match the registered certificate’s subject name (the CN attribute). For example, www.contoso.com.

The following code example describes how to create a notification channel and bind to a toast or Tile notification. For more information about what the error types mean in myChannel_ErrorOccurred below, see ChannelErrorType.

public HttpNotificationChannel myChannel;
public void CreatingANotificationChannel()
{
    myChannel = HttpNotificationChannel.Find("MyChannel");

    if (myChannel == null)
    {
        // Only one notification channel name is supported per application.
        myChannel = new HttpNotificationChannel("MyChannel","www.contoso.com");

        SetUpDelegates();

        // After myChannel.Open() is called, the notification channel URI will be sent to the application through the ChannelUriUpdated delegate.
        // If your application requires a timeout for setting up a notification channel, start it after the myChannel.Open() call. 
        myChannel.Open();
    }
    else // Found an existing notification channel.
    {
        SetUpDelegates();

        // The URI that the application sends to its web service.
        Debug.WriteLine("Notification channel URI:" + myChannel.ChannelUri.ToString());

        if (myChannel.ChannelUri == null)
        {
            // The notification channel URI has not been sent to the client. Wait for the ChannelUriUpdated delegate to fire.
            // If your application requires a timeout for setting up a notification channel, start it here.
        } 
    }

    // An application is expected to send its notification channel URI to its corresponding web service each time it launches.
    // The notification channel URI is not guaranteed to be the same as the last time the application ran. 
    if (myChannel.ChannelUri != null)
    {
        // SendURIToService(myChannel.ChannelUri);
    }
}

public void SetUpDelegates()
{
      myChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(myChannel_ChannelUriUpdated);
      myChannel.HttpNotificationReceived += new EventHandler<HttpNotificationEventArgs>(myChannel_HttpNotificationReceived);
      myChannel.ShellToastNotificationReceived += new EventHandler<NotificationEventArgs>(myChannel_ShellToastNotificationReceived);
      myChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(myChannel_ErrorOccurred);
}

void myChannel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e)
{
    // The URI that the application will send to its corresponding web service.
    Debug.WriteLine("Notification channel URI:" + e.ChannelUri.ToString());
    // SendURIToService(e.ChannelUri);
}

void myChannel_ErrorOccurred(object sender, NotificationChannelErrorEventArgs e)
{
    switch(e.ErrorType)
    {
        case ChannelErrorType.ChannelOpenFailed:
            // ...
            break;
        case ChannelErrorType.MessageBadContent:
            // ...
            break;
        case ChannelErrorType.NotificationRateTooHigh:
            // ...
            break;
        case ChannelErrorType.PayloadFormatError:
            // ...
            break;
        case ChannelErrorType.PowerLevelChanged:
            // ...
            break;
    }
}

// Receiving a toast notification. 
// Toast notifications are only delivered to the device when the application is not running in the foreground. 
// If the application is running in the foreground, the toast notification is instead routed to the application.
void myChannel_ShellToastNotificationReceived(object sender, NotificationEventArgs e)
{
    if (e.Collection != null)
    {
        Dictionary<string, string> collection = (Dictionary<string, string>)e.Collection;
        System.Text.StringBuilder messageBuilder = new System.Text.StringBuilder();

        foreach (string elementName in collection.Keys)
        {
            //...
        }
    }
}

// Receiving a raw notification. 
// Raw notifications are only delivered to the application when it is running in the foreground. 
// If the application is not running in the foreground, the raw notification message 
// is dropped on the Push Notification Service and is not delivered to the device.
void myChannel_HttpNotificationReceived(object sender, HttpNotificationEventArgs e)
{
    if (e.Notification.Body != null && e.Notification.Headers != null)
    {
        System.IO.StreamReader reader = new System.IO.StreamReader(e.Notification.Body);
    }
}



// If you do not want to use remote resources for your Tile's background image, use the following code to bind a notification channel to a local resource. 
// A Tile notification is always delivered to the Tile, regardless of whether the application is running in the foreground.  
private void BindingANotificationsChannelToATileNotification()
{
    if (!myChannel.IsShellTileBound)
    {
        myChannel.BindToShellTile();
    }
}


// If you want to use remote or local resources for your Tile's background image, 
// use the following code to bind a notification channel to either a remote resource that is in the approved list, or to a local resource. 
// A Tile notification is always delivered to the Tile, regardless of whether the application is running in the foreground.  
private void BindingANotificationsChannelToALiveTileNotification()
{
    if (!myChannel.IsShellTileBound)
    {
        myChannel.BindToShellTile();
    }

    // The approved list of URIs that will be verified on every push notification that contains a URI reference.
    Collection<Uri> ListOfAllowedDomains = new Collection<Uri> { new Uri("www.contoso.com") };
    myChannel.BindToShellTile(ListOfAllowedDomains);
}

// Binding a notification channel to a toast notification.
private void BindingANotificationsChannelToAToastNotification()
{
    if (!myChannel.IsShellToastBound) 
    {
        myChannel.BindToShellToast();
    }
}

Show:
© 2014 Microsoft