Geofence in the Background Windows Phone 8.1 (WinRT) - windows

Issue
I'm trying to trigger a BackgroundTask when a Geofence Event (Enter / Exit) occurs in WP8.1 (WinRT). I've written a sample application to try to get it working, but can't seem to be able to do so.
So far, these are the steps I've taken to try to get Geofences working in the background:
Check for Location Capabilities
Create + Register a Geofence
Create + Register a BackgroundTask that listens for LocationTrigger(LocationTriggerType.Geofence);
In my background task, trigger a simple popup notification
Things I have done to Troubleshoot
I have enabled in my app.manifest:
Toast Capable => Yes
Capabilities: Location, Internet(Client &
Server)
Declarations: BackgroundTasks (Location). EntryPoint = BackgroundTask.GeofenceBackgroundTask
My background task is located in a separate project, titled BackgroundTask. It is a WindowsRT Component and contains one class GeofenceBackgroundTask.
Sample Project
The code for the project can be found at this [link](https://github.com/kiangtengl/GeofenceSample):
How To Test
Run the code in the emulator
Set Location to to: Latitude = 01.3369, Longitude = 103.7364
Click the Register Geofence + BackgroundTasks button
Exit the app (press the home button)
Change the current location to anywhere 100m away from the location you set previously. A notification should pop out.
Project Code:
Check for Location Capabilities
public static async Task GetLocationCapabilities()
{
try
{
var geolocator = new Geolocator();
await geolocator.GetGeopositionAsync();
var backgroundAccessStatus = await BackgroundExecutionManager.RequestAccessAsync();
Debug.WriteLine("background access status" + backgroundAccessStatus);
}
catch (UnauthorizedAccessException e)
{
Debug.WriteLine(e);
}
catch (TaskCanceledException e)
{
Debug.WriteLine(e);
}
}
Create Geofence
public static void CreateGeofence(BasicGeoposition position, double radius, string id = "default")
{
// The Geofence is a circular area centered at (latitude, longitude) point, with the
// radius in meter.
var geocircle = new Geocircle(position, radius);
// Sets the events that we want to handle: in this case, the entrace and the exit
// from an area of intereset.
var mask = MonitoredGeofenceStates.Entered | MonitoredGeofenceStates.Exited;
// Specifies for how much time the user must have entered/exited the area before
// receiving the notification.
var dwellTime = TimeSpan.FromSeconds(1);
// Creates the Geofence and adds it to the GeofenceMonitor.
var geofence = new Geofence(id, geocircle, mask, false, dwellTime);
try
{
GeofenceMonitor.Current.Geofences.Add(geofence);
}
catch (Exception e)
{
Debug.WriteLine(e);
// geofence already added to system
}
}
Register Background Task
public static async Task RegisterBackgroundTask()
{
try
{
// Create a new background task builder
var geofenceTaskBuilder = new BackgroundTaskBuilder()
{
Name = GeofenceBackgroundTaskName,
TaskEntryPoint = "BackgroundTask.GeofenceBackgroundTask"
};
// Create a new location trigger
var trigger = new LocationTrigger(LocationTriggerType.Geofence);
// Associate the location trigger with the background task builder
geofenceTaskBuilder.SetTrigger(trigger);
var geofenceTask = geofenceTaskBuilder.Register();
// Associate an event handler with the new background task
geofenceTask.Completed += (sender, e) =>
{
try
{
e.CheckResult();
}
catch(Exception error)
{
Debug.WriteLine(error);
}
};
}
catch(Exception e)
{
// Background task probably exists
Debug.WriteLine(e);
}
}
BackgroundTask Code to Trigger Toast
namespace BackgroundTask
{
public sealed class GeofenceBackgroundTask : IBackgroundTask
{
public void Run(IBackgroundTaskInstance taskInstance)
{
var toastTemplate = ToastTemplateType.ToastText02;
var toastXML = ToastNotificationManager.GetTemplateContent(toastTemplate);
var textElements = toastXML.GetElementsByTagName("text");
textElements[0].AppendChild(toastXML.CreateTextNode("You have left!"));
var toast = new ToastNotification(toastXML);
ToastNotificationManager.CreateToastNotifier().Show(toast);
}
}
}

I've discovered that the above code sample, as well as the above code works. The problem that I was facing was that Windows Phone 8.1 does not automatically trigger a Geofence event. You have to wait a certain amount of time <5 mins before the BackgroundTask is triggered.
This applies to Geofencing in the foreground as well.

I'm busy with the same stuff, and I also noticed this behaviour, but for me its 2 mins.
Unfortunately it always triggers after 2 min, even, if there was no change in location and still inside the fence..

Related

Keep task working when the app is in the background

I'm coding a map tracking app on Xamarin forms. I'm using async tasks(with Device.StartTimer) to keep track of the location and another task for counter timer, but when I need to open another app, like a music app or whatever the app can't keep track while it is in the background. All tasks are stopping when I send the app in the background. When I start the app again, the tasks are not continued.
I just need to keep working while app running.
How could this be done?
You should use background task in this case, which is running outside the life cycle of the application. You can find more information here Xamarin background tasks
Due to Background Execution Limits in Android 8.0 or later, Normal service will be killed when in the Background.
In the Android 8.0 or later, I suggest you to achieve a foreground service to achieve that( receive higher priority than a "regular" service and a foreground service must provide a Notification that Android will display as long as the service is running).
You can use dependence service to open a foreground service in xamarin forms.
IService.cs create a interface for android to start service.
public interface IService
{
void Start();
}
Then achieved DependentService to start a Foreground Service.
DependentService.cs
[assembly: Xamarin.Forms.Dependency(typeof(DependentService))]
namespace TabGuesture.Droid
{
[Service]
public class DependentService : Service, IService
{
public void Start()
{
var intent = new Intent(Android.App.Application.Context,
typeof(DependentService));
if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.O)
{
Android.App.Application.Context.StartForegroundService(intent);
}
else
{
Android.App.Application.Context.StartService(intent);
}
}
public override IBinder OnBind(Intent intent)
{
return null;
}
public const int SERVICE_RUNNING_NOTIFICATION_ID = 10000;
public override StartCommandResult OnStartCommand(Intent intent,
StartCommandFlags flags, int startId)
{
// From shared code or in your PCL
CreateNotificationChannel();
string messageBody = "service starting";
var notification = new Notification.Builder(this, "10111")
.SetContentTitle(Resources.GetString(Resource.String.app_name))
.SetContentText(messageBody)
.SetSmallIcon(Resource.Drawable.main)
.SetOngoing(true)
.Build();
StartForeground(SERVICE_RUNNING_NOTIFICATION_ID, notification);
//do you work
return StartCommandResult.Sticky;
}
void CreateNotificationChannel()
{
if (Build.VERSION.SdkInt < BuildVersionCodes.O)
{
// Notification channels are new in API 26 (and not a part of the
// support library). There is no need to create a notification
// channel on older versions of Android.
return;
}
var channelName = Resources.GetString(Resource.String.channel_name);
var channelDescription = GetString(Resource.String.channel_description);
var channel = new NotificationChannel("10111", channelName, NotificationImportance.Default)
{
Description = channelDescription
};
var notificationManager = (NotificationManager)GetSystemService(NotificationService);
notificationManager.CreateNotificationChannel(channel);
}
}
}
Here is a similar thread about your needs.
How to create service doing work at period time in Xamarin.Forms?
For IOS background Tasks. you can refer to this thread

