天天看点

Umeng推送实例

//定义常量参数

protected final String USER_AGENT = "Mozilla/5.0";
	protected static final String host = "http://msg.umeng.com";
	protected static final String postPath = "/api/send";
	private static  String appkey_Android = "5d788XXXXX";
	private static  String appMasterSecret_Android = "3bmky3ofaycmeurfextXXXX";
           
public static Boolean sendAndroidCustomizedcast(String alias_Type, String alias,
                                                    String title, String ticker, String text, String msgtype, String msgid, String type, JSONObject obj) throws Exception {

		AndroidCustomizedcast customizedcast = new AndroidCustomizedcast(appkey_Android,appMasterSecret_Android);
	    customizedcast.setAlias( alias, alias_Type);
		customizedcast.setPredefinedKeyValue("alias", alias);
		customizedcast.setPredefinedKeyValue("alias_type", alias_Type);
		customizedcast.setPredefinedKeyValue("mi_activity", "com.qiaqia.app.push.MipushTestActivity");
		customizedcast.setPredefinedKeyValue("mipush", "true");
		customizedcast.setTitle(title);
		customizedcast.setTicker(ticker);
		customizedcast.setText(text);
		customizedcast.goAppAfterOpen();
		customizedcast.setPredefinedKeyValue("timestamp", Integer.toString((int) (System.currentTimeMillis() / 1000)));
		customizedcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
		customizedcast.setProductionMode(true);
		customizedcast.setExtraField("msgtype", msgtype);
		customizedcast.setExtraField("msgid", msgid);
		customizedcast.setExtraField("type", type);
		if(obj!=null){
			customizedcast.setExtraField("obj", obj.toString());
		}
		Boolean flag = client.send(customizedcast);

		return flag;
	}
           

send方法

public boolean send(UmengNotification msg) throws Exception {
		String timestamp = Integer.toString((int) (System.currentTimeMillis() / 1000));
		msg.setPredefinedKeyValue("timestamp", timestamp);
		String url = host + postPath;
		String postBody = msg.getPostBody();
		String sign = DigestUtils.md5Hex(("POST" + url + postBody + msg.getAppMasterSecret()).getBytes("utf8"));
		url = url + "?sign=" + sign;
		HttpPost post = new HttpPost(url);
		post.setHeader("User-Agent", USER_AGENT);
		StringEntity se = new StringEntity(postBody, "UTF-8");
		post.setEntity(se);
		// Send the post request and get the response
		HttpResponse response = client.execute(post);
		int status = response.getStatusLine().getStatusCode();
		System.out.println("Response Code : " + status);
		BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
		StringBuffer result = new StringBuffer();
		String line = "";
		while ((line = rd.readLine()) != null) {
			result.append(line);
		}
		System.out.println(result.toString());
		if (status == 200) {
			System.out.println("Notification sent successfully.");
		} else {
			System.out.println("Failed to send the notification!");
		}
		return true;
	}
           
public enum DisplayType {
		NOTIFICATION {
			public String getValue() {
				return "notification";
			}
		}, /// 通知:消息送达到用户设备后,由友盟SDK接管处理并在通知栏上显示通知内容。
		MESSAGE {
			public String getValue() {
				return "message";
			}
		};/// 消息:消息送达到用户设备后,消息内容透传给应用自身进行解析处理。
		public abstract String getValue();
	}