【问题标题】:Send Push notifications (Toasts) with launch parameters in Windows 8.1在 Windows 8.1 中发送带有启动参数的推送通知 (Toast)
【发布时间】:2015-09-14 12:06:05
【问题描述】:

我有一个 WinJS 项目,它在 运行时组件 中有一个 BackgroundTask,它在我自己发送推送通知(原始通知)时触发网络服务。 后台服务会创建本地 toast 并将其显示在操作通知中心。

 public static void ShowNotification(int notificationId, string ToastTitle, int messageType, string messageDetails)
    {
        string messageText = String.Empty;
        ToastTemplateType toastTemplate = ToastTemplateType.ToastText04;
        XmlDocument toastXml = ToastNotificationManager.GetTemplateContent(toastTemplate);
        XmlNodeList toastTextElements = toastXml.GetElementsByTagName("text");

        toastTextElements[0].AppendChild(toastXml.CreateTextNode(ToastTitle));//Toast notification title
        toastTextElements[1].AppendChild(toastXml.CreateTextNode(messageText));
        toastTextElements[2].AppendChild(toastXml.CreateTextNode(messageDetails));

        var launchAttribute = toastXml.CreateAttribute("launch");
        IXmlNode toastNode = toastXml.SelectSingleNode("/toast");
        ((XmlElement)toastNode).SetAttribute("duration", "short");
        toastNode.Attributes.SetNamedItem(launchAttribute);

        //Launch params
        var toastNavigationUriString = messageDetails;
        var toastElement = ((XmlElement)toastXml.SelectSingleNode("/toast"));
        toastElement.SetAttribute("launch", toastNavigationUriString);

        ToastNotification toast = new ToastNotification(toastXml);
        toast.Tag = notificationId.ToString();
        toast.ExpirationTime = DateTimeOffset.UtcNow.AddDays(3);

        if (true)
        {
            toast.SuppressPopup = false;//to send notification directly to action center without displaying a popup on phone.
        }

        ToastNotificationManager.CreateToastNotifier().Show(toast);
    }

我在 JS 中处理这些祝酒词:

WinJS.Application.addEventListener("activated", onActivatedHandler, true);

function onActivatedHandler(args) {
    if (args.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.launch) {
        messageDetails = args.detail.arguments;
        PhonegapService.setNotificationMessage(messageDetails, function () {
            window.location.href = "index.html";
        });
    }
}

XML 在这种情况下在我的网络服务上使用的格式是:

string rawMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
            "<root>" +
                "<Value1>" + "Hello" + "<Value1>" +
                "<Value2>" + "Raw" + "<Value2>" +
            "</root>";

现在推送通知略有变化。我想直接从我的网络服务发送推送通知(Toasts),而不是发送原始消息。

我的问题是:

  1. 如何在网络服务中的我的 toast 通知上附加 launchmessage,就像我们在创建本地 toast 时所做的那样。
  2. 收到 toast 时如何处理点击事件并获取该通知附带的有用消息。

XML 在这种情况下会是这样的:

string toast1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?> ";

           string toast2 = string.Format(@"<toast>
                       <visual>
                           <binding template=""ToastText04"">
                               <text id=""1"">{0}</text>
                               <launch></launch>
                           </binding>
                       </visual>
                   </toast>",message);
           string xml = toast1 + toast2;

更新 1

我在我的网络服务上使用了以下 XML。但我收到的通知格式与我预期的不同:

 string toast1 = "<?xml version=\"1.0\" encoding=\"utf-8\"?> ";
 string message = "some json";
                string toast2 = string.Format(@"<toast launch= ""{0}"">
                         <visual version=""1"">
                             <binding template=""ToastText02"">
                                 <text id=""1"">{1}</text>
                                   <text id=""2"">{2}</text>
                             </binding>
                         </visual>
                     </toast>", message, "Alert", "Test");

【问题讨论】:

  • 你有双引号 "" "" 而不是单字符引号。如果你使用@,你可以使用单引号:'
  • 哦,我注意到了。但我的 json 是这样的:"{\\\"Message\\\":{\\\"ActionId\\\":\\\"6724d22a-87878789fe-4137-b501-9d9a9a078558b1\\\",\\\"ActionDetailId\\\":\\\"dc02e878784-7832-4625-a538-29be9807f885ccb\\\",\\\"Calamity\\\":\\\"Calamity helo\\\",\\\"UserName\\\":null,\\\"ActionDateTime\\\":\\\"2015-09-15T15:36:14+05:45\\\",\\\"CalamityId\\\":\\\"c0fa848e-ee6c-4b91-8391-a12058f25387\\\",\\\"Calls\\\":true,\\\"IsActionCompleted\\\":false},\\\"Type\\\":\\\"Calamity\\\"}"
  • 好的,现在超出了这个问题的范围,但是你的 json 是无效的。将每个 /// 替换为一个 /
  • 哇,感谢您所做的一切。它工作得很好:)

标签: windows windows-runtime windows-phone-8.1 windows-8.1 winjs


【解决方案1】:

您需要创建一个与以下结构相同的 XML(带或不带自定义音频):

<toast launch=\"$param\">
   <audio src=\"ms-appx:///Assets/Sounds/$sound.wav\"/>
   <visual>
       <binding template=\"ToastText04\">
           <text id=\"1\">$title</text>
           <text id=\"2\">$msg</text>
       </binding>
   </visual>
</toast>

注意launch&lt;toast&gt; 标记的成员。

当应用程序被激活时,你可以像以前一样处理点击事件,你得到的字符串是launch的值。

【讨论】:

  • 我有点困惑。我必须在我的网络服务中这样做吗?
  • 抱歉,是的,您必须使用您的网络服务发送此 xml。
  • 请查看我的问题中的编辑。我在我的问题中添加了更新 1。我按照你说的那样使用了,但我得到了一种不寻常的敬酒方式。有什么问题吗?请看一看。
猜你喜欢
  • 2017-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
相关资源
最近更新 更多