C# Corner

Advanced Live Tiles, Part 3: Lock Screens

How to use lock screen badge and tile notifications in your Windows 8 application.

Welcome to the third and final iteration of my Windows 8 Live Tiles series. Today I'll show you how to use lock screen badge and tile notifications in sample Windows 8 application as requested in Using Push Notifications in Windows 8.

Let's get started. Create a new Blank  C# Windows Metro style application in Visual Studio 2012. Now locate the file named Package.appxmanifest within the solution explorer and open it. There are a few settings that must be defined within an application in order to support lock screen notifications. Foremost you must define a Badge logo. The logo should be a 24x24 pixel image. You also need to set the Lock screen notifications type, which are Badge and Badge and Tile Text. For this sample, select Badge and Tile Text. You also need to set a Wide logo as well with the Badge and Tile Text notification selection. Figure 1 shows the needed settings.


[Click on image for larger view.]
Figure 1. Defining Badges in package.appxmanifest.

Once you've set both the Badge logo and Lock screen notifications settings, you'll notice a red error logo. This error indicates that you must declare a background task in order to use lock screen notifications. Click on the Declarations tab. Next select Background Tasks from the Available Declarations combo box and click on the Add button. Now check the Push notifications check box under the Properties group box. Next, enter in the fully qualified path to your MainPage class. For this example, the full path is VSMAdanvedTilesP3Demo.MainPage. Last but not least save the file. See Figure 2 for any necessary guidance.


[Click on image for larger view.]
Figure 2. You must set a background task to use lock screen notifications.

Now the application is fully configured to allow lock screen push notifications. All that's left to do is to implement the needed background task. First let's setup the UI for the application. The user will be able to enter a message and send either a badge  or a tile notification that that may be displayed on their lock screen. Open up MainPage.xaml and insert the StackPanel markup from Listing 1 into the root Grid element.

Now that the UI is set up, you can proceed to create the appropriate tile or badge to be sent from the respective Send Badge or Send Tile buttons. Open up MainPage.xaml.cs and add the following three namespaces:

using Windows.ApplicationModel.Background;
using Windows.UI.Notifications;
using Windows.Data.Xml.Dom; 

The Windows.ApplicationModel.Background namespace contains the BackgroundExecutionManager that will allow the user to permit the application to send badge notifications to their lock screen. Update the OnNavigatedTo method signature to be an async method as follows:

protected async override void OnNavigatedTo(NavigationEventArgs  e)

Within the OnNavigatedTo method, call the BackgroundExecutionManager.RequestAccessAysnc method to prompt the user to grant lock screen notification access to the application:

protected async override void OnNavigatedTo(NavigationEventArgs e)
  {
             BackgroundAccessStatus status = await 	
         BackgroundExecutionManager.RequestAccessAsync();
             Status.Text = GetBadgeAcessMessage(status);
  }

The GetBadgeAcessMessage takes a BackgroundAccessStatus and returns an appropriate message from the enum's value, as defined in Listing 2.

The SendBadge_Click event handler creates a badge notification from the user's entered message. Once the badge is sent, the status text is updated to notify the user that the badge was sent:

private void SendBadge_Click(object sender, RoutedEventArgs e)
 {
     string badge = String.Format("<badge value=\"{0}\"/>", Message.Text);
     XmlDocument xmlDoc = new XmlDocument();
     xmlDoc.LoadXml(badge);
     BadgeNotification badgeNotification = new BadgeNotification(xmlDoc);
     BadgeUpdateManager.CreateBadgeUpdaterForApplication().Update(badgeNotification);
     Status.Text = "Lock Screen Badge was sent.";
 }

The SendTile_Click event handler creates a tile notification from the user's entered message. Once the tile's sent, the status text is updated to notify the user, as shown in Listing 3.

See Listing 4 for the entire MainPage code-behind source code.

The first time your run the application, you'll be prompted to grant lock screen notification access, as shown in Figure 3.


[Click on image for larger view.]
Figure 3. The grant lock screen notification access dialog.

Once you grant lock screen access to the application, you may send Badge notifications (Figure 4). They'll be displayed on the user's lock screen, as shown in Figure 5.


[Click on image for larger view.]
Figure 4. Sending a badge notification.


[Click on image for larger view.]
Figure 5. The badge notification on the lock screen.

You may notice that you're unable to send a tile notification to the lock screen. This is because only one application may send a detailed lock screen notification to the user's start screen. To allow the sample application to send the tile, update open up the "Customize your lock screen and notifications" program. You can locate the program by searing for "lock" from the Start Menu Search screen, as shown in Figure 6.


[Click on image for larger view.]
Figure 6. Finding the customize your lock screen and notifications program.

Once you're on the custom notifications settings screen, click on the + button under the "Choose an app to display detailed status" heading, shown in Figure 7, and select the sample application.


[Click on image for larger view.]
Figure 7. Granting detailed lock screen access.

You should now be able to properly send a tile (Figure 8) to the lock screen (Figure 9).


[Click on image for larger view.]
Figure 8. Sending a lock screen tile notification.


[Click on image for larger view.]
Figure 9. Receiving the tile update on the lock screen.

It can be quite useful to send notifications to the user's lock screen. Keep in mind that the user may only have one application display a tile update on the lock screen at a time. If you need to send a very important notification, you may also want to send the notification on an alternate communication channel. Badge updates, on the other hand, are more plentiful and provide a very concise way to notify the user of an update to an application's content.

About the Author

Eric Vogel is a Senior Software Developer for Red Cedar Solutions Group in Okemos, Michigan. He is the president of the Greater Lansing User Group for .NET. Eric enjoys learning about software architecture and craftsmanship, and is always looking for ways to create more robust and testable applications. Contact him at [email protected].

comments powered by Disqus

Featured

Subscribe on YouTube