locked
UI update from background task

    Question

  • Hi,

    I've been reading the background task whitepaper its states the following best practice:-

     

    Avoid the following:

    1  Do not display UI other than toast notifications from a background task.

    2  Do not rely on user interaction in background tasks.

     

    Therefore we shouldnt be updating the application tile from a background task? This means that the only way the tile can be updated when the application is suspended is via a push notification?

    One of the tile templates and badges shows an SMS badge to show that sms has been received - assuming the sms is received when an sms client is not running, how would this get reflected on the tile?

    Thanks

    Mike

     

     

    Wednesday, October 26, 2011 2:00 PM

Answers

  • Hello Mike,

    Your background task can update the tile.  From your background task you would need to simply call the tile notification.  You can combine the Background Task sample with the Advanced Tile Notifications sample to see this.  For example, I gave a wide tile image in my Background Task sample and then added this code in the backgroundtask.js:

    // global var for convenience
    var Notifications = Windows.UI.Notifications;


    // in the timer when progress is 100:
      // get a filled in version of the template by using getTemplateContent
                    var tileXml = Notifications.TileUpdateManager.getTemplateContent(Notifications.TileTemplateType.tileWideText03);

                    // You will need to look at the template documentation to know how many text fields a particular template has
                    // You can also use the Advanced Tile SDK Sample to preview all of the templates

                    // get the text attributes for this template and fill them in
                    var tileAttributes = tileXml.getElementsByTagName("text");
                    tileAttributes[0].appendChild(tileXml.createTextNode("Hello World! My very own tile notification"));

                    // create the notification from the XML
                    var tileNotification = new Notifications.TileNotification(tileXml);

                    // send the notification to the app's default tile
                    Notifications.TileUpdateManager.createTileUpdaterForApplication().update(tileNotification);

     

    -Jeff


    Jeff Sanders (MSFT)
    Wednesday, October 26, 2011 8:22 PM
    Moderator

All replies

  • Hello Mike,

    Your background task can update the tile.  From your background task you would need to simply call the tile notification.  You can combine the Background Task sample with the Advanced Tile Notifications sample to see this.  For example, I gave a wide tile image in my Background Task sample and then added this code in the backgroundtask.js:

    // global var for convenience
    var Notifications = Windows.UI.Notifications;


    // in the timer when progress is 100:
      // get a filled in version of the template by using getTemplateContent
                    var tileXml = Notifications.TileUpdateManager.getTemplateContent(Notifications.TileTemplateType.tileWideText03);

                    // You will need to look at the template documentation to know how many text fields a particular template has
                    // You can also use the Advanced Tile SDK Sample to preview all of the templates

                    // get the text attributes for this template and fill them in
                    var tileAttributes = tileXml.getElementsByTagName("text");
                    tileAttributes[0].appendChild(tileXml.createTextNode("Hello World! My very own tile notification"));

                    // create the notification from the XML
                    var tileNotification = new Notifications.TileNotification(tileXml);

                    // send the notification to the app's default tile
                    Notifications.TileUpdateManager.createTileUpdaterForApplication().update(tileNotification);

     

    -Jeff


    Jeff Sanders (MSFT)
    Wednesday, October 26, 2011 8:22 PM
    Moderator
  • Hi Jeff,

    Thanks for the reply and the example. I am also currently updating the tile from the background task, I guess my question was more about the background task best practice guidelines I included in the original post about not updating any UI apart from toasts.

    Thanks again

    Mike

    Monday, October 31, 2011 8:32 AM
  • Got ya Mike,

    I think the tone of that warning is that we don't want especially important things that require immediate user interaction to be passively displayed.

    -Jeff


    Jeff Sanders (MSFT)
    Monday, October 31, 2011 8:17 PM
    Moderator
  • Where can I find this specific whitepaper?
    Wednesday, November 16, 2011 6:57 PM
  • Its in the "Introduction to Background tasks" guide found here http://www.microsoft.com/download/en/details.aspx?id=27411

    • Proposed as answer by optimus_p Thursday, November 17, 2011 8:13 PM
    Thursday, November 17, 2011 1:12 PM
  • Got ya Mike,

    I think the tone of that warning is that we don't want especially important things that require immediate user interaction to be passively displayed.

    -Jeff


    Jeff Sanders (MSFT)


    Hey Jeff,

    So what's the "best practice" approach for "important things that require immediate user interaction"? Can a desktop app interact with the Metro UI interface? So that when the desktop app needs to notify the user about an imminent action something displays on top of everything else?

    Like the incoming text message on my Windows Phone for instance? And I can click on that notification and it takes me to the messaging app...

    That's what I need to do... if at all possible...

    Thanks, Florin

    Tuesday, November 22, 2011 3:12 AM
  • Hi Florin,

    Stand by for more information about Desktop notifications!  The Developer preview is geared toward Metro Style app development and there will be more information about this (no date has been announced yet for future previews/beta). 

    -Jeff 


    Jeff Sanders (MSFT)
    Tuesday, November 22, 2011 1:03 PM
    Moderator
  • Hi Jeff,

    Is this the progress handler that is in the metro app where this code would be added? if so then how could you do this is the metro app was suspended since in that case the delegate for the progress is not going to be called according the white paper?

     

    Thanks,

    Shawn

    Wednesday, December 07, 2011 10:04 PM
  • Hi Shawn,

    You would add this code to your background task.  The TileUpdateManager would send the update to the tile.  One important thing here... you could not trap the notification events in your app if it is running to display the notification message the way the advanced tiles sample does if the app is suspended or not running.  But we really want the tile updated, not display the tile notification messages in a real app. 

    Make sense?

    -Jeff


    Jeff Sanders (MSFT)
    Thursday, December 08, 2011 1:13 PM
    Moderator
  • It does, I wasn't able to get it to work until I used the updater constructors that don't take the appid as a parameter. I figured that since the background task was running in another process i would need to specify the appid for the tile, toast, etc. updaters, but just using the default constructor does work when I tested it.

     

    Thanks

    Sunday, December 11, 2011 11:51 PM
  • Cool, thanks for sharing what your problem was.  I am sure it will help others in the community!

    -Jeff


    Jeff Sanders (MSFT)
    Monday, December 12, 2011 1:03 PM
    Moderator
  • Any update on this since W8CP is out?
    Tuesday, March 06, 2012 9:01 PM
  • Hey Ghost,

    Which part of this discussion?

    -Jeff


    Jeff Sanders (MSFT)

    Friday, March 09, 2012 12:58 PM
    Moderator
  • Sample apps have been published on how to send metro UI toast notifications from desktop apps. That's what I was looking for anyway:

    http://code.msdn.microsoft.com/windowsdesktop/sending-toast-notifications-71e230a2

    Friday, April 06, 2012 6:40 PM