我是移动开发和Xamarin的新手。 我试图通过DependencyService提供跨平台的本地通知。
我使用Xamarin的特定平台教程(本例中为iOS):
所以问题是当我按下按钮时没有任何反应。 我的代码:
ContentPage
:
using System;
using Xamarin.Forms;
namespace Tasker
{
public class TestPage : ContentPage
{
StackLayout layout = new StackLayout();
public TestPage ()
{
var notifyButton = new Button
{
Text = "Notethat",
VerticalOptions = LayoutOptions.CenterAndExpand,
HorizontalOptions = LayoutOptions.CenterAndExpand,
};
layout.Children.Add (notifyButton);
notifyButton.Clicked += (sender, e) => {
DependencyService.Get<INotification>().CreateNotification();
};
this.Content = layout;
}
}
}
然后我为跨平台注入创建了界面:
using System;
using Xamarin.Forms;
namespace Tasker
{
public interface INotification
{
void CreateNotification();
}
}
行。然后我实现iOS的Notification接口:
using System;
using Xamarin.Forms;
using Tasker.iOS;
using UIKit;
using Foundation;
[assembly: Dependency(typeof(INotification_IOS))]
namespace Tasker.iOS
{
public class INotification_IOS:INotification
{
public INotification_IOS ()
{
}
public void 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 one minute alert has fired!";
//notification.
// modify the badge
notification.ApplicationIconBadgeNumber = 1;
// set the sound to be the default sound
notification.SoundName = UILocalNotification.DefaultSoundName;
// schedule it
UIApplication.SharedApplication.ScheduleLocalNotification(notification);
}
}
}
然后我的AppDelegate类。我认为问题出在这里:
using Foundation;
using UIKit;
using Xamarin.Forms;
namespace Tasker.iOS
{
[Register("AppDelegate")]
public class AppDelegate :global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
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);
// reset our badge
UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0;
}
}
}
因此,当您看到我尝试进行跨平台本地通知时。
完全跨平台的功能&#34; CreateNotification()&#34;工作 - 我查了一下。
所以问题出现在我为通知创建警报或窗口的地方。
将不胜感激任何帮助!