xamarin.forms detect ios app entering foreground from background - xamarin.forms

I am creating app in Xamarin.Forms shared proeject. I want to detect app when it comes from background
currently, I have implemented OnActivated method in AppDelegate but this method is also called when we open control center in iphone. I want my app to reconnect with my server when app comes to foreground from background and show message accordingly but this message is also shown when I swipe up and open control center.
In native Xamarin.iOS app, there is a method WillEnterForegroundNotification but this method is not available here.
Is there any method that's called only if app comes from background ?

in App.xaml.cs
protected override void OnResume()

In native Xamarin.iOS app, there is a method
WillEnterForegroundNotification but this method is not available here.
You can access WillEnterForegroundNotification method in Xamarin.forms project, just override it in AppDelegate.cs:
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
//
// This method is invoked when the application has loaded and is ready to run. In this
// method you should instantiate the window, load the UI into it and then make the window
// visible.
//
// You have 17 seconds to return from this method, or iOS will terminate your application.
//
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
LoadApplication(new App());
return base.FinishedLaunching(app, options);
}
public override void WillEnterForeground(UIApplication uiApplication)
{
//handle your event here
base.WillEnterForeground(uiApplication);
}
}

Related

xamarin forms android, Font asset not found sans-serif-medium when switching to AppShell

I am attempting to code an App in Xamarin Forms.
Upon startup, if the app is logged in the the user will be taken to the main shell with flyouts etc. If they are not logged in, then they will be taken to the signup/login page.
I have the following code in my App.xaml.cs
public App()
{
Instance = this;
InitializeComponent();
AssessStartupPage();
}
public void AssessStartupPage()
{
if (App.LoggedInUser == null)
{
SwitchToSignupPage_OTP();
return;
}
SwitchToShell();
}
public void SwitchToSignupPage_OTP() => SwitchMainView(new SignupPage_OTP());
public void SwitchToShell() => SwitchMainView(new AppShell());
When I attempt to switch to the shell from another Page (by calling App.Instance.SwitchToShell) the following exception is thrown:
Java.Lang.RuntimeException
Font asset not found sans-serif-medium
However if I change the AssessStartupPage() function which is called on startup to this:
public void AssessStartupPage()
{
SwitchToShell();
return;
}
The shell loads correctly at startup. However I need to be able to have the Signup page load at start if the user is not signed in and therefore need to be able to switch to the shell once signup is complete.
I have updated all the xamarin forms and xamarin essential libraries to the latest stable version but nothing seems to help.
The problem doesn't occur when I debug the app in UWP mode.

FirebasePushNotificationManager.Initialize freeze my app in iOS but no error appears

I am using Xamarin with C#.
FirebasePushNotificationManager.Initialize(options, true);
^ this code works fine in my Android Application.cs file, I can receive notifications in Android. However, when I add this code to AppDelegate.cs in iOS, it freezes my app, meaning there's no response when I click on button or input field. But if I remove this line of code, my iOS app works.
I've Googled a lot but didn't get any luck, can someone please help me?
Following is my AppDelegate.cs code:
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
ServicePointManager
.ServerCertificateValidationCallback +=
(sender, cert, chain, sslPolicyErrors) => true;
global::Xamarin.Forms.Forms.Init();
var iosClientHandler = new NSUrlSessionHandler();
Services.HttpClientService.HttpClientHandler = iosClientHandler;
LoadApplication(new App());
//This line is causing the issue
FirebasePushNotificationManager.Initialize(options, true);
return base.FinishedLaunching(app, options);
}
public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
FirebasePushNotificationManager.DidRegisterRemoteNotifications(deviceToken);
}
public override void FailedToRegisterForRemoteNotifications(UIApplication application, NSError error)
{
FirebasePushNotificationManager.RemoteNotificationRegistrationFailed(error);
}
// To receive notifications in foreground on iOS 9 and below
// To receive notifications in background in any iOS version
public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
{
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired 'till the user taps on the notification launching the application.
// If you disable method swizzling, you'll need to call this method.
// This lets FCM track message delivery and analytics, which is performed
// automatically with method swizzling enabled
FirebasePushNotificationManager.DidReceiveMessage(userInfo);
// Do your magic to handle the notification data
System.Console.WriteLine(userInfo);
completionHandler(UIBackgroundFetchResult.NewData);
}
}

How to navigate to page when on resume is called in app.xaml.cs for mvvm in xamarin forms app

I have an issue with navigation. When I click home screen button of device and get back to app I get app homescreen instead of pin page. Ideally it should show pin page and its working fine with back button of device.
OnStart() method has navigationasync but the same is not working with OnResume() method.
Do I have to go to each of the Platform project cs file and add the navigation there like for Android OnRestart()/OnResume() method?
If anyone knows the solution please let me know
Most commonly when writing your Xamarin Application with Prism you will have something like:
protected override void OnInitialized()
{
NavigationService.NavigateAsync("SomePage");
}
OnInitialized is called each time the App's ctor is invoked. This is an important consideration here because this means that any time that the native platform tombstones the app in the background or otherwise refreshes the app by calling OnCreate in your MainActivity or FinishedLaunching in your AppDelegate, then OnInitialized will be invoked resetting your App's Navigation stack to SomePage.
You can however override the OnStart/OnResume in PrismApplication and use whatever business logic you need to determine where to navigate and how you might want to restore your application.
public override void OnStart()
{
NavigationService.NavigateAsync("MainPage");
}
public override void OnResume()
{
if(someCondition)
{
NavigationService.NavigateAsync("SomePage");
}
else
{
NavigationService.NavigateAsync("AnotherPage");
}
}

Xamarin forms IOS App aksing for RootviewController

I am working on Xamarin Forms on VS 2017. Whenever I am running My IOS app it is asking for RootViewController but my other sample app is working perfectly without any exception.
My Other app has a default Implementation of Finishedlaunching. AppDelegate is inherited by FormsApplicationDelegate class
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
LoadApplication(new App());
return base.FinishedLaunching(app, options);
}
My main app works only after I have updated the Finishedlaunching Method. AppDelegate is inherited by UIApplicationDelegate class
UIWindow window;
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
Rg.Plugins.Popup.Popup.Init();
window = new UIWindow(UIScreen.MainScreen.Bounds);
window.RootViewController = new Login().CreateViewController();
window.MakeKeyAndVisible();
return true;
}
My question is when my other app is working fine with default code why my second application is not working and I have to add RootViewController.
Please let me know if I am missing anything.

How to leverage IApplicationLifecycleAware

Can someone point me to an example of how to leverage IApplicationLifecycleAware for Prism and Xamarin.Forms? I have a view model that implements it but OnResume and OnSleep never fire (although it fires within my App.xaml.cs).
To use IApplicationLifecycleAware you simply need to implement it on any ViewModel you wish to use it, and add the code to the OnSleep and OnResume methods that you require.
It is very important though that if you implement an override of these methods in your App class that you include the base execution otherwise the PrismApplication cannot execute the IApplicationLifecycleAware methods on the ViewModel of your current page.
public partial class App : PrismApplication
{
protected override void OnSleep()
{
// Handle IApplicationLifecycle
base.OnSleep();
// Handle when your app sleeps
}
protected override void OnResume()
{
// Handle IApplicationLifecycle
base.OnResume();
// Handle when your app resumes
}
}

Resources