如何在Windows Phone 8.1运行时应用程序(不是silverlight)中发送推送?

时间:2015-08-10 08:41:16

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

我正在开发windows phone app,在这个应用程序中我想开发发送pushnotifications for devices.

以下是我的代码

string subscriptionUri =" http://s.notify.live.net/u/1/hk2/H2QAAADrEJ2fJgP0eNwNQ5Xp139q1mNu9TCpEwwxjX6j1&#34 ;;     var sendNotificationRequest =(HttpWebRequest)WebRequest.Create(subscriptionUri);

// 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.
var toastMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
   "<wp:Notification xmlns:wp=\"WPNotification\">" +
      "<wp:Toast>" +
           "<wp:Text1>" + message + "</wp:Text1>" +
      "</wp:Toast> " +
   "</wp:Notification>";
// Set the notification payload to send.

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

// Set the web request content length.
sendNotificationRequest.ContentLength = notificationMessage.Length;
sendNotificationRequest.ContentType = "text/xml";
sendNotificationRequest.Headers.Add("X-WindowsPhone-Target", "toast");
sendNotificationRequest.Headers.Add("X-NotificationClass", "2");

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

// Send the notification and get the response.
var response = (HttpWebResponse)sendNotificationRequest.GetResponse();
var notificationStatus = response.Headers["X-NotificationStatus"];
var notificationChannelStatus = response.Headers["X-SubscriptionStatus"];
var 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.
var rep = notificationStatus + " | " + deviceConnectionStatus + " | " + notificationChannelStatus;
return rep;

上面的代码我发送了

的deviceid

string subscriptionUri =&#34; http://s.notify.live.net/u/1/hk2/H2QAAADrEJ2fJgP0eNwNQ5Xp139q1mNu9TCpEwwxjX6j1&#34;

var sendNotificationRequest = (HttpWebRequest)WebRequest.Create(subscriptionUri);

// 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.
var toastMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
   "<wp:Notification xmlns:wp=\"WPNotification\">" +
      "<wp:Toast>" +
           "<wp:Text1>" + message + "</wp:Text1>" +
      "</wp:Toast> " +
   "</wp:Notification>";
// Set the notification payload to send.

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

// Set the web request content length.
sendNotificationRequest.ContentLength = notificationMessage.Length;
sendNotificationRequest.ContentType = "text/xml";
sendNotificationRequest.Headers.Add("X-WindowsPhone-Target", "toast");
sendNotificationRequest.Headers.Add("X-NotificationClass", "2");

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

// Send the notification and get the response.
var response = (HttpWebResponse)sendNotificationRequest.GetResponse();
var notificationStatus = response.Headers["X-NotificationStatus"];
var notificationChannelStatus = response.Headers["X-SubscriptionStatus"];
var 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.
var rep = notificationStatus + " | " + deviceConnectionStatus + " | " + notificationChannelStatus;
return rep;

但每当响应其投掷错误时(400错误)。

我从以下代码获取SubscriptionUri:

var channelOperation = Windows.Networking.PushNotifications.PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();

请帮帮我。如何send pushnotifications for the devices.

0 个答案:

没有答案