Java集成个推简单推送
1、注册个推账号创建应用。
3、集成方式
方法一:maven方式安装
将下边的依赖放到maven项目的 pom.xml 中:
<dependency>
<groupId>com.gexin.platform</groupId>
<artifactId>gexin-rp-sdk-http</artifactId>
<version>4.1.1.2</version>
</dependency>
然后再增加一个repository到 pom.xml 中:
<repositories>
<repository>
<id>getui-nexus</id>
<url>http://mvn.gt.igexin.com/nexus/content/repositories/releases/</url>
</repository>
</repositories>
方法二:jar包方式安装
1.下载工具包
下载服务端SDK开发工具包,下载地址为:下载地址
2.导入依赖jar包
你的工程需要依赖“…\GETUI_SERVER_SDK\资源文件”目录下的所有jar包,如果使用IDEA的话,导入jar包后,如下图所示:
4、springboot 配置文件配置个推控制台的appId、等。
5、扫描配置文件加载到spring容器
/**
* @author Created by John on 2020/1/27
*/
@Data
@Component
@PropertySource("classpath:config/app.properties")
@ConfigurationProperties(prefix = "push.getui")
public class GeTuiProperties {
private String logUrl;
private String appId;
private String appKey;
private String masterSecret;
private String url;
}
6、注入Bean
/**
* 启用个推推送服务
*/
@Bean
@ConditionalOnProperty(prefix = "push.getui", name = "getuiAccessKeySecret")
@ConditionalOnMissingBean(GeTuiServer.class)
public GeTuiServer geTuiServer(GeTuiProperties properties) {
if (log.isDebugEnabled()) {
log.info("启用个推推送服务...");
}
return new GeTuiServer(properties);
}
7、个推service服务类
/**
* @author Created by John on 2020/1/27
*/
@Slf4j
@Service
public class GeTuiServer {
private static String url;
private static String appId;
private static String logUrl;
private static String appKey;
private static String masterSecret;
public GeTuiServer(GeTuiProperties properties) {
url = properties.getUrl();
appId = properties.getAppId();
appKey = properties.getAppKey();
logUrl = properties.getLogUrl();
masterSecret = properties.getMasterSecret();
}
public void pushToSingle(String toID, String title, String text) {
IGtPush push = new IGtPush(url, appKey, masterSecret);
TransmissionTemplate template = transmissionTemplate(toID, title, text);
SingleMessage message = new SingleMessage();
// 开启离线 为 true
message.setOffline(true);
// 离线有效时间,单位为毫秒
message.setOfflineExpireTime(24 * 3600 * 1000);
message.setData(template);
// 可选,1为wifi,0为不限制网络环境。根据手机处于的网络情况,决定是否下发
message.setPushNetWorkType(0);
Target target = new Target();
target.setAppId(appId);
target.setClientId(toID);
IPushResult ret = null;
try {
ret = push.pushMessageToSingle(message, target);
} catch (RequestException e) {
e.printStackTrace();
ret = push.pushMessageToSingle(message, target, e.getRequestId());
}
if (ret != null) {
log.info("------> 调用个推推送给用户【{}】回调结果【{}】。", toID, ret.getResponse().toString());
} else {
log.error("------> 调用个推推送给用户【{}】的【{}】信息出现异常。", toID, text);
}
}
public void pushAll(String title, String text) {
IGtPush push = new IGtPush(url, appKey, masterSecret);
// STEP4:选择通知模板
TransmissionTemplate template = new TransmissionTemplate();
template.setAppId(appId);
template.setAppkey(appKey);
template.setTransmissionType(2);
template.setTransmissionContent("");
Notify notify = new Notify();
notify.setTitle(title);
notify.setContent(text);
notify.setIntent("intent:#Intent;launchFlags=0x10000000;package=com.pp.yl;"
+ "component=***.**" // Android 包名
+ "/com.getui.demo.MainActivity;i.parm1=12;end");
notify.setType(GtReq.NotifyInfo.Type._intent);
template.set3rdNotifyInfo(notify);
template.setAPNInfo(getAPNPayload("+1", text));
// 定义"AppMessage"类型消息对象,设置推送消息有效期等推送参数
List<String> appIds = new ArrayList<String>();
appIds.add(appId);
AppMessage message = new AppMessage();
message.setData(template);
message.setAppIdList(appIds);
message.setOffline(true);
// 时间单位为毫秒
message.setOfflineExpireTime(1000 * 600);
// 执行推送
IPushResult ret = push.pushMessageToApp(message);
log.info("------> 调用个推实行推送给全部用户回调结果【{}】。", ret.getResponse().toString());
}
public static NotificationTemplate getNotificationTemplate(String title, String text) {
NotificationTemplate template = new NotificationTemplate();
// 设置APPID与APPKEY
template.setAppId(appId);
template.setAppkey(appKey);
// 配置通知栏网络图标
template.setLogoUrl(logUrl);
// 设置通知是否响铃,震动,或者可清除
template.setIsRing(true);
template.setIsVibrate(true);
template.setIsClearable(true);
template.setTitle(title);
template.setText(text);
Map<String, Object> map = new HashMap<>();
map.put("title", title);
map.put("content", text);
map.put("payload", text);
// 透传消息接受方式设置,1:立即启动APP,2:客户端收到消息后需要自行处理
template.setTransmissionContent(String.valueOf(map));
// 透传消息接受方式设置,1:立即启动APP,2:客户端收到消息后需要自行处理
template.setTransmissionType(0);
template.setAPNInfo(getAPNPayload("", text));
return template;
}
public static TransmissionTemplate transmissionTemplate(String toID, String title, String text) {
TransmissionTemplate template = new TransmissionTemplate();
template.setAppId(appId);
template.setAppkey(appKey);
template.setTransmissionType(2);
template.setTransmissionContent("");
Notify notify = new Notify();
notify.setTitle(title);
notify.setContent(text);
notify.setIntent("intent:#Intent;launchFlags=0x10000000;package=com.pp.yl;"
+ "component=***.**" // Android 包名
+ "/com.getui.demo.MainActivity;i.parm1=12;end");
notify.setType(GtReq.NotifyInfo.Type._intent);
template.set3rdNotifyInfo(notify);
String count = JedisUtil.getJson(Constant.USER_CLIENT_ + toID);
template.setAPNInfo(getAPNPayload(count, text));
//设置第三方通知
return template;
}
private static APNPayload getAPNPayload(String count, String text) {
APNPayload payload = new APNPayload();
//在已有数字基础上加1显示,设置为-1时,在已有数字上减1显示,设置为数字时,显示指定数字
payload.setAutoBadge(count);
payload.setContentAvailable(0);
//ios 12.0 以上可以使用 Dictionary 类型的 sound
payload.setCategory("$由客户端定义");
payload.setSound("default");
payload.addCustomMsg("text", text);
//简单模式APNPayload.SimpleMsg
payload.setAlertMsg(new APNPayload.SimpleAlertMsg(text));
//payload.setAlertMsg(getDictionaryAlertMsg());
// 字典模式使用APNPayload.DictionaryAlertMsg
//设置语音播报类型,int类型,0.不可用 1.播放body 2.播放自定义文本
payload.setVoicePlayType(2);
//设置语音播报内容,String类型,非必须参数,用户自定义播放内容,仅在voicePlayMessage=2时生效
//注:当"定义类型"=2, "定义内容"为空时则忽略不播放
payload.setVoicePlayMessage("定义内容");
// 添加多媒体资源
payload.addMultiMedia(new MultiMedia().setResType(MultiMedia.MediaType.pic)
.setResUrl("资源文件地址")
.setOnlyWifi(true));
return payload;
}
}