Add handler to manage when user taps on local notification

Cesar.TomatisCesar.Tomatis ✭✭USMember ✭✭

Hi everybody,

I need to add a handler to manage what to do when a user clicks on a local notification. This is the code I have so far:

// Create action
var actionID = "check";
var title = "Check";
var action = UNNotificationAction.FromIdentifier(actionID, title, UNNotificationActionOptions.Foreground);

// Create category
var categoryID = "notification";
var actions = new UNNotificationAction[] { action };
var intentIDs = new string[] { };
//var categoryOptions = new UNNotificationCategoryOptions[] { };
var category = UNNotificationCategory.FromIdentifier(categoryID, actions, intentIDs, UNNotificationCategoryOptions.None);

// Register category
var categories = new UNNotificationCategory[] { category };
UNUserNotificationCenter.Current.SetNotificationCategories(new NSSet<UNNotificationCategory>(categories));

// Create content
var content = new UNMutableNotificationContent();
content.Title = "Title";
content.Subtitle = "Subtitle";
content.Body = "Body";
content.Badge = 1;
content.CategoryIdentifier = categoryID;
content.Sound = UNNotificationSound.Default;

// Fire trigger in one seconds
var trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(1, false);

var requestID = "sampleRequest";
var request = UNNotificationRequest.FromIdentifier(requestID, content, trigger);

UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound, (a, err) => {
    if (err != null) {
        // Report error
        System.Console.WriteLine("Error: {0}", err);
    } else {
        // Report Success
        System.Console.WriteLine("Notification Scheduled: {0}", request);
    }
});

UNUserNotificationCenter.Current.AddNotificationRequest(request, (err) => {
    if (err != null) {
        // Report error
        System.Console.WriteLine("Error: {0}", err);
    } else {
        // Report Success
        System.Console.WriteLine("Notification Scheduled: {0}", request);
    }
});

Does anyone knows what I'm missing to manage user clicking on notification? I need to do different things according to app state, that's why I need to add that. Thanks in advance!

