通过同时发送磁贴和Toast通知?

时间:2013-01-28 10:33:07

标签: c# push-notification windows-phone-8 mpns

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

MainApp - Emultor:

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();
        }

    }

现在您可以看到有一部分代码表示&#34; //创建Toast消息&#34;,

如何通过磁贴更新发送吐司?这样,当toast消息通过时,tile会更新,并在页面中输入相同的信息。

目前只有磁贴更新。

这里是主页的图片。

Main Page

对于长篇文章非常抱歉,但我想尽可能多地提供信息。

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

提前致谢。

1 个答案:

答案 0 :(得分:2)

this it

将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事件以拦截推送通知。