[英]How to provide the iOS local notification through the Xamarin.Forms?
I'm new in in mobile development and Xamarin. 我是移动开发和Xamarin的新手。 I'm trying to provide cross-platform local notifications via DependencyService.
我正在尝试通过DependencyService提供跨平台的本地通知。
I'm using Xamarin's tutorials for specific-platform(for iOS in this example): 我正在针对特定平台使用Xamarin的教程(在此示例中为iOS):
https://developer.xamarin.com/guides/cross-platform/application_fundamentals/notifications/ios/local_notifications_in_ios_walkthrough/ https://developer.xamarin.com/guides/cross-platform/application_fundamentals/notifications/ios/local_notifications_in_ios_walkthrough/
So the problem is that when i press the button nothing happens. 所以问题是当我按下按钮时什么也没发生。 My code:
我的代码:
ContentPage
: 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;
}
}
}
Then I've created interface for my cross-platform injection: 然后,我为跨平台注入创建了接口:
using System;
using Xamarin.Forms;
namespace Tasker
{
public interface INotification
{
void CreateNotification();
}
}
OK. 好。 Then I implement my Notification interface for iOS:
然后,为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);
}
}
}
Then my AppDelegate class. 然后是我的AppDelegate类。 I think that problem is somewhere here:
我认为问题出在这里:
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;
}
}
}
So as you see i tried to make cross-platform local notification. 因此,如您所见,我试图进行跨平台的本地通知。
Exactly cross-platform function "CreateNotification()" works - I checked. 完全跨平台的功能“ CreateNotification()”有效-我检查了。
So the problem is somewhere where I create Alert or window for notification. 所以问题出在我创建警报或通知窗口的地方。
Will be grateful for any help! 将不胜感激!
You could try this plugin that is PCL 您可以尝试使用PCL这个插件
https://www.nuget.org/packages/Xam.Plugins.Notifier/ https://www.nuget.org/packages/Xam.Plugins.Notifier/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.