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.
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With