先在极光推送官网上注册一个极光账号,创建应用,
需要ios或安卓人员提供~应用成功创建之后会生成二个字符码.
创建工具类:
需要在工具类中添加相应的jar
package hls.itms.common.general.JPush;
import cn.jiguang.common.ClientConfig;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
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;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
/**
* Created by Administrator on 2017/1/10.
*/
public final class JPushHandler {
/**
* 广播
*
@param
message
*/
public static void broadCast(String appKey, String masterSecret, String title, String message){
ClientConfig config = ClientConfig.
getInstance
();
JPushClient client = new JPushClient(masterSecret, appKey, null, config);
PushPayload payload =
buildBroadCastPayload
(title, message);
PushResult result = null;
try {
result = client.sendPush(payload);
// System.out.println("推送结果:" + result.getResponseCode());
} catch (Exception e1) {
}
}
private static PushPayload buildBroadCastPayload(String title, String message){
return PushPayload.
newBuilder
()
.setPlatform(Platform.
newBuilder
().setAll(true).build())
.setAudience(Audience.
newBuilder
().setAll(true).build())
.setNotification(Notification.
newBuilder
().
addPlatformNotification(IosNotification.
newBuilder
()
.setBadge(1)
.setContentAvailable(false)
.setMutableContent(false)
.setSound("defalut")
.setAlert(message)
.setCategory(title)
.build()
)
.addPlatformNotification(AndroidNotification.
newBuilder
()
.setAlert(message)
.setTitle(title)
.build())
.build())
.setOptions(Options.
newBuilder
()
.setApnsProduction(true)
.setSendno(100000)
.setTimeToLive(86400)
.build()
)
.build();
}
/**
* 推送消息到个人
*
@param
user_id
*
@param
message
*/
public static void pushForOne(String appKey, String masterSecret, Long user_id, String title, String message){
ClientConfig config = ClientConfig.
getInstance
();
JPushClient client = new JPushClient(masterSecret, appKey, null, config);
PushPayload payload =
buildSinglePayload
(user_id, title, message);
PushResult result = null;
try {
result = client.sendPush(payload);
// System.out.println("推送结果:" + result.getResponseCode());
} catch (Exception e1) {
}
}
private static PushPayload buildSinglePayload(Long user_id, String title, String message){
return PushPayload.
newBuilder
()
.setPlatform(Platform.
newBuilder
().setAll(true).build())
.setAudience(Audience.
alias
(user_id + ""))
.setAudience(Audience.
tag
(user_id + ""))
.setNotification(Notification.
newBuilder
().
addPlatformNotification(IosNotification.
newBuilder
()
.setBadge(1)
.setContentAvailable(false)
.setMutableContent(false)
.setSound("defalut")
.setCategory(title)
.setAlert(message)
.build()
)
.addPlatformNotification(AndroidNotification.
newBuilder
()
.setAlert(message)
.setTitle(title)
.build())
.build())
.setOptions(Options.
newBuilder
()
.setApnsProduction(true)
.setSendno(100000)
.setTimeToLive(86400)
.build()
)
.build();
}
}