Java windows通知_java实现window phone推送通知

这段Java代码展示了如何创建一个HttpPost类,用于向Windows Phone设备发送不同类型的推送通知,包括Raw、Toast和Tile模式。通过设置HTTP请求头并构造XML格式的通知内容,程序能够实现与微软服务器的通信,从而实现即时或定时发送通知。
摘要由CSDN通过智能技术生成

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\"?>" +

"" +

"" +

"123" +

"林楚金" +

"" +

"";

//③Tile Notification模式,固定模式,BackgroundImage背景图片,count数量,Title小标题

String tileMessage = "<?xml  version=\"1.0\" encoding=\"utf-8\"?>" +

"" +

"" +

"/Images/天晴.jpg" +

"2" +

"

fuck 林楚金" +

"" +

"";

HttpPost post = new HttpPost(uri,rawMessage);

post.Send();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值