了解Toast推送通知,以及如何在推送通知上显示文本,并通过按下推送通知来启动应用程序.Windows Phone 8.
本文关键字:通知 下推 启动 Phone Windows 应用程序 显示 Toast 文本 了解 | 更新日期: 2023-09-27 18:26:36
我正在尝试将我的应用程序设置为从服务器接收toast推送通知。
由于该服务器由其他人处理,并且他没有向WSN请求令牌,因此我遵循了一个示例,并使用"本地网页"发送通知
protected void ButtonSendToast_Click(object sender, EventArgs e)
{
try
{
// Get the URI that the Microsoft Push Notification Service returns to the push client when creating a notification channel.
// Normally, a web service would listen for URIs coming from the web client and maintain a list of URIs to send
// notifications out to.
string subscriptionUri = TextBoxUri.Text.ToString();
HttpWebRequest sendNotificationRequest = (HttpWebRequest)WebRequest.Create(subscriptionUri) as HttpWebRequest;
// Create an HTTPWebRequest that posts the toast notification to the Microsoft Push Notification Service.
// HTTP POST is the only method allowed to send the notification.
sendNotificationRequest.Method = "POST";
// The optional custom header X-MessageID uniquely identifies a notification message.
// If it is present, the same value is returned in the notification response. It must be a string that contains a UUID.
// sendNotificationRequest.Headers.Add("X-MessageID", "<UUID>");
// Create the toast message.
string toastMessage = "<?xml version='"1.0'" encoding='"utf-8'"?>" +
"<wp:Notification xmlns:wp='"WPNotification'">" +
"<wp:Toast>" +
"<wp:Text1>" + TextBoxTitle.Text.ToString() + "</wp:Text1>" +
"<wp:Text2>" + TextBoxSubTitle.Text.ToString() + "</wp:Text2>" +
"<wp:Param>/Page2.xaml?NavigatedFrom=Toast Notification</wp:Param>" +
"</wp:Toast> " +
"</wp:Notification>";
// Set the notification payload to send.
byte[] notificationMessage = Encoding.Default.GetBytes(toastMessage);
// Set the web request content length.
sendNotificationRequest.Headers.Add("Authorization", String.Format("Bearer {0}", "EgAdAQMAAAAEgAAAC4AATIYp8fmpjFpbdnRTjf2qfP/GqZ8Bbb62bH6N+0MhSztcV/wXfv9aVjiwbVgF5EX0fgBXC6LvJCpl1+ze7ts9h5je4e1QekryEFqfWl36BtTBnmWqBFk0WmwxpdIgGqhVjAtRdnJ3ODnFSBCfd7dq8nFiFTFDxPcTXhdDbu9W3BKMAFoAjAAAAAAAHFAXTMH+bVbB/m1W60gEAA8AMTkwLjE5My42OS4yMzMAAAAAAF0AbXMtYXBwOi8vcy0xLTE1LTItMTU5OTEyNjk1NS0zODAwNDMxNzQ0LTk2OTg4NTEzNi0xNjkxMDU1MjI4LTcwOTcyNTQ0NC00MDYxNzA4MDczLTI0Mzg0MzM1MzQA"));
sendNotificationRequest.ContentLength = notificationMessage.Length;
sendNotificationRequest.ContentType = "text/xml";
sendNotificationRequest.Headers.Add("X-WNS-Type", "wns/toast");
using (Stream requestStream = sendNotificationRequest.GetRequestStream())
{
requestStream.Write(notificationMessage, 0, notificationMessage.Length);
}
// Send the notification and get the response.
HttpWebResponse response = (HttpWebResponse)sendNotificationRequest.GetResponse();
string notificationStatus = response.Headers["X-NotificationStatus"];
string notificationChannelStatus = response.Headers["X-SubscriptionStatus"];
string deviceConnectionStatus = response.Headers["X-DeviceConnectionStatus"];
// Display the response from the Microsoft Push Notification Service.
// Normally, error handling code would be here. In the real world, because data connections are not always available,
// notifications may need to be throttled back if the device cannot be reached.
TextBoxResponse.Text = notificationStatus + " | " + deviceConnectionStatus + " | " + notificationChannelStatus;
}
catch (Exception ex)
{
TextBoxResponse.Text = "Exception caught sending update: " + ex.ToString();
}
}
现在,在我的应用程序中,我请求了通道uri和一个处理程序,当该通道接收到推送时,会调用该处理程序
PushNotificationChannel channel = null;
try
{
channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
Debug.WriteLine(channel.Uri);
if (channel.Uri != null)
{
var localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
localSettings.Values.Remove("PushToken");
localSettings.Values["PushToken"] = channel.Uri;
channel.PushNotificationReceived += channel_PushNotificationReceived;
}
}
catch (Exception ex)
{
Debug.WriteLine(ex.ToString());
}
}
async void channel_PushNotificationReceived(PushNotificationChannel sender, PushNotificationReceivedEventArgs e)
{
String notificationContent = String.Empty;
notificationContent = e.ToastNotification.Content.GetXml();
Debug.WriteLine(notificationContent);
}
到目前为止,一切都很好:当我的应用程序运行和关闭时,我都会收到通知。但它只写着"新通知",当我点击它时什么也没发生。
我已尝试添加一个事件
e.ToastNotification.Activated += ToastNotification_Activated;
但这不起作用,在阅读了大约20个文档后,我对toast模板以及如何使用它来显示我从服务器收到的内容感到非常困惑
那么,真正的方法是什么呢?在吐司中显示推送中收到的一些数据,并在用户点击时让应用程序"启动/转到某个页面"?
应用程序转到某个页面
自Windows 8/8.1以来,应用程序一直被期望处理用户点击该应用程序的toast通知时的激活——该应用程序应该通过执行导航和显示特定于toast的UI来做出响应。这是通过包含在toast负载中的激活字符串来实现的,然后将该字符串作为激活事件中的参数传递给您的应用程序
对于导航到特定页面,您需要覆盖App.Xaml.cs中的OnActivated事件,并处理字符串参数。
用于显示吐司如果您想在tile中显示文本,则它会尝试Badge通知或仅通过toast接收的数据,然后在从激活事件导航后在页面上处理它。另请检查此链接。它显示了不同通知的标准模板。希望能有所帮助。
当系统无法识别您的toast通知负载时,您将收到一个"新通知"。这是因为你使用的是MPNS快速启动,而不是WNS快速启动,除了吐司通知格式外,大部分内容都转移到了WNS。你想让你的吐司内容看起来像这样:
<toast launch="">
<visual lang="en-US">
<binding template="ToastImageAndText01">
<image id="1" src="World" />
<text id="1">Hello</text>
</binding>
</visual>
</toast>
以下是WNS快速入门指南:https://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh868252.aspx
以及WNS toast架构:https://msdn.microsoft.com/en-us/library/windows/apps/br230849.aspx