IT tutorials
 
Technology
 

Windows Phone 8 : Using Push Notifications (part 1) - Preparing the Application for Push Notifications

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

At times you will want to keep the user apprised of some change that happen on the server. Although you could write a background agent , making network requests every 30 minutes might not be often enough. You might also decide that you want to determine when to update a phone remotely (as close to the data as possible). So instead, you should use the phone’s ability to receive messages from the cloud. These messages are called push notifications.

Push notifications enable your application to register a particular phone to receive these notifications. After the phone is registered, you can send messages from an Internet-connected server to Microsoft’s Push Notification Service (MPNS). This service is hosted by Microsoft and enables your messages to reach users’ phones (and deals with the hard problems of out-of-range and powered-off phones). Using push notifications does require that you have a way to communicate with a service you are responsible for; typically this is a web service hosted on a public Internet server. You can see the general flow of push notifications in Figure 1.

Image

FIGURE 1 Push notification message flow

Using push notifications requires several steps, but they are all fairly straightforward:

Your app opens a push notification channel.

Your app receives a special URL that can be used to send messages to this specific phone.

Your app sends that URL to a service that wishes to send you notifications.

Sometime later, the service can post a new message to MPNS using the special URL from the phone.

The MPNS finds your phone and sends the message to the phone.

Push notifications can be used to do three tasks on the phone: alert the user with a toast notification, update a Live Tile, and send your application raw messages. You will see how to do all three, but let’s start with the basics so that you can learn how all the moving parts work together.

1. Push Notification Requirements

Using push notifications comes with several requirements. To use push notifications, you must do the following:

Add the push notification requirement to your WMAppManifest.xml file:

...
<Capabilities>
  <Capability Name="ID_CAP_PUSH_NOTIFICATION"/>
</Capabilities>
...

Alert the user in the user interface of the application (usually during the first launch of your application) that you are using push notifications.

Allow the user to disable push notifications.

You can have only one push notification channel per application. In addition, a limited number of applications on a phone can use push notifications. When you first open your HTTP notification channel, you might receive an error message stating that the channel could not be opened. This is typically thrown when there are too many push notification applications on the phone.

2. Preparing the Application for Push Notifications

To get started, you will need an instance of a class that will register your application for push notifications as well as listen for certain events (for example, notifications, errors, and so on). This class is called HttpNotificationChannel. When you launch your application the first time, you will register this instance of the application (specifically the application on this particular phone) with the MPNS by creating a new instance of the class and passing in a channel name that is specific to the application (for instance, the app name):

HttpNotificationChannel  myChannel =
  new HttpNotificationChannel("MYAPP");

The name of the channel is usually the name of the application, but because you can have only one channel, it is not important that this name be unique to the phone or user. On subsequent launches of your application, you can get the channel by calling the static Find method using the name like so:

HttpNotificationChannel  myChannel =
  HttpNotificationChannel.Find("MYAPP");

In practice, determining whether this is the first invocation can be tedious, so you should just try to find the channel, and if it is not found just create it like so:

public class PushSignup
{
  const string CHANNELNAME = "FunWithToast";
  private HttpNotificationChannel _myChannel = null;

  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);
...

This way, you won’t need to worry about keeping state on the first invocation of your application.

After you have a valid channel, that channel will give your server access to a special URI that is used to send push notifications. Again, the channel is a little different, depending on whether it was newly created or located with the Find method. You would think that you should be able to take the newly created channel and retrieve the URI, but that only works on existing channels (that is, channels located with Find):

...

// 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
}

...

Newly created channels will get the URI asynchronously. To know when the URI is available, you need to register the ChannelUriUpdated event on the channel object. When this event is fired, you can use the ChannelUri. In practice you should handle this in both cases to ensure that any changes to the URI are caught and reported:

_myChannel.ChannelUriUpdated += myChannel_ChannelUriUpdated;

...

void myChannel_ChannelUriUpdated(object sender,
                                 NotificationChannelUriEventArgs e)
{
  // Use the ChannelUri to tell your server about the phone
}

After you have the channel open and a way to get the ChannelUri, you’re ready to set up the server side of the equation.

 
Others
 
- 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
- SQL Server 2012 : Physical and Virtual Memory (part 2) - NUMA
 
 
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