maui icon indicating copy to clipboard operation
maui copied to clipboard

[Enhancement] More Platform Specific Lifecycle Events

Open mattleibow opened this issue 4 years ago • 4 comments

    public class AppleLifecycle
    {
        readonly ILogger logger;
        public AppleLifecycle(ILogger<AppleLifecycle> logger) => this.logger = logger;


        readonly List<Func<string, Task>> handleEvents = new List<Func<string, Task>>();
        public IDisposable RegisterHandleEventsForBackgroundUrl(Func<string, Task> task)
        {
            this.handleEvents.Add(task);
            return Disposable.Create(() => this.handleEvents.Remove(task));
        }


        internal async void HandleEventsForBackgroundUrl(string sessionIdentifier, Action completionHandler)
        {
            foreach (var handler in this.handleEvents)
            {
                try
                {
                    await handler(sessionIdentifier);
                }
                catch (Exception ex)
                {
                    this.logger.LogError(ex, "HandleEventsForBackgroundUrl");
                }
            }
            completionHandler();
        }


        readonly List<(Action<NSData> OnSuccess, Action<NSError> OnError)> remoteReg = new List<(Action<NSData> Success, Action<NSError> Error)>();
        public IDisposable RegisterForRemoteNotificationToken(Action<NSData> onSuccess, Action<NSError> onError)
        {
            var tuple = (onSuccess, onError);
            this.remoteReg.Add(tuple);
            return Disposable.Create(() => this.remoteReg.Remove(tuple));
        }


        internal void RegisteredForRemoteNotifications(NSData deviceToken)
        {
            foreach (var reg in this.remoteReg)
            {
                try
                {
                    reg.OnSuccess(deviceToken);
                }
                catch (Exception ex)
                {
                    this.logger.LogError(ex, "RegisteredForRemoteNotifications");
                }
            }
        }


        internal void FailedToRegisterForRemoteNotifications(NSError error)
        {
            foreach (var reg in this.remoteReg)
            {
                try
                {
                    reg.OnError(error);
                }
                catch (Exception ex)
                {
                    this.logger.LogError(ex, "FailedToRegisterForRemoteNotifications");
                }
            }
        }


        readonly List<Func<NSDictionary, Task>> receiveReg = new List<Func<NSDictionary, Task>>();
        public IDisposable RegisterToReceiveRemoteNotifications(Func<NSDictionary, Task> task)
        {
            this.receiveReg.Add(task);
            return Disposable.Create(() => this.receiveReg.Add(task));
        }


        internal async void DidReceiveRemoteNotification(NSDictionary dictionary, Action<UIBackgroundFetchResult> completionHandler)
        {
            foreach (var reg in this.receiveReg)
            {
                try
                {
                    await reg.Invoke(dictionary);
                }
                catch (Exception ex)
                {
                    this.logger.LogError(ex, "DidReceiveRemoteNotification");
                }
            }
            completionHandler(UIBackgroundFetchResult.NewData);
        }
    }

mattleibow avatar May 10 '21 18:05 mattleibow

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

ghost avatar Aug 30 '22 13:08 ghost

HandleEventsForBackgroundUrl was used to manage background file downloads, and is still offered by Apple as the way to set up background file downloads: https://developer.apple.com/documentation/foundation/downloading-files-in-the-background

What is the suggested way to do background file downloads in MAUI?

jeremy-bridges avatar Nov 07 '25 21:11 jeremy-bridges

I have the same question. I do see IUIApplicationDelegate has an HandleEventsForBackgroundUrl autogenerated extension method delegate, but it crashes the app the moment you try and use it.

dotMorten avatar Dec 06 '25 01:12 dotMorten

I found I can get the callback by adding the following to the Maui App Delegate in my app:

        [Export("application:handleEventsForBackgroundURLSession:completionHandler:")]
        public virtual void HandleEventsForBackgroundUrlCompletion(UIApplication application, string sessionIdentifier, Action completionHandler)
        {
            completionHandler();
        }

dotMorten avatar Dec 09 '25 21:12 dotMorten