在Xamarin Studio中加载示例代码时,应用程序按预期运行。
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声明没有参数:public ViewController {}。如果我添加该代码,该应用程序符合并运行。通知会触发并显示徽章,但应用内不会显示任何内容。
您如何正确声明:viewController = new ViewController();而不是尝试在新项目上安装代码。使用IntPtr参数?
提前致谢!
答案 0 :(得分:2)
使用UIAlertView,而不是尝试使用将警报附加到ViewController,如演示代码中所示。
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;
}