package com.techec.controller.common;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
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.HashMap;
import java.util.Map;
/**
* @author yf
**/
public class JPushDemo {
protected static final String APP_KEY = "ak";
protected static final String MASTER_SECRET = "sk";
public static void main(String[] args) {
HashMap<String, String> stringStringHashMap = new HashMap<String, String>();
stringStringHashMap.put("msg", "您的订单已被接单");
jpushAndroid(stringStringHashMap);
jpushIOS(stringStringHashMap);
}
public static void jpushAndroid(Map<String, String> parm) {
// 设置好账号的app_key和masterSecret
String appKey = APP_KEY;
String masterSecret = MASTER_SECRET;
//创建JPushClient(极光推送的实例)
JPushClient jpushClient = new JPushClient(masterSecret, appKey);
//推送的关键,构造一个payload
PushPayload payload = PushPayload.newBuilder()
.setPlatform(Platform.android())//指定android平台的用户
.setAudience(Audience.registrationId("1104a89792fda047aff"))//你项目中的所有用户,Audience.all()
.setNotification(Notification.android(parm.get("msg"), parm.get("title"), parm))
//发送内容,这里不要盲目复制粘贴,这里是我从controller层中拿过来的参数)
.setOptions(Options.newBuilder().setApnsProduction(false).build())
//这里false指定开发环境,true生产环境
.setMessage(Message.content(parm.get("msg")))//自定义信息
.build();
try {
PushResult pu = jpushClient.sendPush(payload);
} catch (APIConnectionException e) {
e.printStackTrace();
} catch (APIRequestException e) {
e.printStackTrace();
}
}
public static void jpushIOS(Map<String, String> parm) {
// 设置好账号的app_key和masterSecret是必须的
String appKey = APP_KEY;
String masterSecret = MASTER_SECRET;
//创建JPushClient
JPushClient jpushClient = new JPushClient(masterSecret, appKey);
PushPayload payload = PushPayload.newBuilder()
.setPlatform(Platform.android_ios())//ios平台的用户
.setAudience(Audience.all())//所有用户
.setNotification(Notification.newBuilder()
.addPlatformNotification(IosNotification.newBuilder()
.setAlert(parm.get("msg"))
.setBadge(+1)
.setSound("happy")//这里是设置提示音(更多可以去官网看看)
.addExtras(parm)
.build())
.build())
.setOptions(Options.newBuilder().setApnsProduction(false).build())
.setMessage(Message.newBuilder().setMsgContent(parm.get("msg")).addExtras(parm).build())//自定义信息
.build();
try {
PushResult pu = jpushClient.sendPush(payload);
} catch (APIConnectionException e) {
e.printStackTrace();
} catch (APIRequestException e) {
e.printStackTrace();
}
}
}