向WindowsPhone手机Toast推送通知

Posted by baicai on September 15, 2013

启动带参数的进程,将推送信息传递过去:

PushMsg msg = new PushMsg(appid, title, content);
Push push = new Push();
Thread t = new Thread(new ParameterizedThreadStart(push.PushToast));
t.IsBackground = true;
t.Start(msg);
public class Push
{
    public void PushToast(Object o)
    {
        try
        {
            PushMsg msg = o as PushMsg;
            List<string> urllist = MySqlDB.GetPushUrls(msg.appid);
            foreach (var item in urllist)
            {
                try
                {
                    SendToastNotification(getPushXml(msg), item);
                }
                catch { }
            }
        }
        catch { }
    }

    private void SendToastNotification(byte[] Payload, string url)
    {
        // The URI that the Push Notification Service returns to the Push Client when creating a notification channel.
        HttpWebRequest sendNotificationRequest = (HttpWebRequest)WebRequest.Create(url);

        // HTTP POST is the only allowed method to send the notification.
        sendNotificationRequest.Method = WebRequestMethods.Http.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["X-MessageID"] = Guid.NewGuid().ToString();

        // Sets toast notification
        sendNotificationRequest.ContentType = "text/xml; charset=utf-8";
        sendNotificationRequest.Headers.Add("X-WindowsPhone-Target", "toast");
        sendNotificationRequest.Headers.Add("X-NotificationClass", "2");
        // Possible batching interval values:
        // 2: The message is delivered by the Push Notification Service immediately.
        // 12: The message is delivered by the Push Notification Service within 450 seconds.
        // 22: The message is delivered by the Push Notification Service within 900 seconds.

        // Sets the web request content length.
        sendNotificationRequest.ContentLength = Payload.Length;

        // Sets the notification payload to send.
        byte[] notificationMessage = Payload;

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

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

    private byte[] getPushXml(PushMsg msg)
    {
        string toastMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                    "<wp:Notification xmlns:wp=\"WPNotification\">" +
                       "<wp:Toast>" +
                       "<wp:Text1>" + (msg.title == null ? "" : msg.title) + "</wp:Text1>" +
                          "<wp:Text2>" + msg.content + "</wp:Text2>" +
                       "</wp:Toast>" +
                    "</wp:Notification>";
        byte[] strBytes = new UTF8Encoding().GetBytes(toastMessage);
        return strBytes;
    }
}

public class PushMsg
{
    public string appid { get; set; }
    public string title { get; set; }
    public string content { get; set; }

    public PushMsg(string appid, string title, string content)
    {
        this.appid = appid;
        this.title = title;
        this.content = content;
    }
}

实际应用中需要考虑出错重试/抛弃机制以及多线程实现以加快推送速度。