Local Notification doesn't appear.

YanKononovYanKononov RUMember
edited October 2015 in Cross Platform with Xamarin

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/application_fundamentals/notifications/ios/local_notifications_in_ios_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.

Best Answer

Answers

  • YanKononovYanKononov RUMember

    Thank you very much for this! It works!

  • @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/application_fundamentals/notifications/ios/local_notifications_in_ios_walkthrough/

  • JimTyminskiJimTyminski USUniversity ✭✭

    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!

  • ChandChand USMember ✭✭

    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.

  • DevologyDevology GBMember ✭✭✭

    @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.

Sign In or Register to comment.