3. Setting Up the Server for Push Notifications
Because push notifications come from the
server (and are not necessarily triggered by a running application on
the phone), you need some code somewhere on a server that is reachable
by the phone. The reasoning for this is that the server uses the
channel URI to send any push notifications to the phone. How you choose
to send that information to the server is up to you, but a common
approach is to use a simple web service.
For example, we can have an ASP.NET web project
that hosts a WCF service that accepts the channel URI from the phone.
In this example, I’ve set up a simple database to hold the channel URIs
as well as a WCF service that can accept the channel URIs. The WCF
service interface looks simple enough:
[ServiceContract]
public interface IMyPhoneService
{
[OperationContract]
void RegisterPhoneApp(string channelUrl);
}
The expectation is that the phone will call a
service method with the channel URI when it receives it so that the
server has a list of people to notify. It would be common to include
additional information, but for this example let’s keep it simple. The
implementation of the service would look like this:
public void RegisterPhoneApp(string channelUrl)
{
// Add to list of URIs to notify
using (var ctx = new PhoneEntities())
{
if (ctx.Phones.Where(p => p.PhoneUrl == channelUrl).Count() == 0)
{
var phone = Phone.CreatePhone(0, channelUrl, DateTime.Now);
ctx.Phones.AddObject(phone);
ctx.SaveChanges();
}
}
}
In this case the code just
uses the Entity Framework to store the channel URI in the database for
use later. This means the phone code has to change to actually use this
web service. To do this, you can just create a small helper method that
calls the service:
void SendChannelUriToService(string uri)
{
var client = new MyPhoneServiceClient();
// Handle the completed event
client.RegisterPhoneAppCompleted += (s, e) =>
{
if (e.Error != null)
{
// Log Error (User Can't Fix this error)
}
};
// Send the channel URI to the server
client.RegisterPhoneAppAsync(uri);
}
Now that you have the helper method, you can call this when
you have the channel URI:
public void CreateChannel()
{
try
{
if (_myChannel == null)
{
// Attempt to get the channel
_myChannel = HttpNotificationChannel.Find(CHANNELNAME);
// Can't Find the Channel, so create it
if (_myChannel == null)
{
_myChannel = new HttpNotificationChannel(CHANNELNAME);
// Add Event Handlers
_myChannel.ChannelUriUpdated += myChannel_ChannelUriUpdated;
// Open the channel since it's a new channel
_myChannel.Open();
}
else
{
// Add Event Handlers
_myChannel.ChannelUriUpdated += myChannel_ChannelUriUpdated;
}
// If the channel uri is valid now, then send it to the server
// Otherwise it'll be registered when the URI is updated
if (_myChannel.ChannelUri != null)
{
// Use the ChannelUri to tell your server about the phone
SendChannelUriToService(_myChannel.ChannelUri.ToString());
}
}
return;
}
catch (Exception)
{
MessageBox.Show("Failed to create Push Notification Channel");
}
}
...
void myChannel_ChannelUriUpdated(object sender,
NotificationChannelUriEventArgs e)
{
// Use the ChannelUri to tell your server about the phone
SendChannelUriToService(e.ChannelUri.ToString());
}
With the channel URI in hand, you are ready to push notification messages to the phone!
Authenticating Push Notifications