Best Answers

  • Cesar.TomatisCesar.Tomatis ✭✭ US ✭✭
    Accepted Answer

    @TedRogers maybe I didn't made myself clear enough, I'm not using UIAlertController, I'm using UNMutableNotificationContent.

    I ended up finding a solution, I neede to set the UNUserNotificationCenter.Current.Delegate, see below for the solution:

    in AppDelegate.cs:

            public override bool FinishedLaunching(UIApplication app, NSDictionary options) {
                RegisterForNotifications();
                ScheduleNotification();
                //more code
                return base.FinishedLaunching(app, options);
            }
    
            void RegisterForNotifications() {
                // Create action
                var actionID = "check";
                var title = "Check";
                var action = UNNotificationAction.FromIdentifier(actionID, title, UNNotificationActionOptions.Foreground);
    
                // Create category
                categoryID = "notification";
                var actions = new UNNotificationAction[] { action };
                var intentIDs = new string[] { };
                //var categoryOptions = new UNNotificationCategoryOptions[] { };
                var category = UNNotificationCategory.FromIdentifier(categoryID, actions, intentIDs, UNNotificationCategoryOptions.None);
    
                // Register category
                var categories = new UNNotificationCategory[] { category };
                UNUserNotificationCenter.Current.SetNotificationCategories(new NSSet<UNNotificationCategory>(categories));
    
                UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert
                                                                      | UNAuthorizationOptions.Badge
                                                                      | UNAuthorizationOptions.Sound,
                                                                      (a, err) => {
                                                                          //TODO handle error
                                                                      });
            }
    
            void ScheduleNotification() {
                // Create content
                var content = new UNMutableNotificationContent();
                content.Title = "Title";
                content.Subtitle = "Subtitle";
                content.Body = "Body";
                content.Badge = 1;
                content.CategoryIdentifier = categoryID;
                content.Sound = UNNotificationSound.Default;
    
                // Fire trigger in one seconds
                var trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(1, false);
    
                var requestID = "customNotification";
                var request = UNNotificationRequest.FromIdentifier(requestID, content, trigger);
    
                //                      This is the line that does the trick
                UNUserNotificationCenter.Current.Delegate = new CustomUNUserNotificationCenterDelegate();
    
                UNUserNotificationCenter.Current.AddNotificationRequest(request, (err) => {
                    if (err != null) {
                        // Report error
                        System.Console.WriteLine("Error: {0}", err);
                    } else {
                        // Report Success
                        System.Console.WriteLine("Notification Scheduled: {0}", request);
                    }
                });
            }
    

    in CustomUNUserNotificationCenterDelegate.cs class:

    public class CustomUNUserNotificationCenterDelegate : UNUserNotificationCenterDelegate {
        public override void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler) {
            // Handle notification tap here
            completionHandler();
        }
    
        public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler) {
            StateManager.CurrentState = StateManager.State.Foreground;
            completionHandler(UNNotificationPresentationOptions.Alert | UNNotificationPresentationOptions.Sound | UNNotificationPresentationOptions.Badge);
        }
    }
    
  • Cesar.TomatisCesar.Tomatis ✭✭ US ✭✭
    Accepted Answer

    Not sure why, but my answer got deleted, here i go again:

    @TedRogers that doesn't work for me since I'm using UNMutableNotificationContent.

    I ended up finding a solution, I needed to set the UNUserNotificationCenter.Current.Delegate, see below:

    in AppDelegate.cs:

    public override bool FinishedLaunching(UIApplication app, NSDictionary options) {
        RegisterForNotifications();
        ScheduleNotification();
        //more code
        return base.FinishedLaunching(app, options);
    }
    
    void RegisterForNotifications() {
        // Create action
        var actionID = "check";
        var title = "Check";
        var action = UNNotificationAction.FromIdentifier(actionID, title, UNNotificationActionOptions.Foreground);
    
        // Create category
        categoryID = "notification";
        var actions = new UNNotificationAction[] { };
        var intentIDs = new string[] { };
        //var categoryOptions = new UNNotificationCategoryOptions[] { };
        var category = UNNotificationCategory.FromIdentifier(categoryID, actions, intentIDs, UNNotificationCategoryOptions.None);
    
        // Register category
        var categories = new UNNotificationCategory[] { category };
        UNUserNotificationCenter.Current.SetNotificationCategories(new NSSet<UNNotificationCategory>(categories));
    
        UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert
                                                              | UNAuthorizationOptions.Badge
                                                              | UNAuthorizationOptions.Sound,
                                                              (a, err) => {
                                                                  //TODO handle error
                                                              });
    }
    
    void ScheduleNotification() {
        // Create content
        var content = new UNMutableNotificationContent();
        content.Title = "Title";
        content.Subtitle = "Subtitle";
        content.Body = "Body";
        content.Badge = 1;
        content.CategoryIdentifier = categoryID;
        content.Sound = UNNotificationSound.Default;
    
        // Fire trigger in one seconds
        var trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(1, false);
    
        var requestID = "notificationRequest";
        var request = UNNotificationRequest.FromIdentifier(requestID, content, trigger);
    
        //Here I set the Delegate to handle the user tapping on notification
        UNUserNotificationCenter.Current.Delegate = new CustomUNUserNotificationCenterDelegate();
    
        UNUserNotificationCenter.Current.AddNotificationRequest(request, (err) => {
            if (err != null) {
                // Report error
                System.Console.WriteLine("Error: {0}", err);
            } else {
                // Report Success
                System.Console.WriteLine("Notification Scheduled: {0}", request);
            }
        });
    }
    

    in CustomUNUserNotificationCenterDelegate.cs:

    public class CustomUNUserNotificationCenterDelegate : UNUserNotificationCenterDelegate {
        public override void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler) {
            // Here you handle the user taps
            completionHandler();
        }
    
        public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler) {
            completionHandler(UNNotificationPresentationOptions.Alert | UNNotificationPresentationOptions.Sound | UNNotificationPresentationOptions.Badge);
        }
    }
    

