Xamarin Forms detect if app in foreground

Gordon S 501 Reputation points
2021-05-20T15:23:39.593+00:00

I am sending notifications to my app (using Amazon SNS & Firebase Cloud Messaging).

I am sending data notifications, such as:

{
  "GCM": "{ \"data\": { \"title\": \"Sample message title\", \"message\": \"Sample message for Android endpoints\" } }"
}

(this is being sent from Amazon SNS admin).

The notification is received to my app, in "OnMessageReceived()", which then creates / outputs a notification.

Is it possible to detect if the app is active / in the foreground, so that I can skip creating the notification (to the phone itself) and just show a message in the app instead? (Actually, the messages / notifications will be listed in the app - eventually - in a "notifications" or "messages" area)

This is for Android, but I would also like to do the same thing with iOS.

Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2021-05-21T02:41:02.187+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    Xamarin Forms detect if app in foreground

    Here is achieved by lifecycle to detect app running from background to foreground.

    For Forms, you can detect the app from background to foreground by OnResume() method in the App.xaml.cs.

    If you want to detect the app from background to foreground in different platform.

    For Android: Open the MainActivity.cs, add the OnResume method.

       public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity  
       {  
       protected override void OnCreate (Bundle bundle)  
       {  
       base.OnCreate (bundle);  
       global::Xamarin.Forms.Forms.Init (this, bundle);  
       Xamarin.FormsMaps.Init (this, bundle);  
       LoadApplication (new App ());  
       }  
         
               protected override void OnResume()  
               {  
                   base.OnResume();  
         
         
               }  
           }  
    

    For iOS

    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);  
           }  
       }  
    

    If you want to achieve it by dependenceService to get the if app is foreground directly,

    For Android.

       public bool foregrounded()  
       {  
       ActivityManager.RunningAppProcessInfo appProcessInfo = new ActivityManager.RunningAppProcessInfo();  
       ActivityManager.GetMyMemoryState(appProcessInfo);  
       return (appProcessInfo.Importance == Importance.Foreground || appProcessInfo.Importance == Importance.Visible);  
                }  
    

    For iOS

       public bool foregrounded()  
       {  
       bool isInForeground = UIApplication.SharedApplication.ApplicationState == UIApplicationState.Active;  
         
       return isInForeground;  
       }  
    

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    2 people found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.