Advanced Live Tiles, Part 3: Lock Screens

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:

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.

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.

Eric Vogel is a Sr. Software Developer at Kunz, Leigh, & Associates in Okemos, MI. 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 vogelvision@gmail.com.