Reputation: 446
I am doing an app that shows toast notifications, but if the user have the app opened any notification has to be sent.
If I want to avoid show new notifications while de app is opened I should know if the app is being used by the user.
How can I know if the app is opened in Wp 8.1? (Not running at background, I mean being used by the user in this moment).
Thank you.
Upvotes: 0
Views: 35
Reputation: 3362
If you're displaying your notification from a background task just add a flag to the local storage that indicates whether your app is open or not:
LocalSettings.Values.Add("IsAppOpen", true);
Now all you have to do is to set that flag to true or false whenever your app is:
You can use the standard handlers:
Application.Current.Resuming += new EventHandler<Object>(App_Resuming);
Application.Current.Suspending += new SuspendingEventHandler(App_Suspending);
OnLaunched / OnActivated should already be implemented in your App.xaml.cs. In all those handlers set your flag to the corresponding value:
LocalSettings.Values["IsAppOpen"] = true; // Or false, when the app is suspended
You can read the flag from your background process and stop displaying notifications.
Upvotes: 1