locked
Local Notification doesn't appear. RRS feed

  • Question

  • User160119 posted

    Hello! I'm trying to implement cross-platform notifications through the Dependency Service. Currently - for iOS. So the goal is to invoke the notification when the button is pressed. The problem is that the notification doesn't appear on the screen. I'm using Tamarin tutors about Local Notifications on iOS, but just trying to implement it in cross-platform through the Xamarin.Forms https://developer.xamarin.com/guides/cross-platform/applicationfundamentals/notifications/ios/localnotificationsinios_walkthrough/ Can you look at my code and see what I'm doing wrong? Here's my call of method:

        notifyButton.Clicked += (sender, e) => 
            {
    
                DependencyService.Get<INotification>().CreateNotification();
                //DisplayAlert("asd","asd","ad");
            };
    

    Here's the method. It implements the interface INotification:

    [assembly: Dependency(typeof(INotification_IOS))]
    namespace Tasker.iOS
    {
        public class INotification_IOS:INotification
        {
            public INotification_IOS (){}
            void INotification.CreateNotification()
                {
                     //create the notification
                    var notification = new UILocalNotification();
    
                    // set the fire date (the date time in which it will fire)
                    notification.FireDate = NSDate.FromTimeIntervalSinceNow(1);
    
                    // configure the alert
                    notification.AlertAction = "Some info";
                    notification.AlertBody = "Your alert has fired!";
                    //notification.
    
                    // modify the badge
                    notification.ApplicationIconBadgeNumber = 1;
    
                    // set the sound to be the default sound
                    notification.SoundName = UILocalNotification.DefaultSoundName;
    
                    UIApplication.SharedApplication.ScheduleLocalNotification(notification);
                }
        }
    }
    

    And here's my AppDelegate:

        namespace Tasker.iOS
        {
            [Register("AppDelegate")]
            public class AppDelegate :global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate//, INotification
            {
                private UIViewController viewController;
                private UIWindow window;
                public override bool FinishedLaunching(UIApplication app, NSDictionary options)
                {
                    global::Xamarin.Forms.Forms.Init ();//you can't remove it
                    viewController = new UIViewController();
                    window = new UIWindow ();
    
                    window.RootViewController = viewController;
                    window.MakeKeyAndVisible();
    
    
                     //check for a notification
                    if (options != null)
                    {
                        // check for a local notification
                        if (options.ContainsKey(UIApplication.LaunchOptionsLocalNotificationKey))
                        {
                            var localNotification = options[UIApplication.LaunchOptionsLocalNotificationKey] as UILocalNotification;
                            if (localNotification != null)
                            {
                                UIAlertController okayAlertController = UIAlertController.Create (localNotification.AlertAction, localNotification.AlertBody,                UIAlertControllerStyle.Alert);
                                okayAlertController.AddAction (UIAlertAction.Create ("OK", UIAlertActionStyle.Default, null));
                                viewController.PresentViewController (okayAlertController, true, null);
    
                                // reset our badge
                                UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0;
                            }
                        }
                    }
    
                    if (UIDevice.CurrentDevice.CheckSystemVersion (8, 0)) {
                        var notificationSettings = UIUserNotificationSettings.GetSettingsForTypes (
                            UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound, null
                        );
    
                        app.RegisterUserNotificationSettings (notificationSettings);
                    } 
                    LoadApplication (new App ());//if you will remove it, app won't be launched
    
                    return base.FinishedLaunching (app, options);
                    //return true;
                }
    
                public override void ReceivedLocalNotification(UIApplication application, UILocalNotification notification)
                {
                    // show an alert
                    UIAlertController okayAlertController = UIAlertController.Create (notification.AlertAction, notification.AlertBody, UIAlertControllerStyle.Alert);
                    okayAlertController.AddAction (UIAlertAction.Create ("OK", UIAlertActionStyle.Default, null));
                    viewController.PresentViewController (okayAlertController, true, null);
                    UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0;
                }
    
            }
        }
    

    At the end i can say that the notification is really scheduled - I made an output of number of scheduledNotifications and it really fires at 1 second! I think that the problem somewhere in view or window, but I'm not so familiar with them to just see my bug through the code. Thanks.

    Thursday, October 1, 2015 11:01 PM

Answers

  • User181 posted

    The problem is that you are creating a blank view controller in FinishedLaunching that never really gets used. LoadApplication will overwrite the root view controller (and your window) with its own. You're then trying to present your alert on top of a view controller that isn't on the screen.

    Delete your view controller and window code. Just let LoadApplication do that. Then present your alert on UIApplication.SharedApplication.KeyWindow.RootViewController.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, October 2, 2015 2:00 AM

All replies

  • User181 posted

    The problem is that you are creating a blank view controller in FinishedLaunching that never really gets used. LoadApplication will overwrite the root view controller (and your window) with its own. You're then trying to present your alert on top of a view controller that isn't on the screen.

    Delete your view controller and window code. Just let LoadApplication do that. Then present your alert on UIApplication.SharedApplication.KeyWindow.RootViewController.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, October 2, 2015 2:00 AM
  • User160119 posted

    Thank you very much for this! It works!

    Friday, October 2, 2015 6:29 AM
  • User178774 posted

    @adamkemp said: Delete your view controller and window code. Just let LoadApplication do that. Then present your alert on UIApplication.SharedApplication.KeyWindow.RootViewController.

    This should Definitely be in the walkthrough, instead of just the "viewcontroller" variable. https://developer.xamarin.com/guides/cross-platform/applicationfundamentals/notifications/ios/localnotificationsinios_walkthrough/

    Monday, February 1, 2016 5:46 PM
  • User240432 posted

    Good call @adamkemp ! Answer +1.

    This should Definitely be in the walkthrough, instead of just the "viewcontroller" variable.

    @RicardoColomboOliveira +1 for you too for mentioning!

    Monday, October 31, 2016 3:12 PM
  • User102570 posted

    I have used local notification code from xamarin & achieved. But the notification is showing like alert box. How to get local notification in Notificaion Tray for IOS.

    Friday, November 18, 2016 2:34 PM
  • User125363 posted

    @Chand said: I have used local notification code from xamarin & achieved. But the notification is showing like alert box. How to get local notification in Notificaion Tray for IOS.

    I think you just need to ensure you're not in the app at the time, then it should automatically appear in the notification tray.

    Tuesday, July 9, 2019 10:50 PM