/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ifish.API;

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.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import com.ifish.util.IfishFilePath;
import com.ifish.util.IfishUtil;
import java.util.Map;
import org.springframework.stereotype.Component;

/**
 *
 * @author Administrator
 */
@Component
public class JiGuangPush {

    private static JPushClient jPushClient = null;
    //正式
//    private static final String masterSecret = "60162c8cf195ce9f4dc76629";
//    private static final String appKey = "d970d5e193cb2a0bbe41653c";
    //测试
    private static String masterSecret = "4f759a0609dcd9d2edb06125";
    private static String appKey = "6e5e9d757570859b3f274bb8";

    static {
        if (IfishUtil.isOnLine()) {
            appKey = IfishFilePath.AppKeyFormal;
            masterSecret = IfishFilePath.MasterSecretFormal;
        } else {
            appKey = IfishFilePath.AppKeyFormal;
            masterSecret = IfishFilePath.MasterSecretFormal;
        }
        jPushClient = new JPushClient(masterSecret, appKey);
    }

    /**
     * 向安卓手机推送一条信息
     *
     * @param message
     * @return
     */
    public boolean pushMessageByAndroid(String title, String message, String userId, Map map) {
        try {
            PushPayload payload = PushPayload.newBuilder()
                    .setPlatform(Platform.android())
                    .setAudience(Audience.alias(userId))
                    .setNotification(Notification.android(title, message, map))
                    .setMessage(Message.newBuilder().setTitle(title).setMsgContent(message).addExtras(map).build())
                    .build();

            PushResult result = jPushClient.sendPush(payload);
            if (result.msg_id > 0 && result.isResultOK()) {
                return true;
            }
        } catch (Exception e) {

        }
        return false;
    }

    public boolean pushMessageByIOS(String title, String message, String userId, Map map) {
        try {

            PushPayload payload = PushPayload.newBuilder()
                    .setPlatform(Platform.ios())
                    .setAudience(Audience.alias(userId))
                    .setNotification(Notification.newBuilder().addPlatformNotification(IosNotification.newBuilder().setSound("happy").setAlert(message).build()).build())
                    .setMessage(Message.newBuilder().setTitle(title).setMsgContent(message).addExtras(map).build())
                    .setOptions(Options.newBuilder().setApnsProduction(false).build())
                    .build();

            PushResult result = jPushClient.sendPush(payload);
            if (result.msg_id > 0 && result.isResultOK()) {
                return true;
            }
        } catch (Exception e) {
            String b = e.getMessage();
            String a = "";
        }
        return false;
    }

}
