1
votes

When loading the sample code in Xamarin Studio, the app runs as expected.

Xamarin.com Local Notifications Sample Code

But when starting a new single view project, I tried using bits of the code that seemed necessary. The viewcontroller class is structured differently, than in the sample code, on a new project.

ViewController.cs

using System;
using UIKit;
using Foundation;

namespace TestProject1
{
    public partial class ViewController : UIViewController
    {
        public ViewController (IntPtr handle) : base (handle)
        {

        }

        public override void ViewDidLoad ()
        {
            base.ViewDidLoad ();
            // Perform any additional setup after loading the view, typically from a nib.
        }

        public override void DidReceiveMemoryWarning ()
        {
            base.DidReceiveMemoryWarning ();
            // Release any cached data, images, etc that aren't in use.
        }

        partial void ButButton_TouchUpInside (UIButton sender)
        {
            var notification = new UILocalNotification();
            notification.FireDate = NSDate.FromTimeIntervalSinceNow(5);
            notification.AlertAction = "Test";
            notification.AlertBody = "Test Text";
            notification.ApplicationIconBadgeNumber = 1;
            notification.SoundName = UILocalNotification.DefaultSoundName;
            UIApplication.SharedApplication.ScheduleLocalNotification(notification);

            //throw new NotImplementedException ();
        }
    }
}

AppDelegate.cs

using Foundation;
using UIKit;

namespace TestProject1
{
    [Register ("AppDelegate")]
    public class AppDelegate : UIApplicationDelegate
    {
        private ViewController viewController;
        private UIWindow window;

        public override UIWindow Window {
            get;
            set;
        }
        public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)
        {
            viewController = new ViewController();
            window = new UIWindow (UIScreen.MainScreen.Bounds);
            viewController = new ViewController ();
            window.RootViewController = viewController;
            window.MakeKeyAndVisible ();

            //if (UIDevice.CurrentDevice.CheckSystemVersion (8, 0)) {
                var notificationSettings = UIUserNotificationSettings.GetSettingsForTypes (
                                               UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound, null
                                           );
                application.RegisterUserNotificationSettings (notificationSettings);
            //}

            if (launchOptions != null)
            {
                // check for a local notification
                if (launchOptions.ContainsKey(UIApplication.LaunchOptionsLocalNotificationKey))
                {
                    var localNotification = launchOptions[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;
                    }
                }
            }

            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);

            // reset our badge
            UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0;
        }
    }
}

In the sample project, the only difference is ViewController is declared with no parameters: public ViewController { }. If I add that code, the app complies and runs. The notifications fires and shows the badge, but never shows appears within the app.

Instead of trying to rig the code on a new project, how do you properly declare: viewController = new ViewController(); with a IntPtr parameter?

Thanks in advance!

1
You are adding ViewControllers through Storyboard, right ? - Sreeraj
No, I was using the existing ViewController from the new project. There is only one. - detailCode
No Storyboard or XIB ? - Sreeraj
It does have a StoryBoard. No XIB on the controller. - detailCode
View for the ViewController is in Storyboard right ? - Sreeraj

1 Answers

2
votes

Instead of trying to use attach the alert to the ViewController as shown in the demo code, use UIAlertView.

public override void ReceivedLocalNotification(UIApplication application, UILocalNotification notification)
{
    UIAlertView alert = new UIAlertView () { Title = notification.AlertAction, Message = notification.AlertBody };
    alert.AddButton("OK");
    alert.Show ();
    // CLEAR BADGES
    UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0;
}