使用推送通知通过backgroundTask进行平铺更新。 [英] Tile Updates Using Push Notification Via backgroundTask.

查看:81
本文介绍了使用推送通知通过backgroundTask进行平铺更新。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有这个设置,我有一个Metro应用程序(我称之为客户端应用程序),它在触发器上运行backgroundTask。触发后,我打开pushNotoification频道,我做2件事情。


1)获取频道URI并将其写入文件2)将pushnotificationreceived事件添加到newChannel每次触发任务时都对象。我在同一个backgroundTask JS文件中定义了处理函数。我还在
manage.dev.live中注册了这个应用程序,所以我在"服务器"中使用了package-sid和client secret。应用程序如下所述。
$


现在,我有另一个应用程序,我称之为"server-app"我用来发送实际的WNS推送通知使用包SID,Client Secret和channelURI(我首先获得accessToken ofcourse) 我是从"客户"获得的。 。应用 我通过
XHR发布并在我发现的JavScript代码的一些示例中转换了一些C#语法。 (我发现这种方法比编写Web服务更容易)。
$


我的PUSH(或POSTS)成功,我在我期望的地方看到Http 200OK ;服务器应用程序"我还能够在clientApp的backgroundTask中的pushNotificationReceivedHandler中接收WNS推送通知XML字符串。我可以用它来更新瓷砖
。这很好。



我想做一些我在下面描述的额外内容,这就是我的问题所在。


我想通过推送通知发送一个(编码的)字符串,并解析pushNotificationReceivedHandle中的一部分,并使用它的一部分来更新磁贴,我想将其另一部分记录到MyDocuments中的文件中。这不起作用。
$


我可以在我的backgroundTask中调用close()时以及在pushNotificationReceivedHandler函数中设置断点时强制它工作。 否则,它似乎只是用整个"文本"更新我的磁贴。我从我的服务器App发送。



希望这是有道理的。我在下面附上我的代码片段。我一直坚持这个过去 一个星期了,需要有人来验证和建议,如果有什么明显我做错了吗?


谢谢!


  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(); (这不应该被评论,我必须这样做才能让我的断点在下面的处理函数中命中)

});
});
});
});
},
function (错误){});
}

function pushNotificationReceivedHandler(e){

var notificationPayload = e.tileNotification.content;

var textNodesList = notificationPayload.getElementsByTagName(" text" )[ 0];
var textNode = textNodesList.childNodes [0] .nodeValue;
var relatedDataObj = 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",$ b) $ b 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));

//从XML
var tileNotification = new Notifications.TileNotification(tileXml)创建通知;
//将通知发送到应用程序的默认磁贴
Notifications.TileUpdateManager.createTileUpdaterForApplication()。update(tileNotification);

});
});
});
});
}




解决方案

嗨选择,


当你调用close()时,这会关闭后台任务。 您使用什么事件来触发后台任务? 在这种情况下,为什么使用推送通知?


您不应该进行其他处理来按摩消息。 通知的要点是您的应用程序无需运行即可更新磁贴。 通过运行代码,您将占用处理器时间和内存资源。 
在发送邮件之前对邮件进行所有处理,以便您的应用程序可以根据通知更新磁贴。


-Jeff


 



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);
                        
                    });
                });
            });
        });
    }



解决方案

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

 


这篇关于使用推送通知通过backgroundTask进行平铺更新。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