sending a tile and toast notification through at t

2019-08-03 17:53发布

问题:

i have this desktop application that i created through a MSDN tutorial (i cant find the link, otherwise i would have linked it). what it does , it lets you put in the channel URL needed to connect to the WIndows Phone Emulator, and through the application you can send live tile updates, : here is the code for both the emulator (listener), and the service sending the data. :

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

}

here is service :


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

    }

now as you can see there is a part of code that says "//Create the toast message",

how do i get that toast to be sent with the tile update? so that when the toast message gets through, the tile updates aswell with the same info i types in the page.

at the moment only the tile gets updated.

here is a picture of the main page aswell.

im very sorry about the long post , but i want to give as much info as possible.

using - Visual Studio 2012/Windows Phone 8/C#/Silverlight

Thanks In Advance.

回答1:

Is this it?

Combine your xml payloads into one

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>";

You won't see a toast notification when your application is open. Listen for the PushNotificationReceived event to intercept push notifications when your application is running.