2016-05-06 3 views
1

При загрузке образца кода в Xamarin Studio приложение работает как ожидалось.Создание локального уведомления iOS

Xamarin.com Local Notifications Sample Code

Но при запуске нового единого вида проекта, я попытался с помощью битов кода, который, казалось необходимым. Класс viewcontroller структурирован по-разному, чем в примере кода, в новом проекте.

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

В примере проекта, единственное различие ViewController объявляется без параметров: открытая ViewController {}. Если я добавлю этот код, приложение выполнит и выполнит. Уведомления загораются и показывают значок, но никогда не отображается в приложении.

Вместо того, чтобы пытаться установить код в новом проекте, как правильно объявить: viewController = new ViewController(); с параметром IntPtr?

Заранее благодарен!

+0

Вы добавляете ViewControllers через раскадровку, правильно? – Sreeraj

+0

Нет, я использовал существующий ViewController из нового проекта. Здесь только один. – detailCode

+0

Нет раскадровки или XIB? – Sreeraj

ответ

2

Вместо того, чтобы пытаться подключить оповещение к ViewController, как показано в демо-коде, используйте 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; 
} 
Смежные вопросы