Bard's Tile II : Scheduled and Periodic Notifications

Tags: Release Preview, .NET, Windows 8, WinRT, Live Tiles, Badges, Scheduled, Periodic

 

This is the part II of the Bard’s Tile series, this time about Periodic and Scheduled Tiles.

Bard’s Tile I : Introduction, local Tiles et Badges

Bard’s Tile II : Scheduled et Periodic Notifications

Bard’s Tile III : Push Notifications

Bard’s Tile IV : Secondary Tiles

Bard’s Tile V : Lock Screen Tiles and Badges

Scheduled Tile Notification

As the name implies, it is tiles updates that are well … scheduled. (I feel like stating the obvious).

Imagine you have a calendar application, and you want to send an reminder update one hour before an appointment, for instance. Or one hour before a task due date. That is where scheduled tile notification comes into play.

The principle is the same as with local tile notification, except for three things :

- You use ScheduledTileNotification class instead of TileNotification class.

- You give a DateTimeOffset representing the time at which the tile is scheduled to be updated.

- You use TileUpdater AddToSchedule method to send the notification.

Here is an example :

var tileXml = TileUpdateManager.GetTemplateContent(TileTemplateType.TileSquareText04);

var duetime = new DateTimeOffset(DateTime.Now.AddMinutes(1));

var tileTextAttributes = tileXml.GetElementsByTagName("text");
tileTextAttributes[0].AppendChild(tileXml.CreateTextNode("This is a scheduled notification due : " + duetime.ToString("hh:mm:ss")));

// Create the notification object.
var scheduledTile = new ScheduledTileNotification(tileXml, duetime);
// Add to the schedule.
TileUpdateManager.CreateTileUpdaterForApplication().AddToSchedule(scheduledTile);

 

Note : I didn’t found anything in documentation, but from empiric observation the minimum scheduled time is 1 minute after current time.

The rest is the same as for local tiles. You can use the Tag property to prevent duplicate notification, you can add an ExpirationTime,etc…

Periodic Tile Notification

 

As the name implies, Periodic Notification is periodically polling for update. The idea is to periodically poll an URI that will give a Tile XML. You can have a weather application polling every hour an URL to get the updated temperature. This is the example I’ll show.

Creating a Periodic Tile Notification

I created a simple REST WCF service that accept a single parameter (In my case, the city code) and returns an XML Tile:

 public interface IService1
{
        [WebGet(UriTemplate = "/Tile/{id}")]
        [OperationContract]
        XElement GetTile(string id);
}

The implentation:

public XElement GetTile(string city)
{
    var webConfigPath = Path.GetDirectoryName(
System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/").FilePath);

    var xml = XElement.Load(webConfigPath + @"/TileXML1.xml");

    foreach(var binding in xml.Element("visual").Elements("binding"))
    {
        var textlist = binding.Elements("text").ToList();

        textlist[0].Value = city;

        var temperature = 0;

        switch (city.ToLower())
        {
            case "paris":
                temperature = new Random().Next(0, 25);
                break;
            case "los angeles":
                temperature = new Random().Next(15, 40);
                break;

        }
        textlist[1].Value = string.Format("Temperature {0}°", temperature);

    }
    return xml;
}

The TileXML1.xml is just a Tile xml structure :

<?xml version="1.0" encoding="utf-8" ?>
<tile>
  <visual lang="en-US">
    <binding template="TileSquareText02">
      <text id="1"></text>
      <text id="2"></text>
    </binding>

    <binding template="TileWideText09">
      <text id="1"></text>
      <text id="2"></text>
    </binding>
  </visual>
</tile>

The XML structure of any Tile is here.

So now, if I call the REST service like : http://localhost:1033/Service1.svc/Tile/Paris I get this :

<?xml version="1.0"?>
<tile>
  <visual lang="en-US">
    <binding template="TileSquareText02">
      <text id="1">Paris</text>
      <text id="2">Temperature 24°</text>
    </binding>
    <binding template="TileWideText09">
      <text id="1">Paris</text>
      <text id="2">Temperature 24°</text>
    </binding>
  </visual>
</tile>

 

It is valid XML for a Square and Wide Tile. Now, we just have to connect it with our application. And the way to do it is just to use the StartPeriodicUpdate method of the TileUpdater class, which accepts an URI and a PeriodicUpdateRecurrence parameters:

 var tileupdater = TileUpdateManager.CreateTileUpdaterForApplication();

tileupdater.StartPeriodicUpdate(new Uri("http://localhost:1033/Service1.svc/Tile/Paris"), PeriodicUpdateRecurrence.Hour);

 

That’s all ! Our tile will look like this :

Stopping Periodic Notification

You can, of course, stopping the periodic polling is you want :

var tileupdater = TileUpdateManager.CreateTileUpdaterForApplication();

tileupdater.StopPeriodicUpdate();

Periodic Badge Notification

 

You can also do periodic polling for Badge Notification. The principles are the same as with Tile Periodic Notifications.

Creating a Periodic Badge Notification

 

I created another REST WCF Service that returns a Badge XML. (Details of the Badge XML is here) :

    [ServiceContract]
    public interface IService1
    {
        [WebGet(UriTemplate = "/Badge/{city}")]
        [OperationContract]
        XElement GetBadge(string city);
    }

 

The implementation :

public XElement GetBadge(string city)
        {
            var webConfigPath = Path.GetDirectoryName(
System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/").FilePath);

            var xml = XElement.Load(webConfigPath + @"/BadgeXML1.xml");

            var glyph = 0;

            switch (city.ToLower())
            {
                case "paris":
                    glyph = new Random().Next(1, 49);
                    break;
                case "los angeles":
                    glyph = new Random().Next(50, 99);
                    break;

            }

            xml.Attribute("value").Value = "" + glyph;

            return xml;
        }

 

The Badge XML :

<?xml version="1.0" encoding="utf-8" ?>
<badge value ="" />

Calling http://localhost:1033/Service1.svc/Badge/Paris gives for instance :

<?xml version="1.0"?>
<badge value="6"/>

Now, let’s connect it with our application :

var badgeupdater = BadgeUpdateManager.CreateBadgeUpdaterForApplication();

badgeupdater.StartPeriodicUpdate(new Uri("http://localhost:1033/Service1.svc/Badge/Paris"), PeriodicUpdateRecurrence.HalfHour);

 

Now our tile looks like this :

Stopping Periodic Notification

You can also stop the polling :

var badgeupdater = BadgeUpdateManager.CreateBadgeUpdaterForApplication();

badgeupdater.StopPeriodicUpdate();

 

Conclusion

As saw, WinRT offers a very simple way to provide scheduled or periodic tile notification. Again, it is not because you can do it that you have to do it. In some situations, it is a big plus for the UX. I others, it is just information pollution. There is no clear answers, it really depends from application to application. Just ask yourself if it is really a plus, making your application easier to use. That should give you the start of an answer.

Next post will be about the main dish : Push Notifications, a more complex but powerful way of updating Tiles (Or Badges).

Sources of example application are here

Comments powered by Disqus