IT tutorials
 
Technology
 

Windows Phone 8 : Using Push Notifications (part 2) - Setting Up the Server for Push Notifications

9/21/2013 2:02:24 AM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

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.


Server-Side Code

There is nothing special about the server-side code that requires the server technology and operating system to be Microsoft servers or ASP.NET, but this example will use WCF and ASP.NET because that is a common approach. You can use whatever server technology and operating systems you choose.


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

The MPNS can use a custom certificate you send to Microsoft during the submission process for authenticating your phone. Push notifications are specifically from your own server(s).

 
Others
 
- Windows Phone 8 : Using Push Notifications (part 1) - Preparing the Application for Push Notifications
- InfoPath with SharePoint 2010 : Document Information Panel Content Type - Modify the DIP
- InfoPath with SharePoint 2010 : Document Information Panel Content Type - Create the Document Library, Add Columns to Your DIP
- Windows Small Business Server 2011 : Working with Users (part 4) - Creating User Roles
- Windows Small Business Server 2011 : Working with Users (part 3) - Managing User Properties
- Windows Small Business Server 2011 : Working with Users (part 2) - Creating Multiple User Accounts
- Windows Small Business Server 2011 : Working with Users (part 1) - Creating a User Account
- Active Directory 2008 : Managing Operations Masters (part 3) - Seizing Operations Master Roles, Returning a Role to Its Original Holder
- Active Directory 2008 : Managing Operations Masters (part 2) - Optimizing the Placement of Operations Masters, Transferring Operations Master Roles
- Active Directory 2008 : Managing Operations Masters (part 1) - Domain-Wide Operations Master Roles
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us