发送磁贴通知 - Windows Phone

时间:2013-03-21 15:02:54

标签: c# windows-phone-7

直升机, 我想问你,我是否可以使用WCF服务从一部手机向另一部手机发送平铺通知,反之亦然? 谢谢

1 个答案:

答案 0 :(得分:2)

要将推送发送到其他手机,您必须拥有ChannelURI。关于它的文章很好 http://msdn.microsoft.com/en-us/library/hh221549.aspx

当你有这个地址时,你必须发送特殊的XML数据

        string textTitle = tbxTitle.Text;
        string textSubtitle = tbxSubtitle.Text;

        string deviceUri = tbxUri.Text;

        string msg =
            "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
            "<wp:Notification xmlns:wp=\"WPNotification\">" +
                "<wp:Toast>" +
                "<wp:Text1>" + textTitle + "</wp:Text1>" +
                "<wp:Text2>" + textSubtitle + "</wp:Text2>" +
                "</wp:Toast>" +
            "</wp:Notification>";

        byte[] msgBytes = new UTF8Encoding().GetBytes(msg);

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(channelUri);
        request.Method = WebRequestMethods.Http.Post;
        request.ContentType = "text/xml";
        request.ContentLength = msg.Length;
        request.Headers["X-MessageID"] = Guid.NewGuid().ToString();
        request.Headers["X-WindowsPhone-Target"] = "toast";
        request.Headers["X-NotificationClass"] = "2";

        Stream requestStream = request.GetRequestStream();
        requestStream.Write(msgBytes, 0, msgBytes.Length);
        requestStream.Close();