Answers

  • Cesar.TomatisCesar.Tomatis ✭✭ USMember ✭✭
    Accepted Answer

    @TedRogers maybe I didn't made myself clear enough, I'm not using UIAlertController, I'm using UNMutableNotificationContent.

    I ended up finding a solution, I neede to set the UNUserNotificationCenter.Current.Delegate, see below for the solution:

    in AppDelegate.cs:

            public override bool FinishedLaunching(UIApplication app, NSDictionary options) {
                RegisterForNotifications();
                ScheduleNotification();
                //more code
                return base.FinishedLaunching(app, options);
            }
    
            void RegisterForNotifications() {
                // Create action
                var actionID = "check";
                var title = "Check";
                var action = UNNotificationAction.FromIdentifier(actionID, title, UNNotificationActionOptions.Foreground);
    
                // Create category
                categoryID = "notification";
                var actions = new UNNotificationAction[] { action };
                var intentIDs = new string[] { };
                //var categoryOptions = new UNNotificationCategoryOptions[] { };
                var category = UNNotificationCategory.FromIdentifier(categoryID, actions, intentIDs, UNNotificationCategoryOptions.None);
    
                // Register category
                var categories = new UNNotificationCategory[] { category };
                UNUserNotificationCenter.Current.SetNotificationCategories(new NSSet<UNNotificationCategory>(categories));
    
                UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert
                                                                      | UNAuthorizationOptions.Badge
                                                                      | UNAuthorizationOptions.Sound,
                                                                      (a, err) => {
                                                                          //TODO handle error
                                                                      });
            }
    
            void ScheduleNotification() {
                // Create content
                var content = new UNMutableNotificationContent();
                content.Title = "Title";
                content.Subtitle = "Subtitle";
                content.Body = "Body";
                content.Badge = 1;
                content.CategoryIdentifier = categoryID;
                content.Sound = UNNotificationSound.Default;
    
                // Fire trigger in one seconds
                var trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(1, false);
    
                var requestID = "customNotification";
                var request = UNNotificationRequest.FromIdentifier(requestID, content, trigger);
    
                //                      This is the line that does the trick
                UNUserNotificationCenter.Current.Delegate = new CustomUNUserNotificationCenterDelegate();
    
                UNUserNotificationCenter.Current.AddNotificationRequest(request, (err) => {
                    if (err != null) {
                        // Report error
                        System.Console.WriteLine("Error: {0}", err);
                    } else {
                        // Report Success
                        System.Console.WriteLine("Notification Scheduled: {0}", request);
                    }
                });
            }
    

    in CustomUNUserNotificationCenterDelegate.cs class:

    public class CustomUNUserNotificationCenterDelegate : UNUserNotificationCenterDelegate {
        public override void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler) {
            // Handle notification tap here
            completionHandler();
        }
    
        public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler) {
            StateManager.CurrentState = StateManager.State.Foreground;
            completionHandler(UNNotificationPresentationOptions.Alert | UNNotificationPresentationOptions.Sound | UNNotificationPresentationOptions.Badge);
        }
    }
    
  • Cesar.TomatisCesar.Tomatis ✭✭ USMember ✭✭
    Accepted Answer

    Not sure why, but my answer got deleted, here i go again:

    @TedRogers that doesn't work for me since I'm using UNMutableNotificationContent.

    I ended up finding a solution, I needed to set the UNUserNotificationCenter.Current.Delegate, see below:

    in AppDelegate.cs:

    public override bool FinishedLaunching(UIApplication app, NSDictionary options) {
        RegisterForNotifications();
        ScheduleNotification();
        //more code
        return base.FinishedLaunching(app, options);
    }
    
    void RegisterForNotifications() {
        // Create action
        var actionID = "check";
        var title = "Check";
        var action = UNNotificationAction.FromIdentifier(actionID, title, UNNotificationActionOptions.Foreground);
    
        // Create category
        categoryID = "notification";
        var actions = new UNNotificationAction[] { };
        var intentIDs = new string[] { };
        //var categoryOptions = new UNNotificationCategoryOptions[] { };
        var category = UNNotificationCategory.FromIdentifier(categoryID, actions, intentIDs, UNNotificationCategoryOptions.None);
    
        // Register category
        var categories = new UNNotificationCategory[] { category };
        UNUserNotificationCenter.Current.SetNotificationCategories(new NSSet<UNNotificationCategory>(categories));
    
        UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert
                                                              | UNAuthorizationOptions.Badge
                                                              | UNAuthorizationOptions.Sound,
                                                              (a, err) => {
                                                                  //TODO handle error
                                                              });
    }
    
    void ScheduleNotification() {
        // Create content
        var content = new UNMutableNotificationContent();
        content.Title = "Title";
        content.Subtitle = "Subtitle";
        content.Body = "Body";
        content.Badge = 1;
        content.CategoryIdentifier = categoryID;
        content.Sound = UNNotificationSound.Default;
    
        // Fire trigger in one seconds
        var trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(1, false);
    
        var requestID = "notificationRequest";
        var request = UNNotificationRequest.FromIdentifier(requestID, content, trigger);
    
        //Here I set the Delegate to handle the user tapping on notification
        UNUserNotificationCenter.Current.Delegate = new CustomUNUserNotificationCenterDelegate();
    
        UNUserNotificationCenter.Current.AddNotificationRequest(request, (err) => {
            if (err != null) {
                // Report error
                System.Console.WriteLine("Error: {0}", err);
            } else {
                // Report Success
                System.Console.WriteLine("Notification Scheduled: {0}", request);
            }
        });
    }
    

    in CustomUNUserNotificationCenterDelegate.cs:

    public class CustomUNUserNotificationCenterDelegate : UNUserNotificationCenterDelegate {
        public override void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler) {
            // Here you handle the user taps
            completionHandler();
        }
    
        public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler) {
            completionHandler(UNNotificationPresentationOptions.Alert | UNNotificationPresentationOptions.Sound | UNNotificationPresentationOptions.Badge);
        }
    }
    
  • Cesar.TomatisCesar.Tomatis ✭✭ USMember ✭✭

    Not sure why, but my answer got deleted, here i go again:

    @TedRogers that doesn't work for me since I'm using UNMutableNotificationContent.

    I ended up finding a solution, I needed to set the UNUserNotificationCenter.Current.Delegate, see below:

    in AppDelegate.cs:

    public override bool FinishedLaunching(UIApplication app, NSDictionary options) {
        RegisterForNotifications();
        ScheduleNotification();
        //more code
        return base.FinishedLaunching(app, options);
    }
    
    void RegisterForNotifications() {
        // Create action
        var actionID = "check";
        var title = "Check";
        var action = UNNotificationAction.FromIdentifier(actionID, title, UNNotificationActionOptions.Foreground);
    
        // Create category
        categoryID = "notification";
        var actions = new UNNotificationAction[] { };
        var intentIDs = new string[] { };
        //var categoryOptions = new UNNotificationCategoryOptions[] { };
        var category = UNNotificationCategory.FromIdentifier(categoryID, actions, intentIDs, UNNotificationCategoryOptions.None);
    
        // Register category
        var categories = new UNNotificationCategory[] { category };
        UNUserNotificationCenter.Current.SetNotificationCategories(new NSSet<UNNotificationCategory>(categories));
    
        UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert
                                                              | UNAuthorizationOptions.Badge
                                                              | UNAuthorizationOptions.Sound,
                                                              (a, err) => {
                                                                  //TODO handle error
                                                              });
    }
    
    void ScheduleNotification() {
        // Create content
        var content = new UNMutableNotificationContent();
        content.Title = "Title";
        content.Subtitle = "Subtitle";
        content.Body = "Body";
        content.Badge = 1;
        content.CategoryIdentifier = categoryID;
        content.Sound = UNNotificationSound.Default;
    
        // Fire trigger in one seconds
        var trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(1, false);
    
        var requestID = "notificationRequest";
        var request = UNNotificationRequest.FromIdentifier(requestID, content, trigger);
    
        //Here I set the Delegate to handle the user tapping on notification
        UNUserNotificationCenter.Current.Delegate = new CustomUNUserNotificationCenterDelegate();
    
        UNUserNotificationCenter.Current.AddNotificationRequest(request, (err) => {
            if (err != null) {
                // Report error
                System.Console.WriteLine("Error: {0}", err);
            } else {
                // Report Success
                System.Console.WriteLine("Notification Scheduled: {0}", request);
            }
        });
    }
    

    in CustomUNUserNotificationCenterDelegate.cs:

    public class CustomUNUserNotificationCenterDelegate : UNUserNotificationCenterDelegate {
        public override void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler) {
            // Here you handle the user taps
            completionHandler();
        }
    
        public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler) {
            completionHandler(UNNotificationPresentationOptions.Alert | UNNotificationPresentationOptions.Sound | UNNotificationPresentationOptions.Badge);
        }
    }
    
  • Cesar.TomatisCesar.Tomatis ✭✭ USMember ✭✭

    Not sure why, but my answer got deleted, here i go again:

    @TedRogers that doesn't work for me since I'm using UNMutableNotificationContent.

    I ended up finding a solution, I needed to set the UNUserNotificationCenter.Current.Delegate, see below:

    in AppDelegate.cs:

    public override bool FinishedLaunching(UIApplication app, NSDictionary options) {
        RegisterForNotifications();
        ScheduleNotification();
        //more code
        return base.FinishedLaunching(app, options);
    }
    
    void RegisterForNotifications() {
        // Create action
        var actionID = "check";
        var title = "Check";
        var action = UNNotificationAction.FromIdentifier(actionID, title, UNNotificationActionOptions.Foreground);
    
        // Create category
        categoryID = "notification";
        var actions = new UNNotificationAction[] { };
        var intentIDs = new string[] { };
        //var categoryOptions = new UNNotificationCategoryOptions[] { };
        var category = UNNotificationCategory.FromIdentifier(categoryID, actions, intentIDs, UNNotificationCategoryOptions.None);
    
        // Register category
        var categories = new UNNotificationCategory[] { category };
        UNUserNotificationCenter.Current.SetNotificationCategories(new NSSet<UNNotificationCategory>(categories));
    
        UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert
                                                              | UNAuthorizationOptions.Badge
                                                              | UNAuthorizationOptions.Sound,
                                                              (a, err) => {
                                                                  //TODO handle error
                                                              });
    }
    
    void ScheduleNotification() {
        // Create content
        var content = new UNMutableNotificationContent();
        content.Title = "Title";
        content.Subtitle = "Subtitle";
        content.Body = "Body";
        content.Badge = 1;
        content.CategoryIdentifier = categoryID;
        content.Sound = UNNotificationSound.Default;
    
        // Fire trigger in one seconds
        var trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(1, false);
    
        var requestID = "notificationRequest";
        var request = UNNotificationRequest.FromIdentifier(requestID, content, trigger);
    
        //Here I set the Delegate to handle the user tapping on notification
        UNUserNotificationCenter.Current.Delegate = new CustomUNUserNotificationCenterDelegate();
    
        UNUserNotificationCenter.Current.AddNotificationRequest(request, (err) => {
            if (err != null) {
                // Report error
                System.Console.WriteLine("Error: {0}", err);
            } else {
                // Report Success
                System.Console.WriteLine("Notification Scheduled: {0}", request);
            }
        });
    }
    

    in CustomUNUserNotificationCenterDelegate.cs:

    public class CustomUNUserNotificationCenterDelegate : UNUserNotificationCenterDelegate {
        public override void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler) {
            // Here you handle the user taps
            completionHandler();
        }
    
        public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler) {
            completionHandler(UNNotificationPresentationOptions.Alert | UNNotificationPresentationOptions.Sound | UNNotificationPresentationOptions.Badge);
        }
    }
    
  • Cesar.TomatisCesar.Tomatis ✭✭ USMember ✭✭

    Not sure why, but my answer got deleted, here i go again:

    @TedRogers that doesn't work for me since I'm using UNMutableNotificationContent.

    I ended up finding a solution, I needed to set the UNUserNotificationCenter.Current.Delegate, see below:

    in AppDelegate.cs:

    public override bool FinishedLaunching(UIApplication app, NSDictionary options) {
        RegisterForNotifications();
        ScheduleNotification();
        //more code
        return base.FinishedLaunching(app, options);
    }
    
    void RegisterForNotifications() {
        // Create action
        var actionID = "check";
        var title = "Check";
        var action = UNNotificationAction.FromIdentifier(actionID, title, UNNotificationActionOptions.Foreground);
    
        // Create category
        categoryID = "notification";
        var actions = new UNNotificationAction[] { };
        var intentIDs = new string[] { };
        //var categoryOptions = new UNNotificationCategoryOptions[] { };
        var category = UNNotificationCategory.FromIdentifier(categoryID, actions, intentIDs, UNNotificationCategoryOptions.None);
    
        // Register category
        var categories = new UNNotificationCategory[] { category };
        UNUserNotificationCenter.Current.SetNotificationCategories(new NSSet<UNNotificationCategory>(categories));
    
        UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert
                                                              | UNAuthorizationOptions.Badge
                                                              | UNAuthorizationOptions.Sound,
                                                              (a, err) => {
                                                                  //TODO handle error
                                                              });
    }
    
    void ScheduleNotification() {
        // Create content
        var content = new UNMutableNotificationContent();
        content.Title = "Title";
        content.Subtitle = "Subtitle";
        content.Body = "Body";
        content.Badge = 1;
        content.CategoryIdentifier = categoryID;
        content.Sound = UNNotificationSound.Default;
    
        // Fire trigger in one seconds
        var trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(1, false);
    
        var requestID = "notificationRequest";
        var request = UNNotificationRequest.FromIdentifier(requestID, content, trigger);
    
        //Here I set the Delegate to handle the user tapping on notification
        UNUserNotificationCenter.Current.Delegate = new CustomUNUserNotificationCenterDelegate();
    
        UNUserNotificationCenter.Current.AddNotificationRequest(request, (err) => {
            if (err != null) {
                // Report error
                System.Console.WriteLine("Error: {0}", err);
            } else {
                // Report Success
                System.Console.WriteLine("Notification Scheduled: {0}", request);
            }
        });
    }
    

    in CustomUNUserNotificationCenterDelegate.cs:

    public class CustomUNUserNotificationCenterDelegate : UNUserNotificationCenterDelegate {
        public override void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler) {
            // Here you handle the user taps
            completionHandler();
        }
    
        public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler) {
            completionHandler(UNNotificationPresentationOptions.Alert | UNNotificationPresentationOptions.Sound | UNNotificationPresentationOptions.Badge);
        }
    }
    
  • Cesar.TomatisCesar.Tomatis ✭✭ USMember ✭✭

    Not sure why, but my answer got deleted, here i go again:

    @TedRogers that doesn't work for me since I'm using UNMutableNotificationContent.

    I ended up finding a solution, I needed to set the UNUserNotificationCenter.Current.Delegate, see below:

    in AppDelegate.cs:

    public override bool FinishedLaunching(UIApplication app, NSDictionary options) {
        RegisterForNotifications();
        ScheduleNotification();
        //more code
        return base.FinishedLaunching(app, options);
    }
    
    void RegisterForNotifications() {
        // Create action
        var actionID = "check";
        var title = "Check";
        var action = UNNotificationAction.FromIdentifier(actionID, title, UNNotificationActionOptions.Foreground);
    
        // Create category
        categoryID = "notification";
        var actions = new UNNotificationAction[] { };
        var intentIDs = new string[] { };
        //var categoryOptions = new UNNotificationCategoryOptions[] { };
        var category = UNNotificationCategory.FromIdentifier(categoryID, actions, intentIDs, UNNotificationCategoryOptions.None);
    
        // Register category
        var categories = new UNNotificationCategory[] { category };
        UNUserNotificationCenter.Current.SetNotificationCategories(new NSSet<UNNotificationCategory>(categories));
    
        UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert
                                                              | UNAuthorizationOptions.Badge
                                                              | UNAuthorizationOptions.Sound,
                                                              (a, err) => {
                                                                  //TODO handle error
                                                              });
    }
    
    void ScheduleNotification() {
        // Create content
        var content = new UNMutableNotificationContent();
        content.Title = "Title";
        content.Subtitle = "Subtitle";
        content.Body = "Body";
        content.Badge = 1;
        content.CategoryIdentifier = categoryID;
        content.Sound = UNNotificationSound.Default;
    
        // Fire trigger in one seconds
        var trigger = UNTimeIntervalNotificationTrigger.CreateTrigger(1, false);
    
        var requestID = "notificationRequest";
        var request = UNNotificationRequest.FromIdentifier(requestID, content, trigger);
    
        //Here I set the Delegate to handle the user tapping on notification
        UNUserNotificationCenter.Current.Delegate = new CustomUNUserNotificationCenterDelegate();
    
        UNUserNotificationCenter.Current.AddNotificationRequest(request, (err) => {
            if (err != null) {
                // Report error
                System.Console.WriteLine("Error: {0}", err);
            } else {
                // Report Success
                System.Console.WriteLine("Notification Scheduled: {0}", request);
            }
        });
    }
    

    in CustomUNUserNotificationCenterDelegate.cs:

    public class CustomUNUserNotificationCenterDelegate : UNUserNotificationCenterDelegate {
        public override void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler) {
            // Here you handle the user taps
            completionHandler();
        }
    
        public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler) {
            completionHandler(UNNotificationPresentationOptions.Alert | UNNotificationPresentationOptions.Sound | UNNotificationPresentationOptions.Badge);
        }
    }
    
  • ChrisColeTechChrisColeTech ✭✭✭ USMember ✭✭✭

    thanks for updating us with the solution, i've implemented this tricky notification system and am currently testing

  • TripleDoubleTripleDouble ✭✭✭ BGMember ✭✭✭

    Hey, I have some questions.

    1. How can I make the local notification to stay longer (until it is tapped or swipe for dismiss). Now it is just show for some seconds and hide ?

    2 . How to open the my app from notification when the notification it is tapped ?

    Thanks,

Sign In or Register to comment.