Search code examples
c#backgroundnotificationswindows-phone-7.1foreground

Toast Notification while app is on the FG


I have an app with BG agent implementation. If the BG agent identifies any up-gradation availability then it will notify the same to the user through a toast notification. I have one doubt regarding this. The scenario is something like this, if the app is exited and is running in the BG then the toast notification(TN) is displayed, everything is smooth and fine. But, if the app is running in the FG and also in the BG and BG has something to notify to the user, then TN is not displayed, though everything is logged in the log file nothing shows up on the UI of the device. Is this normal? or should something be done? Also, Is there a way to detect if my application is running in the FG or not? If so i don't want to start the BG task , Can this be implemented?

All answers and suggestions appreciated.

Thank you.


Solution

  • You may use Thread.CurrentThread.IsBackground to check if its in the background or foreground and do work accordingly. Also you may make use of Thread.CurrentThread.IsAlive according to your requirements. You may find useful information here: http://msdn.microsoft.com/en-us/library/h158zycw

    If the app is in background use Toast Notifications else use Raw Notifictions. You can learn about Raw Notifications from here: http://msdn.microsoft.com/en-us/library/windowsphone/develop/hh202977(v=vs.92).aspx

    So summarizing it, you may use toast notification when app is the BG and raw notifications when app is in the FG

    I haven't tried this, but i guess it should work if I am not wrong. Also there is a similar question on this here at How to detect if the main app is running from a background agent

    If the threading option works then it great, else you may also try using the application state property mentioned on http://www.windowsphonegeek.com/articles/WP7-Application-Lifecycle-and-Tombstoning to check and track the state of your app, i.e. if its BG or FG.