Toast通知在Xamarin UWP Windows应用程序中不起作用
我有UWP Windows应用程序,是在Xamarin.forms下开发的。我已经实现了Toast通知,但我面临着这个问题。在某些Windows 10系统中,它可以正常工作并正确显示toast通知,但在某些Windows 10系统中(即使具有相同的Windows 10操作系统更新),它无法工作 下面是我在本机UWP中实现的第一个代码片段Toast通知在Xamarin UWP Windows应用程序中不起作用,xamarin,xamarin.forms,uwp,uwp-xaml,xamarin.uwp,Xamarin,Xamarin.forms,Uwp,Uwp Xaml,Xamarin.uwp,我有UWP Windows应用程序,是在Xamarin.forms下开发的。我已经实现了Toast通知,但我面临着这个问题。在某些Windows 10系统中,它可以正常工作并正确显示toast通知,但在某些Windows 10系统中(即使具有相同的Windows 10操作系统更新),它无法工作 下面是我在本机UWP中实现的第一个代码片段 string msg = "Toast Notification Header"; string subMsg = "Toast
string msg = "Toast Notification Header";
string subMsg = "Toast Notification Title";
var toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);
var toastTextElements = toastXml.GetElementsByTagName("text");
toastTextElements[0].AppendChild(toastXml.CreateTextNode(msg));
toastTextElements[1].AppendChild(toastXml.CreateTextNode(subMsg));
//To play the custom sound
var toastNode = toastXml.SelectSingleNode("/toast");
var audio = toastXml.CreateElement("audio");
audio.SetAttribute("src", "ms-appx:///Assets/incoming_message.wav");
audio.SetAttribute("loop", "false");
toastNode.AppendChild(audio);
var toast = new ToastNotification(toastXml);
ToastNotificationManager.CreateToastNotifier().Show(toast);
// "With Microsoft.Toolkit.Uwp.Notifications"
// Construct the toast content
ToastContent toastContent = new ToastContent()
{
Visual = new ToastVisual()
{
BindingGeneric = new ToastBindingGeneric()
{
Children =
{
new AdaptiveText()
{
Text = "Toast Notification Header"
},
new AdaptiveText()
{
Text = "Toast Notification Content"
}
}
}
}
};
bool supportsCustomAudio = true;
// If we're running on Desktop before Version 1511, do NOT include custom audio
// since it was not supported until Version 1511, and would result in a silent toast.
if (AnalyticsInfo.VersionInfo.DeviceFamily.Equals("Windows.Desktop")
&& !ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 2))
{
supportsCustomAudio = false;
}
if (supportsCustomAudio)
{
toastContent.Audio = new ToastAudio()
{
Src = new Uri("ms-appx:///Assets/incoming_message.wav")
};
}
// And create the toast notification
ToastNotification notification = new ToastNotification(toastContent.GetXml());
// And then send the toast
ToastNotificationManager.CreateToastNotifier().Show(notification);
下面是我在本机UWP中实现的第二个代码片段
string msg = "Toast Notification Header";
string subMsg = "Toast Notification Title";
var toastXml = ToastNotificationManager.GetTemplateContent(ToastTemplateType.ToastText02);
var toastTextElements = toastXml.GetElementsByTagName("text");
toastTextElements[0].AppendChild(toastXml.CreateTextNode(msg));
toastTextElements[1].AppendChild(toastXml.CreateTextNode(subMsg));
//To play the custom sound
var toastNode = toastXml.SelectSingleNode("/toast");
var audio = toastXml.CreateElement("audio");
audio.SetAttribute("src", "ms-appx:///Assets/incoming_message.wav");
audio.SetAttribute("loop", "false");
toastNode.AppendChild(audio);
var toast = new ToastNotification(toastXml);
ToastNotificationManager.CreateToastNotifier().Show(toast);
// "With Microsoft.Toolkit.Uwp.Notifications"
// Construct the toast content
ToastContent toastContent = new ToastContent()
{
Visual = new ToastVisual()
{
BindingGeneric = new ToastBindingGeneric()
{
Children =
{
new AdaptiveText()
{
Text = "Toast Notification Header"
},
new AdaptiveText()
{
Text = "Toast Notification Content"
}
}
}
}
};
bool supportsCustomAudio = true;
// If we're running on Desktop before Version 1511, do NOT include custom audio
// since it was not supported until Version 1511, and would result in a silent toast.
if (AnalyticsInfo.VersionInfo.DeviceFamily.Equals("Windows.Desktop")
&& !ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 2))
{
supportsCustomAudio = false;
}
if (supportsCustomAudio)
{
toastContent.Audio = new ToastAudio()
{
Src = new Uri("ms-appx:///Assets/incoming_message.wav")
};
}
// And create the toast notification
ToastNotification notification = new ToastNotification(toastContent.GetXml());
// And then send the toast
ToastNotificationManager.CreateToastNotifier().Show(notification);
上面的代码剪贴显示了某些Windows 10系统中的Toast通知,而在某些其他Windows 10系统中不起作用。
请在这方面指导我。提前谢谢
问候,,
Vivek请按照以下步骤在UWP项目中添加toast通知 步骤1:-创建一个新的UWP项目 步骤2:-转到代码隐藏并添加名称空间 使用Windows.UI.Notifications
使用 祝酒 步骤3:-我创建了一个Toast通用模板,如下代码所示:
public static Windows.Data.Xml.Dom.XmlDocument CreateToast()
{
var xDoc = new XDocument(
new XElement("toast",
new XElement("visual",
new XElement("binding", new XAttribute("template", "ToastGeneric"),
new XElement("text", "C# Corner"),
new XElement("text", "Do you got MVP award?")
)
),// actions
new XElement("actions",
new XElement("action", new XAttribute("activationType", "background"),
new XAttribute("content", "Yes"), new XAttribute("arguments", "yes")),
new XElement("action", new XAttribute("activationType", "background"),
new XAttribute("content", "No"), new XAttribute("arguments", "no"))
)
)
);
var xmlDoc = new Windows.Data.Xml.Dom.XmlDocument();
xmlDoc.LoadXml(xDoc.ToString());
return xmlDoc;
}
步骤4:-使用XML文档创建toast通知对象
var xmdock = CreateToast();
var toast = new ToastNotification(xmdock);
Next show the toast using ToastNotificationManager class.
var notifi = Windows.UI.Notifications.ToastNotificationManager.CreateToastNotifier();
notifi.Show(toast);
步骤5:-C#代码隐藏:
private void showToastBtn_Click(object sender, RoutedEventArgs e)
{
var xmdock = CreateToast();
var toast = new ToastNotification(xmdock);
var notifi = Windows.UI.Notifications.ToastNotificationManager.CreateToastNotifier();
notifi.Show(toast);
}
我希望上面的代码对您有用
谢谢您好,有些系统不支持哪些系统?在不正常弹出通知的设备中,您可以在
设置->系统->通知和操作中检查是否已启用应用程序的通知推送权限。系统表示Windows 10系统,一些Windows 10系统正在显示通知,而一些未显示,即使系统具有相同的Windows update版本。我已经检查了通知权限,并且在所有系统中都已启用。抱歉,我没有明确说明,Windows 10有多个发布版本,您是否测试同一个系统?如果同一版本中的一些可以推送通知,而另一些不能,那么这应该与代码无关,而是与我在多个系统上检查的计算机配置有关,这些系统具有最新和相同的1909版本的Windows 10。在一个系统中,它正在工作,而在另一个系统中,它不工作。很抱歉,我无法重现此问题。也许我们可以通过测试找到问题的原因:1。分别使用Xamarin.Forms
和Native UWP
创建通知,并在多个系统上测试它们。这是为了看看问题是否与开发框架有关。2.为测试创建一个简单的通知(仅包含文本),这是为了确认问题是否与通知的创建有关。3.如果上述两项都不能提供清晰的信息,则可能是系统原因(尝试在系统中启用开发人员模式)