Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get notified with MonoTouch about my application being closed/sent to background?

Although I think this is a fairly trivial question, I could not find any answers out there.

My question is:

Is there a way to get a notification in a MonoTouch iPhone application when my application is being closed or sent to background (by a user clicking the home button)?

I thought the WillTerminate override was good for this, but in the debugger, it is never called.

like image 483
Uwe Keim Avatar asked Sep 12 '11 14:09

Uwe Keim


People also ask

Do push notifications work when app is closed?

Let's start with Android. The Android OS is designed to listen for push messages and upon receiving one, wake up the appropriate Android app to handle the push message, regardless of whether the app is closed or not.

How do I show notifications in xamarin form?

Create the Android interface implementation. For the Xamarin. Forms application to send and receive notifications on Android, the application must provide an implementation of the INotificationManager interface.


1 Answers

There are two ways to get notified when an app goes to the background:

a. Override the appropriate method in your AppDelegate:

public override void DidEnterBackground(UIApplication application)
{
    // App entered background, do some light stuff here, 
    // there is not much time before getting suspended
}

b. Add notification observers through the NSNotificationCenter class:

 NSObject observer = NSNotificationCenter.DefaultCenter.AddObserver(
    UIApplication.DidEnterBackgroundNotification, 
    delegate(NSNotification ntf) {

            // Same as above
    });

You can use the NSObject object returned from the AddObserver method to remove the observer when you no longer need it:

NSNotificationCenter.DefaultCenter.RemoveObserver(observer);
like image 145
Dimitris Tavlikos Avatar answered Sep 23 '22 01:09

Dimitris Tavlikos