How to properly implement location changes listener to xamarin forms?

I have a listener that captures the location every 10 seconds or 100 meters or so. I am using
xam.plugin.geolocator
to implement the listener. My problem is the location listener is not working(meaning the changes in location were not capturing or saved in the location cache) when my application is minimized or the application is opened but the phone is locked.
Here is my code:
async Task StartListening()
{
if (!CrossGeolocator.Current.IsListening)
{
var defaultgpsaccuracy = Convert.ToDouble(Preferences.Get("gpsaccuracy", String.Empty, "private_prefs"));
await CrossGeolocator.Current.StartListeningAsync(TimeSpan.FromSeconds(10), defaultgpsaccuracy, false, new Plugin.Geolocator.Abstractions.ListenerSettings
{
ActivityType = Plugin.Geolocator.Abstractions.ActivityType.Other,
AllowBackgroundUpdates = true,
DeferLocationUpdates = true,
DeferralDistanceMeters = 1,
DeferralTime = TimeSpan.FromSeconds(1),
ListenForSignificantChanges = true,
PauseLocationUpdatesAutomatically = false
});
}
}
I place this code in the first view/page of my application in my login.xaml.cs
Here are my questions:
How can I implement the listener properly so that when the application minimized or the phone/device is locked it still captures the changes of location?
What is the best GPS settings I need to capture the changes in location faster and accurately? Right now, my current settings are capturing the location every 10 seconds or 100 meters.
First you need to init StartListening then create event handlers for position changes and error handling
public Position CurrentPosition { get; set; }
public event EventHandler PositionChanged;
Don't forget to init it in your constructor :
CurrentPosition = new Position();
await CrossGeolocator.Current.StartListeningAsync(TimeSpan.FromSeconds(20), 10, true);
CrossGeolocator.Current.PositionChanged += PositionChanging;
CrossGeolocator.Current.PositionError += PositionError;
Functions :
`private void PositionChanging(object sender, PositionEventArgs e)
{
CurrentPosition = e.Position;
if (PositionChanged != null)
{
PositionChanged(this, null);
}
}
private void PositionError(object sender, PositionErrorEventArgs e)
{
Debug.WriteLine(e.Error);
}`
You can now call PositionChanged when ever you want the latest position
Don't forget to stop listening :
`public async Task StopListeningAsync()
{
if (!CrossGeolocator.Current.IsListening)
return;
await CrossGeolocator.Current.StopListeningAsync();
CrossGeolocator.Current.PositionChanged -= PositionChanging;
CrossGeolocator.Current.PositionError -= PositionError;
}`

