locked
Tile Updates Using Push Notification Via backgroundTask.

    Question


  • I have this setup where I have a metro App (I call this client app) that runs a backgroundTask on a trigger. When triggered, I open the pushNotoification channel and I do 2 things

    1) get the channel URI and write that to a file 2) add the pushnotificationreceived event to the newChannel Object each time the task is triggered. I have the handler function defined in the same backgroundTask JS file. I also have this app registered with manage.dev.live so I have the package-sid and client secret which I use in the "Server" App described below.

    Now, I have another app, that I call "server-app" that I use to send the actual WNS push notifications using the package SID, Client Secret and the channelURI (I first get the accessToken ofcourse)  I obtained from the "Client" app.  I do this via XHR posts and converting some C# syntax in some examples I found to JavScript code. (I found this approach easier than writing a web service).

    My PUSH (or POSTS) go successful, I see Http 200OK in the place I expect in my "server app". I am also able to receive the WNS push notification XML string in the pushNotificationReceivedHandler in the backgroundTask of my clientApp. I can use that to update the tile. That works fine.

    I want to do something extra that I am describing below and that is where my problem is

    I want to send an (encoded) string via the push notification and parse that in the pushNotificationReceivedHandlerand use a portion of it to update the tile and I want to log another portion of it to a file in MyDocuments. This does not work.

    I can force it work when dont call close() in my backgroundTask and when I have a breakpoint set in my pushNotificationReceivedHandler function.  Otherwise, it just seems to update my tile with the entire "text" I send from my server App.

    Hope this makes sense. I am attaching snippets of my code below. I have been stuck on this for over  a week now, need someone to please verify and advice if there's something obvious I am doing wrong?

    Thank you!

    function openNotificationsChannel() {
               channelOperation = pushNotifications.PushNotificationChannelManager.createPushNotificationChannelForApplicationAsync();
               channelOperation.then(function (newChannel) {
                   newChannel.addEventListener("pushnotificationreceived", pushNotificationReceivedHandler);
                   Windows.Storage.KnownFolders.documentsLibrary.createFileAsync("chl.uri",
                   Windows.Storage.CreationCollisionOption.replaceExisting).then(function (file) {
                       file.openAsync(Windows.Storage.FileAccessMode.readWrite).then(function (stream) {
                           var outputStream = stream.getOutputStreamAt(stream.size);
                           var writer = new Windows.Storage.Streams.DataWriter(outputStream);
                           writer.writeString(newChannel.uri + '&ts=' + new Date().getTime().toString());
                           writer.storeAsync().then(function () {
                               outputStream.flushAsync().then(function () {
                                   Windows.UI.Notifications.TileUpdateManager.createTileUpdaterForApplication().clear();
                                   //close(); ( this shouldnt be commented, I have to do it to have my breakpoint hit in the handler function below)
    
                               });
                           });
                       });
                   });
               },
               function (error) {});
       }
    
    function pushNotificationReceivedHandler(e) {
           
           var notificationPayload = e.tileNotification.content;
    
           var textNodesList = notificationPayload.getElementsByTagName("text")[0];
           var textNode = textNodesList.childNodes[0].nodeValue;
           var relevantDataObj = JSON.parse(textNode);
           var tileText = relevantDataObj.tileText;
           var transitionScreenInfo = relevantDataObj.transitionScreen;
           writetransitionScreenInfoToFile(JSON.stringify(transitionScreenInfo), tileText);
                   
           e.cancel = true;
            
       }
     function writetransitionScreenInfoToFile(transitionData, tileText) {
            Windows.Storage.KnownFolders.documentsLibrary.createFileAsync("subscription.txt",
                Windows.Storage.CreationCollisionOption.replaceExisting).then(function (file) {
                file.openAsync(Windows.Storage.FileAccessMode.readWrite).then(function (stream) {
                    var outputStream = stream.getOutputStreamAt(stream.size);
                    var writer = new Windows.Storage.Streams.DataWriter(outputStream);
                    writer.writeString(transitionData);
                    writer.storeAsync().then(function () {
                        outputStream.flushAsync().then(function () {
                            var tileXml = Notifications.TileUpdateManager.getTemplateContent(Notifications.TileTemplateType.tileWideText03);
                            var tileAttributes = tileXml.getElementsByTagName("text");
                            tileAttributes[0].appendChild(tileXml.createTextNode(tileText));
    
                            // 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);
                            
                        });
                    });
                });
            });
        }






    • Edited by optimus_p Tuesday, January 03, 2012 11:11 PM
    Tuesday, January 03, 2012 11:10 PM

All replies

  • Hi Opt,

    When you call close(), this closes the background task.  What event are you using to trigger the background task?  Why are you using push notifications in this case?

    You should not do additional processing to massage the message.  The point of the notifications is that your application does not need to run to update the tiles.  By running your code, you are taking up processor time and memory resources.  Do all of your processing to the message before sending it so your application can update the tiles based on the notifications.

    -Jeff

     


    Jeff Sanders (MSFT)
    Wednesday, January 04, 2012 3:17 PM
    Moderator