首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Xamarin.UWP本地通知不起作用

Xamarin.UWP本地通知不起作用
EN

Stack Overflow用户
提问于 2019-11-26 18:23:27
回答 2查看 591关注 0票数 3

我已经在Android和iOS platform.And上实现了发送特定日期和时间的本地通知的代码,我的代码用于Android和iOS platform.In Android,我使用的是AlarmManager。在iOS中,UILocationNotification用于发送通知。但是,我没有为UWP平台找到任何解决方案来使用依赖服务来实现本地通知功能,就像我在Android和iOS平台上得到的一样。是否有办法为所有平台发送本地通知,包括Xamarin.forms中的“UWP”(强制性)?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-11-27 05:09:11

Xamarin.UWP本地通知不起作用

如果要在Xamarin.Forms之前发出本地通知,请参考此文档。但还没有实现UWP平台。因此,我们需要在uwp中实现附表通知,并使用依赖服务来调用它。我实现了一个你可以参考的简单方法。

接口

代码语言:javascript
复制
public interface INotificationManager
{ 
    int ScheduleNotification(string title, string message,DateTime scheduledTime);   
}

实现

代码语言:javascript
复制
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;
    }

}

使用

代码语言:javascript
复制
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));
}
票数 3
EN

Stack Overflow用户

发布于 2022-08-02 18:18:52

试着使用

代码语言:javascript
复制
ToastNotificationManager.CreateToastNotifier().Show(toast);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59057231

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档