ifish_english/src/main/java/com/ifish/API/JiGuangPush.java

87 lines
3.0 KiB
Java

/*
* 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 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 final static String masterSecret = "4f759a0609dcd9d2edb06125";
private final static String appKey = "6e5e9d757570859b3f274bb8";
static {
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;
}
}