How to Send Live Tile Notifications in Windows Store Apps

Working with Tile notifications in Windows Store apps is pretty easy. Check out this blog post and download and play with a great example from MSDN. Now, how do you send live tile notifications in your Windows Store apps? You can send tile notifications when something happens in your app, e.g. when the setting is changed by user, or you can send updates periodically, e.g. every 15 minutes, when the app is running in the background on a lock screen.

Sending Live Tiles When Settings Are Changed

One simple example is to send an update when the source file of a video player or the video player volume is changed. In my quick demo, I use the Video Player. To add tile notification function to the app, add the NotificationsExtentions project from the tile example, and one line of code to call the method, SendTile. See sample code below.

private void VolumeSlider_ValueChanged(object sender, RangeBaseValueChangedEventArgs e)
{
VideoPlayer.Volume = (VolumeSlider.Value) / 100;
SendTile();
}

//Send Tile
private void SendTile()
{
// Note: This sample contains an additional project, NotificationsExtensions.
// NotificationsExtensions exposes an object model for creating notifications, but you can also
// modify the strings directly. See UpdateTileWithTextWithStringManipulation_Click for an example

    // create the wide template
ITileWideText03 tileContent = TileContentFactory.CreateTileWideText03();
//tileContent.TextHeadingWrap.Text = "Hello World! My very own tile notification";
tileContent.TextHeadingWrap.Text = "Player's volume (wide tile): " + VolumeSlider.Value;
   

    // Users can resize tiles to square or wide.
// Apps can choose to include only square assets (meaning the app's tile can never be wide), or
// include both wide and square assets (the user can resize the tile to square or wide).
// Apps cannot include only wide assets.

    // Apps that support being wide should include square tile notifications since users
// determine the size of the tile.

    // create the square template and attach it to the wide template
ITileSquareText04 squareContent = TileContentFactory.CreateTileSquareText04();
//squareContent.TextBodyWrap.Text = "Hello World! My very own tile notification";
squareContent.TextBodyWrap.Text = "Player's volume (square tile): " + VolumeSlider.Value;

tileContent.SquareContent = squareContent;

    // send the notification
TileUpdateManager.CreateTileUpdaterForApplication().Update(tileContent.CreateNotification());
}

Sending Live Tiles When App Is Running on a Lock Screen

In this scenario, you will need to run the app as a background task and place it on the lock screen. You can find more info on background tasks here. You can download and test a background task sample app here.

In order to place an app on the lock screen, you call the BackgroundExecutionManager.RequestAccessAsync method in your app. Below is sample code from a lock screen app.

    BackgroundAccessStatus status = await BackgroundExecutionManager.RequestAccessAsync();
switch (status)
{
case BackgroundAccessStatus.AllowedWithAlwaysOnRealTimeConnectivity:
rootPage.NotifyUser("This app is on the lock screen and has access to Always-On Real Time Connectivity.", NotifyType.StatusMessage);
break;
case BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity:
rootPage.NotifyUser("This app is on the lock screen and has access to Active Real Time Connectivity.", NotifyType.StatusMessage);
break;
case BackgroundAccessStatus.Denied:
rootPage.NotifyUser("This app is not on the lock screen.", NotifyType.StatusMessage);
break;
case BackgroundAccessStatus.Unspecified:
rootPage.NotifyUser("The user has not yet taken any action. This is the default setting and the app is not on the lock screen.", NotifyType.StatusMessage);
break;
default:
break;
}
}

image

Note that Tile updates are sent in the same manner as updates sent to the tile on the Start screen, but are limited to text only and must be wide templates.

 

Alternatively, you can register the task of sending Tile updates and schedule to run the task every 15 minutes by using BackgroundTaskBuilder and TimeTrigger.

var builder = new BackgroundTaskBuilder();

builder.Name = name;
builder.TaskEntryPoint = taskEntryPoint;
builder.SetTrigger(new TimeTrigger(15, false));

if (condition != null)
{
builder.AddCondition(condition);
}

BackgroundTaskRegistration task = builder.Register();

Note that the minimum time duration is 15 minutes by design. See more info on “Supporting your app with background tasks”.

image