Xamarin iOS RefreshControl is stuck

I have a problem with RefreshControl... I have this code:
In ViewDidLoad() I call method InitializeRefreshControl();
private void InitializeRefreshControl()
{
if (UIDevice.CurrentDevice.CheckSystemVersion(6, 0))
{
//UIRefreshControl iOS6
ordersCollectionView.RefreshControl = new UIRefreshControl();
ordersCollectionView.RefreshControl.AttributedTitle = new NSAttributedString("Pull To Refresh",
new UIStringAttributes()
{
ForegroundColor = UIColor.Red,
KerningAdjustment = 3
});
ordersCollectionView.RefreshControl.ValueChanged += HandleValueChanged;
}
else
{
// old style refresh button and no PassKit for older iOS
NavigationItem.SetRightBarButtonItem(new UIBarButtonItem(UIBarButtonSystemItem.Refresh), false);
NavigationItem.RightBarButtonItem.Clicked += (sender, e) => { Refresh(); };
}
}
HandleValueChange method and Refresh merhod is here:
private void HandleValueChanged(object sender, EventArgs e)
{
ordersCollectionView.RefreshControl.BeginRefreshing();
ordersCollectionView.RefreshControl.AttributedTitle = new NSAttributedString("Refreshing",
new UIStringAttributes()
{
ForegroundColor = UIColor.Blue,
KerningAdjustment = 5
});
Refresh();
ordersCollectionView.RefreshControl.EndRefreshing();
}
private void Refresh()
{
var viewModel = (OrdersViewModel)DataContext;
viewModel.OnReloadData();
}
My problem is when I pull down collectionVIew so Refresh loading is displayed but is stuck no loading effect and still with text "Pull to refresh". When method Refresh end so for 0,1ms is showing loading effect and text "Refreshing" but not before method Refresh... Someone know how solve this problem? Thanks for answer.
It looks like the issue is related to the Refresh(); method being synchronous. You'll need to make this operation happen in the background so that the UI thread is free to provide the animation for the RefreshControl. For example:
private async void HandleValueChanged(object sender, EventArgs e)
{
ordersCollectionView.RefreshControl.BeginRefreshing();
ordersCollectionView.RefreshControl.AttributedTitle = new NSAttributedString("Refreshing",
new UIStringAttributes()
{
ForegroundColor = UIColor.Blue,
KerningAdjustment = 5
});
// await a Task so that operation is done in the background
await Refresh();
ordersCollectionView.RefreshControl.EndRefreshing();
}
// Marked async and Task returning
private async Task Refresh()
{
var viewModel = (OrdersViewModel)DataContext;
// Need to update this method to be a Task returning, async method.
await viewModel.OnReloadData();
}
The above code refactors what you had to use async/await and Tasks. You may need to refactor some more of your code to make that work, including the OnReloadData() method.
There are lots of resources for getting started with Tasks, async and await. I can start you off with this reference from the Xamarin blog.

