java实现window phone推送通知

package com.windowphone.text;

import java.io.IOException;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.UUID;

public class HttpPost {

 private String xml;
 private String url;

 public HttpPost(String url, String xml) {
  this.xml = xml;
  this.url = url;
 }

 private void Send() {
  
  HttpURLConnection con = null;
  URL url = null;
  try {
   url = new URL(this.url);
   con = (HttpURLConnection) url.openConnection();
   con.setRequestMethod("POST");
   con.setDoOutput(true);
   con.setDoInput(true);
   con.setUseCaches(false);
   
   //全球唯一的ID,类型:eb84a429-1ac6-46e2-b3f3-51929fd17648
   String guid = UUID.randomUUID().toString();  
   con.setRequestProperty("X-MessageID",guid);
   con.setRequestProperty("Content-Type","text/xml;charset=utf-8");
   
   //①Raw Notification模式
   //3:立刻发送 13:等待450秒发送 23:等待900秒发送
   con.setRequestProperty("X-NotificationClass", "3");
   
   
   //②Toast Notification模式
   //2:立刻发送 12:等待450秒发送 22:等待900秒发送
//   con.setRequestProperty("X-WindowsPhone-Target", "toast");
//   con.setRequestProperty("X-NotificationClass", "2");
   
   //③Tile Notification模式
   //1:立刻发送 11:等待450秒发送 21:等待900秒发送
//            con.setRequestProperty("X-WindowsPhone-Target", "token");
//            con.setRequestProperty("X-NotificationClass", "1");
            
   OutputStream out = con.getOutputStream();
   //在此要特别的小心,发送比特流,要把获取字节码改为utf-8,不然中文会乱码
   out.write(this.xml.getBytes("utf-8"));
   out.flush();
   
   //输出微软服务器response的情况,正常输出OK
   System.out.println("response:   "+con.getResponseMessage());
   
   out.close();
   con.disconnect();
  } catch (ConnectException ce) {
  } catch (IOException ie) {
  } catch (Exception e) {
  }
 }

 
 public static void main(String[] args) {
  
  //这里直接复制window phone 应用注册微软的Uri
  String uri = "http://db3.notify.live.net/throttledthirdparty/01.00/AAGKzo1xh_AfR4Ia6ePTklzoAgAAAAADAQAAAAQUZm52OjIzOEQ2NDJDRkI5MEVFMEQ";
  
  ///①Raw Notification模式
  String rawMessage = "hitler 林楚金!";
  
  //②Toast Notification模式,固定模式,Text1和Text2两个参数
  String toastMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
        "<wp:Notification xmlns:wp=\"wpNotification\">" +
        "<wp:Toast>" +
        "<wp:Text1>123</wp:Text1>" +
        "<wp:Text2>林楚金</wp:Text2>" +
        "</wp:Toast>" +
        "</wp:Notification>";
  
  //③Tile Notification模式,固定模式,BackgroundImage背景图片,count数量,Title小标题
  String tileMessage = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
        "<wp:Notification xmlns:wp=\"wpNotification\">" +
        "<wp:Tile>" +
        "<wp:BackgroundImage>/Images/天晴.jpg</wp:BackgroundImage>" +
        "<wp:Count>2</wp:Count>" +
        "<wp:Title>fuck 林楚金</wp:Title>" +
        "</wp:Tile>" +
        "</wp:Notification>";
    
  HttpPost post = new HttpPost(uri,rawMessage);
  
  post.Send();
 }

}

 

posted on 2013-06-18 13:45  针尖上的舞者 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/fish-king/p/3142115.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值