To subscribe to push notification within an app, we first attempt to retrieve an existing HttpNotificationChannel
by name.
channel = HttpNotificationChannel.Find("<a channel name>");
The HttpNotificationChannel.Find
method does not raise an Exception
and returns null
if the channel with the specified name is not found. If so, a new instance must be created.
The Push Notification sample in the downloadable code contains a class called PushNotificationSubscriber
, which manages subscription to the MPNS by wrapping a notification channel. The main benefit of using a wrapper for the notification channel is that if the PushNotificationSubscriber
instance is unable to perform the subscription to the MPNS, due to the absence of a network connection for example, it automatically retries.
If the channel cannot be found, a new channel is created as shown:
if (channel == null)
{
channel = string.IsNullOrEmpty(serviceName)
? new HttpNotificationChannel(channelName)
: new HttpNotificationChannel(channelName, serviceName);
}
The HttpNotificationChannel
constructor has the following two overloads:
public HttpNotificationChannel(string channelName);
public HttpNotificationChannel(string channelName, string serviceName);
The channelName
parameter is the name that the app uses to identify the notification channel instance. This allows an application to retrieve an existing channel by name using the static HttpNotificationChannel.Find
method.
The serviceName
parameter is the name of the cloud service to which the notification channel is associated. The serviceName
parameter may be used to subscribe to an authenticated cloud service. For more information, see the section “Cloud Service Authentication” later in this chapter.
After you have obtained a channel instance, it can be opened like so:
channel.Open();
3.17.74.154