How to resume the application and open a specific page with a push notification in Xamarin.forms

I'm currently working on a Xamarin application working both on iOS and Android, but the problem I'm going to explain only concerns the Android application (this is not yet implemented in the iOS app).
Actually, when I receive a given push notification, I need to open a specific page in my application. It works very well if the application is open when the push notification is received, but the app crashes if my app is closed or run in background.
Well, when I receive the notification, I end up in the method called "OnShouldOpenCommand" :
private void OnShouldOpenCommand(string commandId)
{
NotifyNewCommand(AppResources.AppName, AppResources.CommandNotificationText, commandId);
Device.BeginInvokeOnMainThread(() =>
{
try
{
App.MasterDetailPage.Detail = new NavigationPage(new CommandAcceptancePage(commandId))
{
BarBackgroundColor = Color.FromHex("1e1d1d")
};
App.MasterDetailPage.NavigationStack.Push(((NavigationPage)(App.MasterDetailPage.Detail)).CurrentPage);
}
catch(Exception e)
{
Log.Debug("PushAsync", "Unable to push CommandAcceptancePage : "+ex.Message);
}
});
}
private void NotifyNewCommand(string Title,string Description, string commandId)
{
var intent = new Intent(this, typeof(MainActivity));
if (!String.IsNullOrEmpty(commandId))
{
intent.PutExtra("CommandId", commandId);
}
intent.AddFlags(ActivityFlags.ClearTop);
var pendingIntent = PendingIntent.GetActivity(this, 0, intent, 0);
var notificationBuilder = new Notification.Builder(this)
.SetSmallIcon(Resource.Drawable.icon)
.SetContentTitle("Kluox")
.SetContentText(Description)
.SetAutoCancel(true)
.SetContentIntent(pendingIntent);
Notification notification = notificationBuilder.Build();
var notificationManager = (NotificationManager)GetSystemService(Context.NotificationService);
notificationManager.Notify(0, notification);
}
And the code
App.MasterDetailPage.Detail = new NavigationPage(new CommandAcceptancePage(commandId))
{
BarBackgroundColor = Color.FromHex("1e1d1d")
};
is generating an exception of type :
Java.Lang.IllegalStateException: Can not perform this action after
onSaveInstanceState
So well, I suppose I can't access "App" and redirect to another page if my application is not running in foreground. Well, this is when I receive the push notification an not when I click on it. But well, I do not intend to reopen my app by doing this.
Because afther that, when I click on the push notification called Kluox (and this is supposed to reopen my app), the app crashes and I really don't know why, I don't know where to put breakpoints to be able to debug because Visual Studio just tells me "An unhandled exception occured.".
Could anyone help me ? If you need any piece of code, you can just ask me, I'll edit my message and give you any information you need !
EDIT 1 : Here is the code of my OnCreate method :
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;
base.OnCreate(bundle);
var info = Intent.Extras?.GetString("CommandId", "");
global::Xamarin.Forms.Forms.Init(this, bundle);
if (String.IsNullOrEmpty(info))
{
LoadApplication(new App());
}
else
{
LoadApplication(new App(info));
}
if (instance == null)
{
instance = this;
RegisterWithGCM();
}
else
{
instance = this;
}
}
After overriding all the methods of MainActivity, I finally found the cause of the crash : the method OnDestroy was called twice, and threw a IllegalStateException because the activity was already destroyed. I found this workaround :
protected override void OnDestroy()
{
try
{
base.OnDestroy();
}
catch (Java.Lang.IllegalStateException ex)
{
Log.Debug("MainActivity.OnDestroy", ex, "The activity was destroyed twice");
}
}
And the exception is simply logged, the application can open and be used without problems.
I'll edit this answer when the redirection works too.
EDIT : how to redirect to a page
First, we needed to register for the MessagingCenter, in the constructor
public static MyPackage.Model.Command CurrentCommand { get; set; }
public App()
{
InitializeComponent();
MainPage = new ContentPage();
MessagingCenter.Subscribe<object, DataLib.Model.Command>(this, "Command", (sender, arg) => {
try
{
CurrentCommand = arg;
}
catch(Exception ex)
{
CurrentCommand = null;
}
});
}
And send the message when we get the push notification :
private void OnMessage(string serializedCommand)
{
//stuff happens
MessagingCenter.Send<object, MyPackage.Model.Command>(this, "Command", command);
}
Finally, when we get the OnStart() of App.Xaml.cs
if (CurrentCommand != null)
{
App.MasterDetailPage.Detail = new NavigationPage(new CommandAcceptancePage(CurrentCommand, service))
{
BarBackgroundColor = Color.FromHex("1e1d1d")
};
}
For now, it seems to do the trick ! More debugging will follow, but the code seems to work. Thanks a VERY lot to #BraveHeart for their help !
well luckily for you I was there few days ago and lost a lot of hair till I got it working in Android (and still in the strugle for iOS).
When you kill your app and instantiate it again form the icon or from the notification in both cases you will go to the main activity .
If we want to take some information in the main activity from the notification that instantiated it we do it like this in OnCreate():
var info = Intent.Extras?.GetString("info", "");
Now in your case I would add extra information to the notification showing that which View/Page this notification is about, something like the name of it for example)
This extra piece of information you can pass it to the constructor of the App before you load it.
In the constructor of the app you can check if there are extra info or not , if not that means to start the app's mainPage is the default MainPage, otherwise it is a certain page.

