1

我有这个通过 MSDN 教程创建的桌面应用程序(我找不到链接,否则我会链接它)。它做了什么,它允许您输入连接到 WIndows Phone Emulator 所需的频道 URL,并通过该应用程序您可以发送实时磁贴更新,:这是模拟器(侦听器)和发送服务的代码数据。:

MainApp - 仿真器:

public MainPage()
    {

        HttpNotificationChannel pushChannel;
        string channelName = "TileSampleChannel";
        InitializeComponent();

        pushChannel = HttpNotificationChannel.Find(channelName);

        if (pushChannel == null)
        {
            pushChannel = new HttpNotificationChannel(channelName);

            pushChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(PushChannel_ChannelUriUpdated);
            pushChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(PushChannel_ErrorOccurred);

            pushChannel.Open();
            pushChannel.BindToShellTile();


        }
        else
        {
            pushChannel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(PushChannel_ChannelUriUpdated);
            pushChannel.ErrorOccurred += new EventHandler<NotificationChannelErrorEventArgs>(PushChannel_ErrorOccurred);

            System.Diagnostics.Debug.WriteLine(pushChannel.ChannelUri.ToString());
            MessageBox.Show(String.Format("Channel Uri is {0}",
                pushChannel.ChannelUri.ToString()));

        }
    }


    void PushChannel_ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e)
    {

        Dispatcher.BeginInvoke(() =>
        {
            System.Diagnostics.Debug.WriteLine(e.ChannelUri.ToString());

            //MessageBox.Show(String.Format("Channel Uri is {0}",
            //    e.ChannelUri.ToString()));

        });
    }

    void PushChannel_ErrorOccurred(object sender, NotificationChannelErrorEventArgs e)
    {
        Dispatcher.BeginInvoke(() =>
            MessageBox.Show(String.Format("A push notification {0} error occurred.  {1} ({2}) {3}",
                e.ErrorType, e.Message, e.ErrorCode, e.ErrorAdditionalData))
                );
    }

}

这里是服务:


protected void ButtonSendTile_Click(object sender, EventArgs e)
    {


        try
        {
            string NotifyURI = TextBoxUri.Text.ToString();


            HttpWebRequest sendNotificationRequest = (HttpWebRequest)WebRequest.Create(NotifyURI);

            sendNotificationRequest.Method = "POST";


            //Create the toast message

            string notificationData = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
            "<wp:Notification xmlns:wp=\"WPNotification\">" +
            "<wp:Toast>" +
            "<wp:Text1>WP8 TOAST</wp:Text1>" +
            "<wp:Text2>" + TextBoxBackContent.Text + "</wp:Text2>" +
            "</wp:Toast>" +
            "</wp:Notification>";

            // Create the tile message.
            string tileMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
            "<wp:Notification xmlns:wp=\"WPNotification\">" +
                "<wp:Tile>" +
                  "<wp:BackgroundImage>" + TextBoxBackgroundImage.Text + "</wp:BackgroundImage>" +
                  "<wp:Count>" + TextBoxCount.Text + "</wp:Count>" +
                  "<wp:Title>" + TextBoxTitle.Text + "</wp:Title>" +
                  "<wp:BackBackgroundImage>" + TextBoxBackBackgroundImage.Text + "</wp:BackBackgroundImage>" +
                  "<wp:BackTitle>" + TextBoxBackTitle.Text + "</wp:BackTitle>" +
                  "<wp:BackContent>" + TextBoxBackContent.Text + "</wp:BackContent>" +
               "</wp:Tile> " +
            "</wp:Notification>";

            byte[] notificationMessage = Encoding.Default.GetBytes(tileMessage);


            sendNotificationRequest.ContentLength = notificationMessage.Length;
            sendNotificationRequest.ContentType = "text/xml";
            sendNotificationRequest.Headers.Add("X-WindowsPhone-Target", "token");
            sendNotificationRequest.Headers.Add("X-NotificationClass", "1");


            using (Stream requestStream = sendNotificationRequest.GetRequestStream())
            {
                requestStream.Write(notificationMessage, 0, notificationMessage.Length);
            }


            HttpWebResponse response = (HttpWebResponse)sendNotificationRequest.GetResponse();
            string notificationStatus = response.Headers["X-NotificationStatus"];
            string notificationChannelStatus = response.Headers["X-SubscriptionStatus"];
            string deviceConnectionStatus = response.Headers["X-DeviceConnectionStatus"];


            TextBoxResponse.Text = notificationStatus + " | " + deviceConnectionStatus + " | " + notificationChannelStatus;
        }
        catch (Exception ex)
        {
            TextBoxResponse.Text = "Exception caught sending update: " + ex.ToString();
        }

    }

现在你可以看到有一部分代码说“//创建吐司消息”,

我如何让 toast 与磁贴更新一起发送?这样当 toast 消息通过时,磁贴也会更新为我在页面中键入的相同信息。

目前只有瓷砖得到更新。

这是主页的图片。

主页

我对这篇长篇文章感到非常抱歉,但我想提供尽可能多的信息。

使用 - Visual Studio 2012/Windows Phone 8/C#/Silverlight

提前致谢。

4

1 回答 1

2

这个吗?

将您的 xml 有效负载合二为一

string notificationData = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
        "<wp:Notification xmlns:wp=\"WPNotification\">" +
        "<wp:Toast>" +
        "<wp:Text1>WP8 TOAST</wp:Text1>" +
        "<wp:Text2>" + TextBoxBackContent.Text + "</wp:Text2>" +
        "</wp:Toast>" +
        "<wp:Notification xmlns:wp=\"WPNotification\">" +
            "<wp:Tile>" +
              "<wp:BackgroundImage>" + TextBoxBackgroundImage.Text + "</wp:BackgroundImage>" +
              "<wp:Count>" + TextBoxCount.Text + "</wp:Count>" +
              "<wp:Title>" + TextBoxTitle.Text + "</wp:Title>" +
              "<wp:BackBackgroundImage>" + TextBoxBackBackgroundImage.Text + "</wp:BackBackgroundImage>" +
              "<wp:BackTitle>" + TextBoxBackTitle.Text + "</wp:BackTitle>" +
              "<wp:BackContent>" + TextBoxBackContent.Text + "</wp:BackContent>" +
           "</wp:Tile> " +
        "</wp:Notification>";

当您的应用程序打开时,您不会看到 toast 通知。侦听PushNotificationReceived事件以在应用程序运行时拦截推送通知。

于 2013-01-28T14:37:01.357 回答