我已经在Android和iOS platform.And上实现了发送特定日期和时间的本地通知的代码,我的代码用于Android和iOS platform.In Android,我使用的是AlarmManager。在iOS中,UILocationNotification用于发送通知。但是,我没有为UWP平台找到任何解决方案来使用依赖服务来实现本地通知功能,就像我在Android和iOS平台上得到的一样。是否有办法为所有平台发送本地通知,包括Xamarin.forms中的“UWP”(强制性)?
发布于 2019-11-27 05:09:11
Xamarin.UWP本地通知不起作用
如果要在Xamarin.Forms之前发出本地通知,请参考此文档。但还没有实现UWP平台。因此,我们需要在uwp中实现附表通知,并使用依赖服务来调用它。我实现了一个你可以参考的简单方法。
接口
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;
}
}使用
private void OnScheduleClick(object sender, EventArgs e)
{
notificationNumber++;
string title = $"Local Notification #{notificationNumber}";
string message = $"You have now received {notificationNumber} notifications!";
notificationManager.ScheduleNotification(title, message,DateTime.Now + TimeSpan.FromSeconds(3));
}发布于 2022-08-02 18:18:52
试着使用
ToastNotificationManager.CreateToastNotifier().Show(toast);https://stackoverflow.com/questions/59057231
复制相似问题