极光推送示例

package com.wlkj.cxf.jpush;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jws.WebService;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.Notification;
import cn.jpush.api.report.ReceivedsResult;
import cn.jpush.api.report.ReceivedsResult.Received;
import cn.jpush.api.report.ReportClient;

import com.jfinal.kit.PropKit;

@WebService
public class JPushImpl implements JPushInterface {
	static final Logger logger = Logger.getLogger(JPushImpl.class);
	public static JPushClient jpushClient = new JPushClient(PropKit.use("config.properties").get("masterSecret"), PropKit.use("config.properties").get("appKey"));
	public JPushClient jpushClient_Android = new JPushClient("2587ca7xxx13ae8XXXXXea677", "f2dfeXXXXXXXd4d951");
	public static ReportClient reportClient = new ReportClient(PropKit.use("config.properties").get("masterSecret"), PropKit.use("config.properties").get("appKey"));
	PushResult result_all = null;
	PushResult result_ios = null;

	@Override
	// 是否送达
	public boolean pushReport(String msg_id) {
		try {
			ReceivedsResult rs = reportClient.getReceiveds(msg_id);
			List lists = rs.received_list;
			for (Received received : lists) {
				int num = received.ios_apns_sent;
				if (num > 0) {
					return true;
				} else {
					return false;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return false;
	}

	@Override
	// 苹果自定义消息是否送达
	public boolean pushMessageIosReport(String msg_id) {
		try {
			ReceivedsResult rs = reportClient.getReceiveds(msg_id);
			List lists = rs.received_list;
			for (Received received : lists) {
				int num = received.ios_msg_received;
				if (num > 0) {
					return true;
				} else {
					return false;
				}
			}
		} catch (APIConnectionException e) {
			e.printStackTrace();
		} catch (APIRequestException e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	// 根据组织id推送消息
	public String pushMsgByOrgId(String param) {
		JSONObject jo = JSONObject.fromObject(param);
		Map extras = (Map) jo.get("extras");
		String org_id = extras.get("org_id") ;
		PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.android_ios()).setAudience(Audience.tag(org_id))
				.setMessage(Message.newBuilder().setMsgContent("智慧党务消息平台").addExtras(extras).build()).build();
		try {
			result_all = jpushClient.sendPush(payload);
			return Long.toString(result_all.msg_id);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	// 根据党员id推送消息
	public String pushMsgByMemberId(String param) {
		JSONObject jo = JSONObject.fromObject(param);
		Map extras = (Map) jo.get("extras");
		String member_id = extras.get("member_id");
		PushPayload payload = PushPayload.newBuilder().setPlatform(Platform.android_ios()).setAudience(Audience.alias(member_id))
				.setMessage(Message.newBuilder().setMsgContent("智慧党务消息平台").addExtras(extras).build()).build();
		try {
			result_all = jpushClient.sendPush(payload);
			return Long.toString(result_all.msg_id);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	// 党员政治生日提醒Android
	@Override
	public String pushAliasAndroid(String param) {
		System.out.println(param);
		JSONObject jo = JSONObject.fromObject(param);
		String[] aliasArr = new String[]{jo.getString("alias")};
		String alert = jo.getString("alert");
		String title = jo.getString("title");
		Map extras = (Map) jo.get("extras");
		
		PushPayload payload_android = PushPayload.newBuilder().setPlatform(Platform.android())
				.setAudience(Audience.alias(aliasArr))
				.setNotification(Notification.android(alert, title, extras))
				.build();
		try {
			result_all = jpushClient_Android.sendPush(payload_android);
			return Long.toString(result_all.msg_id);
		} catch (Exception e) {
			e.printStackTrace();
			return null; 
		} 
		
	}
	
	// 党员政治生日提醒IOS
	@Override
	public String pushAliasIos(String param) {
		System.out.println(param);
		JSONObject jo = JSONObject.fromObject(param);
		String[] aliasArr = new String[]{jo.getString("alias")};
		String alert = jo.getString("alert");
		Map extras = (Map) jo.get("extras");
		PushPayload payload_ios = PushPayload.newBuilder().setPlatform(Platform.ios())
				.setAudience(Audience.alias(aliasArr))
				.setNotification(Notification.ios(alert, extras)).build();
		
		try {
			result_ios = jpushClient.sendPush(payload_ios);
			return Long.toString(result_ios.msg_id);
		} catch (Exception e) {
			e.printStackTrace();
			return null; 
		} 
	}
}

你可能感兴趣的:(代码封装)