Xamarin form Geolocation task cancelled exception

I am working on Xamarin form app with andorid, UWP and Windows 8 project. I am using Geolocation plugin created by Jamesmontemagno to get the current device location. It is working fine in windows 8 and UWP but whenever I am trying to run it against the android device I keep getting task cancelled exception. I have checked all the permissions that are required as per suggestion but still no luck. My code to access location is below
protected override void OnAppearing()
{
var locator = CrossGeolocator.Current;
locator.DesiredAccuracy = 100; //100 is new default
if (locator.IsGeolocationAvailable && locator.IsGeolocationEnabled)
{
try
{
var position = locator.GetPositionAsync(timeoutMilliseconds: 60000).Result;
//var pp = helper.Setting.Location;
var Latitude = position.Latitude;
var Longitude = position.Longitude;
}
catch(Exception ex)
{
var exc = ex;
}
}
}
Below is an image for my settings for android manifest
For anyone else who gets a timeout even with the await, only on Android, and even though the device's Google Maps app works fine, you are probably running into this bug which only happens on certain Android devices, but quite a few of them at that.
The issue is an old one that Google has never fixed. The solution, and one possible reason the Google Maps app works fine, is to use Google Play Services' fused location provider.
Currently the Geolocator Plugin just uses the regular Android Location Provider, but James has mentioned that he would like to use the Fused provider at some point. I have yet to try the fused provider myself though.
Try using the await keyword like it is used in the original code:
try
{
var locator = CrossGeolocator.Current;
locator.DesiredAccuracy = 50;
var position = await locator.GetPositionAsync (timeoutMilliseconds: 10000);
Console.WriteLine ("Position Status: {0}", position.Timestamp);
Console.WriteLine ("Position Latitude: {0}", position.Latitude);
Console.WriteLine ("Position Longitude: {0}", position.Longitude);
}
catch(Exception ex)
{
Debug.WriteLine("Unable to get location, may need to increase timeout: " + ex);
}
This should take care that there are no race condition and therefore TaskCancellationException.
Thanks to #Radinator below is the working solution.
protected async override void OnAppearing()
{
var locator = CrossGeolocator.Current;
locator.DesiredAccuracy = 100; //100 is new default
if (locator.IsGeolocationAvailable && locator.IsGeolocationEnabled)
{
try
{
await SetLocation();
}
catch (Exception ex)
{
var exc = ex;
}
}
}
private async Task SetLocation()
{
var locator = CrossGeolocator.Current;
locator.DesiredAccuracy = 100; //100 is new default
if (locator.IsGeolocationAvailable && locator.IsGeolocationEnabled)
{
try
{
var position = await locator.GetPositionAsync(timeoutMilliseconds: 60000);
var Latitude = position.Latitude;
var Longitude = position.Longitude;
}
catch (Exception ex)
{
//log ex;
throw ex;
}
}
}
Faced 'Task killed' issue with v3.0.4. The following worked for me:
Uninstall the app
Update Geolocator to prerelease 4.0

Resources