Xamarin.UWP本地通知不起作用
我已经实现了一个在Android和iOS平台上发送特定日期和时间的本地通知的代码。而且,我的代码适用于Android和iOS平台。在Android中,我使用的是AlarmManager。 在iOS中,用于发送通知的UILocationNotification。但是,我没有找到UWP平台实现本地通知功能的任何解决方案 使用Android和iOS平台上的依赖服务。是否有任何解决方案可用于发送特定的本地通知 所有平台的日期和时间,包括Xamarin.表格中的“UWP”(强制要求) Xamarin.UWP本地通知不起作用 如果您想在Xamarin.Forms前发出本地通知,请参阅此。但它还没有实现UWP平台。所以我们需要在uwp中实现并使用依赖服务来调用它。我已经实现了一个你可以参考的简单方法 接口Xamarin.UWP本地通知不起作用,xamarin,xamarin.forms,uwp,Xamarin,Xamarin.forms,Uwp,我已经实现了一个在Android和iOS平台上发送特定日期和时间的本地通知的代码。而且,我的代码适用于Android和iOS平台。在Android中,我使用的是AlarmManager。 在iOS中,用于发送通知的UILocationNotification。但是,我没有找到UWP平台实现本地通知功能的任何解决方案 使用Android和iOS平台上的依赖服务。是否有任何解决方案可用于发送特定的本地通知 所有平台的日期和时间,包括Xamarin.表格中的“UWP”(强制要求) Xamarin.UW
public interface INotificationManager
{
int ScheduleNotification(string title, string message,DateTime scheduledTime);
}
public class UWPNotificationManager : INotificationManager
{
int messageId = -1;
public int ScheduleNotification(string title, string message,DateTime scheduledTime)
{
messageId++;
string TOAST = $@"<toast>
<visual>
<binding template='ToastGeneric'>
<text>{title}</text>
<text>{message}</text>
</binding>
</visual>
<audio src =""ms-winsoundevent:Notification.Mail"" loop=""true""/>
</toast>";
Windows.Data.Xml.Dom.XmlDocument xml = new Windows.Data.Xml.Dom.XmlDocument();
xml.LoadXml(TOAST);
ScheduledToastNotification toast = new ScheduledToastNotification(xml, scheduledTime);
toast.Id = "IdTostone" + messageId.ToString();
toast.Tag = "NotificationOne";
toast.Group = nameof(UWPNotificationManager);
ToastNotificationManager.CreateToastNotifier().AddToSchedule(toast);
return messageId;
}
}
实施
public interface INotificationManager
{
int ScheduleNotification(string title, string message,DateTime scheduledTime);
}
public class UWPNotificationManager : INotificationManager
{
int messageId = -1;
public int ScheduleNotification(string title, string message,DateTime scheduledTime)
{
messageId++;
string TOAST = $@"<toast>
<visual>
<binding template='ToastGeneric'>
<text>{title}</text>
<text>{message}</text>
</binding>
</visual>
<audio src =""ms-winsoundevent:Notification.Mail"" loop=""true""/>
</toast>";
Windows.Data.Xml.Dom.XmlDocument xml = new Windows.Data.Xml.Dom.XmlDocument();
xml.LoadXml(TOAST);
ScheduledToastNotification toast = new ScheduledToastNotification(xml, scheduledTime);
toast.Id = "IdTostone" + messageId.ToString();
toast.Tag = "NotificationOne";
toast.Group = nameof(UWPNotificationManager);
ToastNotificationManager.CreateToastNotifier().AddToSchedule(toast);
return messageId;
}
}
您好,我遵循了您的代码并实现了相同的功能。但我面临一个与初始化方法相关的错误。您是否为INotificationManager实现了一些接口??请帮我解决这个问题。我只是简化了界面,你们可以直接使用上面的代码。嗨,我试过这么做。但是,它正在NotificationReceived()的Main.xaml.cs文件中引发异常“System.NullReferenceException:“对象引用未设置为对象的实例”。”。你能在GitHub repo中为我提供相同的代码,以便我能更好地测试它吗???当然,请检查这个。好的,如果我卡住了,我会再试一次并联系你。