package org.cs.util;

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

import org.apache.log4j.Logger;

import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Options;
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.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

/**
 * 极光推送
 * @author uu
 *
 */
public class JPush {
	private static Logger log = Logger.getLogger(JPush.class);
	private static final String appKey = "ebe7e692869bcc5b955286b6";
	private static final String masterSecret = "ec2b5744f9b1e9ad7201e66c"; 
	private static boolean APNS_PRODUCTION=true;//true 生产环境，false 开发环境
	
	public static void main(String[] args) {
		/*Map<String,String> param = new HashMap<String, String>();
		param.put("id", "1");
		JPush.sendByClientId("云调味", "异地登陆1", new String[]{"141fe1da9ea3a8241bc"}, param,0);*/
		
		//给原登录设备推送消息，如在线，则进行提示并退出操作
		Map<String,String> param = new HashMap<String,String>();
		param.put("type", "10");
		param.put("phone", "17839603577");
		param.put("userId", "16");

		try {
//			JPush.sendByClientId("东旭通", "订单浩子的试2332！", new String[]{"171976fa8ad0e831186"}, param,0);
			JPush.sendByClientId("东旭通", "测试消息", new String[]{"100d8559091d030d36e"},param, 0);
		} catch (Exception e) {
			log.info("17839603577推送失败。推送内容：您的账号已在别处登录。失败原因:"+e.getMessage());
		}finally{

		}

		/*Map<String,String> param = new HashMap<String,String>();
		param.put("type", "10");
		param.put("id", "1201");
		param.put("content", "【测试数据，请忽略】");
		JPush.sendAll(param);*/
		
		/*Map<String, String> param = new HashMap<String, String>();
		param.put("type", "0");  //系统消息
		param.put("title", "恒之源心意礼物");
    	param.put("content", "您好！感谢您对恒之源心意礼物的支持与帮助");
		JPush.sendAll(param);*/
	}
	
	public static void sendAll(Map<String,String> param){
		JPushClient jpushClient = new JPushClient(masterSecret, appKey);
		PushResult result = null;
		PushPayload payload = buildPushObject_all_alertWithExtras(param);
		try {
			result = jpushClient.sendPush(payload);
		    if (result.isResultOK()) {
		    	log.info(result.toString());
	        } else {
	        	log.info("Maybe connect error. Retry laster.");
	        }
		} catch (Exception e) {
			log.info("推送失败，失败原因:"+e.getMessage());
		}
	}
	
	/**
	 * 根据clientId批量给在线用户推送消息
	 * @param title 通知标题
	 * @param text 通知内容
	 * @param sendId 设备ID
	 * @param param 附加参数
	 * @param badge ios icon显示数
	 */
	public static void sendByClientId(String title, String text, String[] sendId,Map<String,String> param,int badge) {
        JPushClient jpushClient = null;
        PushPayload payload = null;
        PushResult result=null;
    	jpushClient = new JPushClient(masterSecret, appKey);
    	payload = buildPushObject_alertWithExtrasAndMessage(title, text, sendId,param,badge);
		try {
			System.out.println("---payload---"+payload.toString());
			result = jpushClient.sendPush(payload);
			System.out.println("---result---"+result.toString());
		} catch (Exception e) {
			System.out.println(e.getMessage());
			log.error("推送失败，失败原因:"+e.getMessage());
		} 
        if (result.isResultOK()) {
        	log.info(result.toString());
        } else {
        	log.info(result.toString());
        }
	}
	//不保存离线消息
	private static PushPayload buildPushObject_alertWithExtrasAndMessage(String title, String text, String[] sendId,Map<String,String> param,int badge) {
        return PushPayload.newBuilder()
                .setPlatform(Platform.android_ios())
                .setAudience(Audience.registrationId(sendId))
                .setNotification(Notification.newBuilder()
                    .addPlatformNotification(IosNotification.newBuilder()
                            .setAlert(text)
                            .addExtras(param)
                            .setBadge(badge)
                            //.setSound("default")
                            .setSound("happy")
                            .build())
                    .addPlatformNotification(AndroidNotification.newBuilder()
                    		.setAlert(text)
                    		.addExtras(param)
                    		.build())
                    .build())
//                .setMessage(Message.newBuilder()
//                		.addExtras(param)
//                		.setMsgContent(text)
//                		.build())
                .setOptions(Options.newBuilder()
                		.setTimeToLive(0)
                		.setApnsProduction(APNS_PRODUCTION)
                		.build())
                .build();
    }
	
	private static PushPayload buildPushObject_all_alertWithExtras(Map<String,String> param){
		return PushPayload.newBuilder()
                .setPlatform(Platform.all())
                .setAudience(Audience.all())
                .setNotification(Notification.newBuilder()
                        .addPlatformNotification(IosNotification.newBuilder()
                                .setAlert(param.get("content"))
                                .addExtras(param)
                                .setBadge(0)
                                .build())
                        .addPlatformNotification(AndroidNotification.newBuilder()
                        		.setAlert(param.get("content"))
                        		.addExtras(param)
                        		.build())
                        .build())
               /* .setMessage(Message.newBuilder()
                		.addExtras(param)
                		.setMsgContent(param.get("content"))
                		.build())*/
                .setOptions(Options.newBuilder()
                		.setTimeToLive(0)
                		.setApnsProduction(APNS_PRODUCTION)
                		.build())
                .build();
	}
}
