Metro App Prevent BackgroundTask from Running if UI is Open - windows-runtime

I have a BackgroundTask which connects to a remote server and does some kind of action, download, upload, etc. This task runs every 15 minutes.
The UI associated to the BackgroundTask does the same and more.
However, the remote server allows only one session per login and I have only one login to that server. As such, I need a strategy to ensure that either one of these two are active and not both.
Currently, I store a value in LocalSettings which indicates if the UI is open or not. If the UI is closed, the BackgroundTask will do it's job as per normal. If the UI is open, the BackgroundTask will do nothing.
This works fine, except for when the BackgroundTask is in the middle of it's Run and the UI is launched. I need a way of cancelling this BackgroundTask immediately when the UI is launched, so that the UI can use the login to the remote server. Any tips or suggestions on how this may be achieved?

Thanks to Dave Smits for the solution.
In a nutshell:
Register for Cancellation event in Run method.
In the Cancellation event handler ensure connected sessions are disconnected.
When the OnLaunched event is raised in the App object, Unregister the
task which raises the task Cancellation event.

Related

Windows Phone 8, need to cancel Async task on back key press

I am new to WP8 Aync tasks and have a query-
In WP8, on backbutton press, since it basically pops the last screen from the stack, then why are the Async tasks not cancelled when back key is pressed?
Also, if I am using the HttpWebRequest response, is using Abort() the only way of cancelling the async tasks?
The reason is because Back is a UI concept but things like networking are typically decoupled from the UI. As an example, imagine the user hit "Save" in an app and that started a web request to send their data to the cloud. Then they hit the Back button to go back to the previous page; it would be unexpected for the web request to be aborted and the data to potentially not be saved.
If you have a need to stop tasks when your UI changes (eg, because of resource consumption concerns) then you can try to do that, but I have a vague recollection that Abort is a no-op for Windows Phone 8 (but I could be wrong).

Windows phone 8 push notification how to use ChannelUpdatedUri to detect channel updates

I would like to know what is the clean & correct way to notify my WP8 app that the ChannelUri has changed?
I read that i need to handle this in the ChannelUpdatedUri method. But i have a few queries here. Doesn't this require my app to be running all the time?
Second query is suppose i use ChannelUpdatedUri to listen for changes to the Uri. If there is a change will the app be notified as soon as it launches? Or will it be notified at a later time?
The trouble is if the app is not notified on launch then there is a possibility that it may re-register for a new channel uri before ChannelUpdatedUri is invoked. Isnt it?
Please help!
You'll only get notified of a change in the ChannelUri when you have the listener attached and the app running.
The best approach is to always check for the current Uri and pass it to the server when the app starts. You could leave the listener attached for the lifetime of your app and get a new one if it's lost while the app is running but in reality it's far more likely that the channel will be dropped/disconnected when the phone isn't in use.
There is, of course, the scenario where a channelUri may expire when the app isn't in use but the utility of the app is primarily around sending notifications. Obviously you need the user to restart the app to get a new connection but you can't tell them to restart the app.
This is a generic problem: how do you tell someone that you can't talk to them?
The reality of this situation is that if you really need to tell someone to launch the app again you'll need another way to do it.
Unfortunately, it's not possible to get an updated channelUri from a background agent, but you could query your backend to see if you need one and then raise a toast to the user to prompt them to reopen the app. Or you could just update the tile from the agent when this happens.
Alternatively you'll need to send them the prompt in another way (e.g. email or SMS?) but this is reliant on you capturing and securely storing these details.
There is no perfect solution to this scenario other than creating an app that the user opens regularly regardless of whether there are notifications or not and simply having the notifications as an extra feature.

Handle push notification on background

I am developing an application in windows phone which having push notification facility. I am able to receive push message at device.
If Application is not running , I am getting message at home screen on the click of that msg it starts that application. I want to perform some task on the basis of that Push Notification Message. But i am not able to handle that event.
Please help me to handle that EVENT if application is running or application is in background or application is not running .
Thanking You,
Which type of notification are you sending? Based on your question I assume you are referring to toast notifications.
When do you want to perform the task - before or after the user clicks on the message? If the application is not running, you can only do it after clicking the message.

Determine when user leaves WinJS app

I'm building some very basic analytics for in-house WinJS apps. Take this to mean that a 3rd-party analytics solution would both overkill and/or unworkable and/or against the 3rd-party providers terms of use as they generally disallow capturing personally identifiable information about the user, and in this case that is a business requirement.
The thing I'm trying to do is determine how much time is spent in multiple apps, and in areas within certain areas of the app. For this I obviously need to know when they enter and leave.
All the documentation I've found says to use the WinJS.Application.oncheckpoint event or the Windows.UI.WebUI.WebUIApplication.onsuspending event, which really seem to be two access points into the same basic concept. The problem is this doesn't accurately reflect when the user leaves the app! Suspend seems to happen only after the user has switched to another app, plus about 10 seconds ...... if the system feels like it.
If the user simply hits the Windows key to go out to the Start Screen and just sits there, the app continues to run indefinitely (calls to setInterval are able to affect state) even though the app cannot be seen!
I understand this is a bit of an edge case, but is there any more reliable way to tell when the user can't see the app, for lack of a better definition?
Notes:
I did look at the Cordova 2.7 code for Windows 8 and they are using the checkpoint event to drive the Cordova pause event.
App Visibility section on Application lifecycle seem to address this. This means registering for `msvisibilitychange' event, to know when user moved away and moved back to your app.
default.js:
document.addEventListener('msvisibilitychange', function ()
{
console.log('visibility changed');
console.log(document.visibilityState); // 'hidden' or 'visible'
});
In addition, suspending, resuming and activated events also needs to be handled.
default.js:
Windows.UI.WebUI.WebUIApplication.onsuspending = function ()
{
console.log('suspending');
}
Windows.UI.WebUI.WebUIApplication.onresuming= function ()
{
console.log('resuming');
}
Needless to say, that nuance of ordering, and/or event being absent cases needs to be handled. For example - if the user moves away and comes back quickly, visibilitychange event will be received. whereas if user does not come back suspending event may come after some time. if the app is not terminated, it may be followed by resuming event. otherwise, activated event.
regards spending time on specific pages, page ready and unload method should work. unload() will not get called if the app is suspended or terminated.
https://developer.mozilla.org/en-US/docs/Web/Reference/Events/visibilitychange
Use the visibility change event to recognize when the user can no longer see the app.

Windows Store reject the submitted app

We have submitted our app today over Windows Store.
Now, We found one change, so is there anyway to reject previous build and submit the new build? It is submitted today morning only, so it may not be in testing cycle. So, We want to apply this change if possible.
Do we have to wait for app submission status?
Yes, in the Dev Center portal, you can resubmit your application while the previous one is still being reviewed. You can as well view the current review status of your app in the portal
The certification process starts again at the beginning though.
You can manage your app submissions here : https://appdev.microsoft.com/StorePortals/en-US/Home/Index