Getting network change notification on windows phone
I am currently stuck with my application over getting network change notification. I have a scenario in my app where in if user has no internet connectivity, commands needs to be sent to the server keeps on getting queued and once network is available all the queued commands are sent to the server. Now i want to be notified by the system when network is available.
What i tried.?
Following [URL="http://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkinterface.getisnetworkavailable%28v=VS.95%29.aspx"]this[/URL] post i registered a network changed listener but it works only when user is on that particular screen where i have added the listener. I have lot of screens in my app. Do i need to add network change listener on all the screens. Isn't there a way to just write logic at one place and get notified about network change on all the screens.
Re: Getting network change notification on windows phone
If you are using the [URL="http://en.wikipedia.org/wiki/Model_View_ViewModel"]MVVM[/URL] pattern in your app, registered the event in the View-Model.
Otherwise try to register the event in App.xaml.cs (I didn't try it yet)
Where are your queued commands being saved ?
Re: Getting network change notification on windows phone
Hi Loukt, thanks for your response.
[QUOTE=Loukt;910924]If you are using the [URL="http://en.wikipedia.org/wiki/Model_View_ViewModel"]MVVM[/URL] pattern in your app, registered the event in the View-Model.[/QUOTE]
Yes i have followed MVVM design pattern. But almost every view has its own ViewModel so this solution wont do.
[QUOTE=Loukt;910924]Otherwise try to register the event in App.xaml.cs (I didn't try it yet)[/QUOTE]
Haven't tried with this yet. Will try it today and get back.:)
[QUOTE=Loukt;910924]Where are your queued commands being saved ?[/QUOTE]
The commands are stored in isolated storage.
Re: Getting network change notification on windows phone
I just figuered out [QUOTE]NetworkChange.NetworkAddressChanged += new NetworkAddressChangedEventHandler(OnNetworkChange);[/QUOTE] does not fires for network lost and network gain events.
I was conneted to a wifi netowk(mobile network was off) then i went to a place where i had no wifi in range i.e i was disconnected from the wifi but the NetworkChange event didin't fired. Then i again walked into wifi range, i get the network back but still NetworkChange didn't fired up. Is this happening because my IP is not changing. If yes then how do i get Notifications for network connected/ disconneted events on same IP.?
Re: Getting network change notification on windows phone
Yes OnNetworkChange will only fire up when you IP address change so If your on a wifi you get an IP adress (192.168.x.x) but that doesn't mean you have an Internet Connection.
Regarding the Wifi Range it should fireup since out of the wifi range you lose the ip adress once back you get it back.
You can put a DispatcherTimer that will try every 15 30sec or xx time to connect to the server and add a PeriodicAgent which will try send those commands to the server when internetconnection is available even if the application is closed.
Or just notify the user that there is no internet connection (the notification can be a simple warning marker [IMG]http://climat.meteofrance.com/img/icone-warning.png[/IMG] in the top right corner and when clicked it it will retry connect to the server.)
Re: Getting network change notification on windows phone
[QUOTE=Loukt;911021]Regarding the Wifi Range it should fireup since out of the wifi range you lose the ip adress once back you get it back. [/QUOTE]
I tried it with another device. Sadly doesn't works.:(
[QUOTE=Loukt;911021]You can put a DispatcherTimer that will try every 15 30sec or xx time to connect to the server and add a PeriodicAgent which will try send those commands to the server when internetconnection is available even if the application is closed.
Or just notify the user that there is no internet connection (the notification can be a simple warning marker [IMG]http://climat.meteofrance.com/img/icone-warning.png[/IMG] in the top right corner and when clicked it it will retry connect to the server.)[/QUOTE]
I used [B]DispatcherTimer[/B] and it works seamless. hope it wont cause any blocks in the UI.:) But using a [B]PeriodicAgent[/B] seems typical. [B]ResourceIntensiveTask[/B] is completely out of question in my case because of its need to have device pugged in to external power source.
Even there are lot of constraints on [B]PeriodicTask [/B]like 25 seconds execution time, battery saver mode etc. Will see what i can come up with.
Anyways thanks for the help Loukt.:)
Re: Getting network change notification on windows phone
Glad that the work around works for you.
Good luck
Re: Getting network change notification on windows phone
I came through a really nice implementation for network detection. [URL="http://www.codeproject.com/Articles/121737/Zune-Detection-and-Network-Awareness"]Network detector[/URL]
So all i did was included NetworkDetector class in my app. Created an instance of NetworkDetector in app.xaml and added a OnNetworkON handler on it.
NetworkDetector nd = NetworkDetector.Instance;
nd.OnNetworkON += new EventHandler<NetworkAvailableEventArgs>(nd_OnNetworkON);
nd.SetNetworkPolling(minutes,seconds,milliseconds);
======
void nd_OnNetworkON(object sender, NetworkAvailableEventArgs e)
{
//Your implementation when network is available.
}
Thanks and regards