|
|
|
|
@ -1,592 +1,294 @@
|
|
|
|
|
package com.ruoyi.common.utils;
|
|
|
|
|
|
|
|
|
|
import cn.jiguang.common.resp.APIConnectionException;
|
|
|
|
|
import cn.jiguang.common.resp.APIRequestException;
|
|
|
|
|
import cn.jiguang.common.resp.BooleanResult;
|
|
|
|
|
import cn.jiguang.common.resp.DefaultResult;
|
|
|
|
|
import cn.jpush.api.JPushClient;
|
|
|
|
|
import cn.jpush.api.device.TagAliasResult;
|
|
|
|
|
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.AndroidNotification;
|
|
|
|
|
import cn.jpush.api.push.model.notification.IosNotification;
|
|
|
|
|
import cn.jpush.api.push.model.notification.Notification;
|
|
|
|
|
import cn.jpush.api.schedule.ScheduleResult;
|
|
|
|
|
import cn.hutool.core.bean.BeanUtil;
|
|
|
|
|
import cn.jiguang.sdk.api.DeviceApi;
|
|
|
|
|
import cn.jiguang.sdk.api.PushApi;
|
|
|
|
|
import cn.jiguang.sdk.bean.push.PushSendParam;
|
|
|
|
|
import cn.jiguang.sdk.bean.push.PushSendParam;
|
|
|
|
|
import cn.jiguang.sdk.bean.push.PushSendResult;
|
|
|
|
|
import cn.jiguang.sdk.exception.ApiErrorException;
|
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
|
import okhttp3.*;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.*;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 极光推送工具类
|
|
|
|
|
* 极光推送工具类 (适配 SDK 5.3.0)
|
|
|
|
|
* 用于 IM 系统的离线消息推送
|
|
|
|
|
*/
|
|
|
|
|
@Component
|
|
|
|
|
public class JpushClientUtil {
|
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(JpushClientUtil.class);
|
|
|
|
|
/**
|
|
|
|
|
* 设置好账号的app_key和masterSecret是必须的
|
|
|
|
|
*/
|
|
|
|
|
private final static String APPKER = "c2e673cd0e77c762ffe1a857";
|
|
|
|
|
private final static String MASTERSECRET = "34aace4d60d847468ab96097";
|
|
|
|
|
/**
|
|
|
|
|
* ApnsProduction
|
|
|
|
|
* 此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义
|
|
|
|
|
* 上线之后要改为 true
|
|
|
|
|
*/
|
|
|
|
|
private final static Boolean ApnsProduction = false;
|
|
|
|
|
|
|
|
|
|
private static JPushClient jPushClient = new JPushClient(MASTERSECRET,APPKER);
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(JpushClientUtil.class);
|
|
|
|
|
private static final Gson gson = new Gson();
|
|
|
|
|
private static final String DEVICE_API_URL = "https://device.jpush.cn/v3/devices/";
|
|
|
|
|
private static final MediaType JSON_MEDIA_TYPE = MediaType.parse("application/json; charset=utf-8");
|
|
|
|
|
|
|
|
|
|
// 静态变量,由 Spring 初始化
|
|
|
|
|
private static String appKey;
|
|
|
|
|
private static String masterSecret;
|
|
|
|
|
private static boolean apnsProduction;
|
|
|
|
|
private static PushApi pushApi;
|
|
|
|
|
|
|
|
|
|
private static OkHttpClient httpClient;
|
|
|
|
|
|
|
|
|
|
// 注入的属性
|
|
|
|
|
// @Value("${jpush.appKey}")
|
|
|
|
|
// private String injectedAppKey;
|
|
|
|
|
//
|
|
|
|
|
// @Value("${jpush.masterSecret}")
|
|
|
|
|
// private String injectedMasterSecret;
|
|
|
|
|
// private final static String injectedAppKey = "e75debc0daaed0a2a888d2f3";
|
|
|
|
|
// private final static String injectedMasterSecret = "e8728877c098e641cc6c51e5";
|
|
|
|
|
private final static String injectedAppKey = "d5edb270e4b1cc29616e1dd5";
|
|
|
|
|
private final static String injectedMasterSecret = "a49a9fd3df40747fb6e0d180";
|
|
|
|
|
|
|
|
|
|
// private final static String customAction = "com.test.java.sdk.";
|
|
|
|
|
// private final static String targetPackage = "com.test.java.sdk";
|
|
|
|
|
// private final static String targetActivity = "Activity 组件路径;";
|
|
|
|
|
// private final static String extrasParam = "S.key1=value;i.key2=3;";
|
|
|
|
|
// @Value("${jpush.apnsProduction:true}")
|
|
|
|
|
private final static boolean injectedApnsProduction = true;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@PostConstruct
|
|
|
|
|
public void init() {
|
|
|
|
|
// 将注入的值赋给静态变量
|
|
|
|
|
appKey = injectedAppKey;
|
|
|
|
|
masterSecret = injectedMasterSecret;
|
|
|
|
|
apnsProduction = injectedApnsProduction;
|
|
|
|
|
|
|
|
|
|
// 初始化 PushApi
|
|
|
|
|
pushApi = new PushApi.Builder()
|
|
|
|
|
.setAppKey(appKey)
|
|
|
|
|
.setMasterSecret(masterSecret)
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
private JpushClientUtil() {
|
|
|
|
|
// 初始化 OkHttpClient
|
|
|
|
|
httpClient = new OkHttpClient.Builder()
|
|
|
|
|
.connectTimeout(10, java.util.concurrent.TimeUnit.SECONDS)
|
|
|
|
|
.readTimeout(10, java.util.concurrent.TimeUnit.SECONDS)
|
|
|
|
|
.writeTimeout(10, java.util.concurrent.TimeUnit.SECONDS)
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
throw new AssertionError("不能产生该对象");
|
|
|
|
|
log.info("极光推送工具类初始化完成");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 绑定电话
|
|
|
|
|
*/
|
|
|
|
|
public static void testBindMobile() {
|
|
|
|
|
try {
|
|
|
|
|
DefaultResult result = jPushClient.bindMobile("100d855909a0c6d0655", "15004070936");
|
|
|
|
|
System.out.println("Got result " + result);
|
|
|
|
|
} catch (APIConnectionException e) {
|
|
|
|
|
logger.error("Connection error. Should retry later. ", e);
|
|
|
|
|
} catch (APIRequestException e) {
|
|
|
|
|
logger.error("Error response from JPush server. Should review and fix it. ", e);
|
|
|
|
|
logger.info("HTTP Status: " + e.getStatus());
|
|
|
|
|
logger.info("Error Code: " + e.getErrorCode());
|
|
|
|
|
logger.info("Error Message: " + e.getErrorMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param
|
|
|
|
|
* @return
|
|
|
|
|
*生成别名
|
|
|
|
|
*/
|
|
|
|
|
public static boolean bindAlias(String registerId,String phoneNumber) {
|
|
|
|
|
try {
|
|
|
|
|
DefaultResult defaultResult = jPushClient.updateDeviceTagAlias(registerId, phoneNumber, null,
|
|
|
|
|
null);
|
|
|
|
|
logger.info("Got result"+defaultResult);
|
|
|
|
|
} catch (APIConnectionException e) {
|
|
|
|
|
logger.error("Connection error. Should retry later. ", e);
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
} catch (APIRequestException e) {
|
|
|
|
|
logger.error("Error response from JPush server. Should review and fix it. ", e);
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}finally {
|
|
|
|
|
jPushClient.close();
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static BooleanResult hasMobile() {
|
|
|
|
|
BooleanResult deviceInTag = null;
|
|
|
|
|
try {
|
|
|
|
|
deviceInTag = jPushClient.isDeviceInTag(
|
|
|
|
|
String.valueOf(jPushClient.getDeviceTagAlias("100d855909a0c6d0655").alias), "100d855909a0c6d0655");
|
|
|
|
|
logger.info("Got result" + deviceInTag);
|
|
|
|
|
return deviceInTag ;
|
|
|
|
|
} catch (APIConnectionException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
} catch (APIRequestException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}finally {
|
|
|
|
|
jPushClient.close();
|
|
|
|
|
}
|
|
|
|
|
return deviceInTag ;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void testGetDeviceTagAlias() {
|
|
|
|
|
try {
|
|
|
|
|
TagAliasResult result = jPushClient.getDeviceTagAlias("100d855909a0c6d0655");
|
|
|
|
|
|
|
|
|
|
logger.info(result.alias);
|
|
|
|
|
logger.info(result.tags.toString());
|
|
|
|
|
|
|
|
|
|
} catch (APIConnectionException e) {
|
|
|
|
|
logger.error("Connection error. Should retry later. ", e);
|
|
|
|
|
|
|
|
|
|
} catch (APIRequestException e) {
|
|
|
|
|
logger.error("Error response from JPush server. Should review and fix it. ", e);
|
|
|
|
|
logger.info("HTTP Status: " + e.getStatus());
|
|
|
|
|
logger.info("Error Code: " + e.getErrorCode());
|
|
|
|
|
logger.info("Error Message: " + e.getErrorMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// ==================== 别名绑定方法 ====================
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 推送给设备标识(别名)参数的用户
|
|
|
|
|
* @param alias 设备标识(别名)
|
|
|
|
|
* @param notification_title 通知内容标题
|
|
|
|
|
* @param msg_title 消息内容标题
|
|
|
|
|
* @param msg_content 消息内容
|
|
|
|
|
* @param extrasparam 扩展字段(通常传跳转的链接)
|
|
|
|
|
* @return 0推送失败,1推送成功
|
|
|
|
|
* 服务端直接绑定别名,将 registrationId 与 alias 关联。
|
|
|
|
|
* 调用时机:用户登录成功后,客户端上报 registrationId 即可调用。
|
|
|
|
|
*
|
|
|
|
|
* @param registrationId 客户端上报的设备唯一标识
|
|
|
|
|
* @param alias 别名,如用户ID或手机号
|
|
|
|
|
*/
|
|
|
|
|
public static int sendToAlias( List<String> alias,String notification_title, String msg_title,
|
|
|
|
|
String msg_content, String extrasparam) {
|
|
|
|
|
JPushClient jPushClient = new JPushClient(MASTERSECRET,APPKER);
|
|
|
|
|
int result = 0;
|
|
|
|
|
try {
|
|
|
|
|
PushPayload pushPayload= JpushClientUtil.buildPushObject_all_alias_alertWithTitle(
|
|
|
|
|
alias,notification_title,msg_title,msg_content,extrasparam);
|
|
|
|
|
PushResult pushResult = jPushClient.sendPush(pushPayload);
|
|
|
|
|
if(pushResult.getResponseCode()==200){
|
|
|
|
|
result = 1;
|
|
|
|
|
public static void bindAlias(String registrationId, String alias) {
|
|
|
|
|
if (registrationId == null || registrationId.isEmpty() || alias == null || alias.isEmpty()) {
|
|
|
|
|
log.warn("绑定别名失败: registrationId 或 alias 为空");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 构建请求体
|
|
|
|
|
Map<String, String> bodyMap = new HashMap<>();
|
|
|
|
|
bodyMap.put("alias", alias);
|
|
|
|
|
String jsonBody = gson.toJson(bodyMap);
|
|
|
|
|
|
|
|
|
|
// Basic 认证
|
|
|
|
|
String auth = Base64.getEncoder().encodeToString((appKey + ":" + masterSecret).getBytes());
|
|
|
|
|
|
|
|
|
|
RequestBody requestBody = RequestBody.create(JSON_MEDIA_TYPE,jsonBody);
|
|
|
|
|
Request request = new Request.Builder()
|
|
|
|
|
.url(DEVICE_API_URL + registrationId)
|
|
|
|
|
.addHeader("Authorization", "Basic " + auth)
|
|
|
|
|
.post(requestBody)
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
try (Response response = httpClient.newCall(request).execute()) {
|
|
|
|
|
if (response.isSuccessful()) {
|
|
|
|
|
log.info("别名绑定成功 | registrationId: {}, alias: {}", registrationId, alias);
|
|
|
|
|
} else {
|
|
|
|
|
// 极光 API 有时返回 404 但仍视为成功
|
|
|
|
|
String respBody = response.body() != null ? response.body().string() : "";
|
|
|
|
|
log.warn("别名绑定响应非成功,可能已成功 | registrationId: {}, alias: {}, code: {}, body: {}",
|
|
|
|
|
registrationId, alias, response.code(), respBody);
|
|
|
|
|
}
|
|
|
|
|
logger.info("[极光推送]PushResult result is " + pushResult);
|
|
|
|
|
} catch (APIConnectionException e) {
|
|
|
|
|
logger.error("[极光推送]Connection error. Should retry later. ", e);
|
|
|
|
|
} catch (APIRequestException e) {
|
|
|
|
|
logger.error("[极光推送]Error response from JPush server. Should review and fix it. ", e);
|
|
|
|
|
logger.info("[极光推送]HTTP Status: " + e.getStatus());
|
|
|
|
|
logger.info("[极光推送]Error Code: " + e.getErrorCode());
|
|
|
|
|
logger.info("[极光推送]Error Message: " + e.getErrorMessage());
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
log.error("别名绑定请求异常 | registrationId: {}, alias: {}, error: {}",
|
|
|
|
|
registrationId, alias, e.getMessage(), e);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// ==================== 单聊推送:按别名 ====================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 推送给设备标识(别名)参数的用户(定时)
|
|
|
|
|
* @param alias 设备标识(别名)
|
|
|
|
|
* @param notification_title 通知内容标题
|
|
|
|
|
* @param msg_title 消息内容标题
|
|
|
|
|
* @param msg_content 消息内容
|
|
|
|
|
* @param extrasparam 扩展字段
|
|
|
|
|
* @param time 格式:yyyy-MM-dd HH:mm:ss
|
|
|
|
|
* @return 0推送失败,1推送成功
|
|
|
|
|
* 推送到单个用户 (别名,即 userId)
|
|
|
|
|
* @param alias 用户别名
|
|
|
|
|
* @param title 通知标题
|
|
|
|
|
* @param content 通知内容
|
|
|
|
|
* @param extras 额外参数 (如会话类型、消息类型等)
|
|
|
|
|
*/
|
|
|
|
|
public static int sendToAlias2( List<String> alias,String notification_title, String msg_title, String msg_content, String extrasparam, String time) {
|
|
|
|
|
JPushClient jPushClient = new JPushClient(MASTERSECRET,APPKER);
|
|
|
|
|
int result = 0;
|
|
|
|
|
try {
|
|
|
|
|
PushPayload pushPayload= JpushClientUtil.buildPushObject_all_alias_alertWithTitle(alias,notification_title,msg_title,msg_content,extrasparam);
|
|
|
|
|
ScheduleResult scheduleResult=jPushClient.createSingleSchedule("点呗生活", time, pushPayload);
|
|
|
|
|
if(scheduleResult.getResponseCode()==200){
|
|
|
|
|
result=1;
|
|
|
|
|
}
|
|
|
|
|
logger.info("[极光推送]ScheduleResult result is " + scheduleResult);
|
|
|
|
|
} catch (APIConnectionException e) {
|
|
|
|
|
logger.error("[极光推送]Connection error. Should retry later. ", e);
|
|
|
|
|
} catch (APIRequestException e) {
|
|
|
|
|
logger.error("[极光推送]Error response from JPush server. Should review and fix it. ", e);
|
|
|
|
|
logger.info("[极光推送]HTTP Status: " + e.getStatus());
|
|
|
|
|
logger.info("[极光推送]Error Code: " + e.getErrorCode());
|
|
|
|
|
logger.info("[极光推送]Error Message: " + e.getErrorMessage());
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
public static void pushToUser(String alias, String title, String content, Map<String, String> extras) {
|
|
|
|
|
PushSendParam param = buildPushParamWithAlias(alias, title, content, extras);
|
|
|
|
|
sendPush(param, "user", alias);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ==================== 群聊推送:按别名批量 ====================
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 推送给设备RegistrationIds参数的用户
|
|
|
|
|
* @param registrationIdList 设备注册ID
|
|
|
|
|
* @param notification_title 通知内容标题
|
|
|
|
|
* @param msg_title 消息内容标题
|
|
|
|
|
* @param msg_content 消息内容
|
|
|
|
|
* @param extrasparam 扩展字段
|
|
|
|
|
* @return 0推送失败,1推送成功
|
|
|
|
|
* 推送到多个用户 (别名批量,适合群聊离线推送)
|
|
|
|
|
* @param aliases 用户别名集合
|
|
|
|
|
* @param title 通知标题
|
|
|
|
|
* @param content 通知内容
|
|
|
|
|
* @param extras 额外参数
|
|
|
|
|
*/
|
|
|
|
|
public static int sendToRegistrationIds( List<String> registrationIdList,String notification_title, String msg_title, String msg_content, String extrasparam) {
|
|
|
|
|
JPushClient jPushClient = new JPushClient(MASTERSECRET,APPKER);
|
|
|
|
|
int result = 0;
|
|
|
|
|
try {
|
|
|
|
|
PushPayload pushPayload= JpushClientUtil.buildPushObject_all_registrationIds_alertWithTitle(registrationIdList,notification_title,msg_title,msg_content,extrasparam);
|
|
|
|
|
PushResult pushResult=jPushClient.sendPush(pushPayload);
|
|
|
|
|
if(pushResult.getResponseCode()==200){
|
|
|
|
|
result=1;
|
|
|
|
|
}
|
|
|
|
|
logger.info("[极光推送]PushResult result is " + pushResult);
|
|
|
|
|
} catch (APIConnectionException e) {
|
|
|
|
|
logger.error("[极光推送]Connection error. Should retry later. ", e);
|
|
|
|
|
} catch (APIRequestException e) {
|
|
|
|
|
logger.error("[极光推送]Error response from JPush server. Should review and fix it. ", e);
|
|
|
|
|
logger.info("[极光推送]HTTP Status: " + e.getStatus());
|
|
|
|
|
logger.info("[极光推送]Error Code: " + e.getErrorCode());
|
|
|
|
|
logger.info("[极光推送]Error Message: " + e.getErrorMessage());
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
public static void pushToUsers(List<String> aliases, String title, String content, Map<String, String> extras) {
|
|
|
|
|
if (aliases == null || aliases.isEmpty()) return;
|
|
|
|
|
PushSendParam param = buildPushParamWithAliases(aliases, title, content, extras);
|
|
|
|
|
sendPush(param, "group-users", aliases.size() + " users");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// public static void main(String[] agrs){
|
|
|
|
|
//
|
|
|
|
|
// List<String> strings=new ArrayList<>();
|
|
|
|
|
// strings.add("13065ffa4ee0d41a70c");
|
|
|
|
|
// String notification_title="通知内容标题";//通知内容标题
|
|
|
|
|
// String msg_title="消息内容标题";//消息内容标题
|
|
|
|
|
// String msg_content="消息内容";//消息内容
|
|
|
|
|
// String extrasparam="扩展字段";//扩展字段
|
|
|
|
|
// int i=sendToRegistrationIds(strings,notification_title,msg_title,msg_content,extrasparam);
|
|
|
|
|
// System.out.println(i==0?"0推送失败":"1推送成功");
|
|
|
|
|
// }
|
|
|
|
|
// ==================== 全量推送 (谨慎使用) ====================
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 发送给所有安卓用户
|
|
|
|
|
* @param notification_title 通知内容标题
|
|
|
|
|
* @param msg_title 消息内容标题
|
|
|
|
|
* @param msg_content 消息内容
|
|
|
|
|
* @param extrasparam 扩展字段
|
|
|
|
|
* @return 0推送失败,1推送成功
|
|
|
|
|
* 推送给所有设备
|
|
|
|
|
*/
|
|
|
|
|
public static int sendToAllAndroid( String notification_title, String msg_title, String msg_content, String extrasparam) {
|
|
|
|
|
int result = 0;
|
|
|
|
|
try {
|
|
|
|
|
PushPayload pushPayload= JpushClientUtil.buildPushObject_android_all_alertWithTitle(notification_title,msg_title,msg_content,extrasparam);
|
|
|
|
|
PushResult pushResult=jPushClient.sendPush(pushPayload);
|
|
|
|
|
if(pushResult.getResponseCode()==200){
|
|
|
|
|
result=1;
|
|
|
|
|
}
|
|
|
|
|
logger.info("[极光推送]PushResult result is " + pushResult);
|
|
|
|
|
} catch (APIConnectionException e) {
|
|
|
|
|
logger.error("[极光推送]Connection error. Should retry later. ", e);
|
|
|
|
|
} catch (APIRequestException e) {
|
|
|
|
|
logger.error("[极光推送]Error response from JPush server. Should review and fix it. ", e);
|
|
|
|
|
logger.info("[极光推送]HTTP Status: " + e.getStatus());
|
|
|
|
|
logger.info("[极光推送]Error Code: " + e.getErrorCode());
|
|
|
|
|
logger.info("[极光推送]Error Message: " + e.getErrorMessage());
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
public static void pushToAll(String title, String content, Map<String, String> extras) {
|
|
|
|
|
PushSendParam param = buildPushParamWithAll(title, content, extras);
|
|
|
|
|
sendPush(param, "all", null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ==================== 根据 Registration ID 推送 (备用) ====================
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 发送给所有IOS用户
|
|
|
|
|
* @param notification_title 通知内容标题
|
|
|
|
|
* @param msg_title 消息内容标题
|
|
|
|
|
* @param msg_content 消息内容
|
|
|
|
|
* @param extrasparam 扩展字段
|
|
|
|
|
* @return 0推送失败,1推送成功
|
|
|
|
|
* 按 Registration ID 推送 (适用于未绑定别名的场景)
|
|
|
|
|
*/
|
|
|
|
|
public static int sendToAllIos(String notification_title, String msg_title, String msg_content, String extrasparam) {
|
|
|
|
|
int result = 0;
|
|
|
|
|
try {
|
|
|
|
|
PushPayload pushPayload= JpushClientUtil.buildPushObject_ios_all_alertWithTitle(notification_title,msg_title,msg_content,extrasparam);
|
|
|
|
|
PushResult pushResult=jPushClient.sendPush(pushPayload);
|
|
|
|
|
if(pushResult.getResponseCode()==200){
|
|
|
|
|
result=1;
|
|
|
|
|
}
|
|
|
|
|
logger.info("[极光推送]PushResult result is " + pushResult);
|
|
|
|
|
} catch (APIConnectionException e) {
|
|
|
|
|
logger.error("[极光推送]Connection error. Should retry later. ", e);
|
|
|
|
|
} catch (APIRequestException e) {
|
|
|
|
|
logger.error("[极光推送]Error response from JPush server. Should review and fix it. ", e);
|
|
|
|
|
logger.info("[极光推送]HTTP Status: " + e.getStatus());
|
|
|
|
|
logger.info("[极光推送]Error Code: " + e.getErrorCode());
|
|
|
|
|
logger.info("[极光推送]Error Message: " + e.getErrorMessage());
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
public static void pushByRegistrationId(List<String> regId, String title, String content, Map<String, String> extras) {
|
|
|
|
|
PushSendParam param = buildPushParamWithRegistrationId(regId, title, content, extras);
|
|
|
|
|
sendPush(param, "regId", regId.size()+ " users");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ==================== 内部构建方法 (核心适配) ====================
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 发送给所有用户
|
|
|
|
|
* @param notification_title 通知内容标题
|
|
|
|
|
* @param msg_title 消息内容标题
|
|
|
|
|
* @param msg_content 消息内容
|
|
|
|
|
* @param extrasparam 扩展字段
|
|
|
|
|
* @return 0推送失败,1推送成功
|
|
|
|
|
* 构建单个别名的推送参数
|
|
|
|
|
*/
|
|
|
|
|
public static int sendToAll( String notification_title, String msg_title, String msg_content, String extrasparam) {
|
|
|
|
|
int result = 0;
|
|
|
|
|
try {
|
|
|
|
|
PushPayload pushPayload= JpushClientUtil.buildPushObject_android_and_ios(notification_title,msg_title,msg_content,extrasparam);
|
|
|
|
|
PushResult pushResult=jPushClient.sendPush(pushPayload);
|
|
|
|
|
if(pushResult.getResponseCode()==200){
|
|
|
|
|
result=1;
|
|
|
|
|
}
|
|
|
|
|
logger.info("[极光推送]PushResult result is " + pushResult);
|
|
|
|
|
} catch (APIConnectionException e) {
|
|
|
|
|
logger.error("[极光推送]Connection error. Should retry later. ", e);
|
|
|
|
|
} catch (APIRequestException e) {
|
|
|
|
|
logger.error("[极光推送]Error response from JPush server. Should review and fix it. ", e);
|
|
|
|
|
logger.info("[极光推送]HTTP Status: " + e.getStatus());
|
|
|
|
|
logger.info("[极光推送]Error Code: " + e.getErrorCode());
|
|
|
|
|
logger.info("[极光推送]Error Message: " + e.getErrorMessage());
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
private static PushSendParam buildPushParamWithAlias(String alias, String title, String content, Map<String, String> extras) {
|
|
|
|
|
Map<String, Object> pushBody = buildCommonPushBody(title, content, extras);
|
|
|
|
|
Map map= new HashMap<>();
|
|
|
|
|
map.put("alias", Arrays.asList(alias));
|
|
|
|
|
pushBody.put("audience", map);
|
|
|
|
|
PushSendParam param = BeanUtil.toBean(pushBody, PushSendParam.class);
|
|
|
|
|
return param;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 发送给所有用户(定时推送)
|
|
|
|
|
* @param notification_title 通知内容标题
|
|
|
|
|
* @param msg_title 消息内容标题
|
|
|
|
|
* @param msg_content 消息内容
|
|
|
|
|
* @param extrasparam 扩展字段
|
|
|
|
|
* @param time 格式:yyyy-MM-dd HH:mm:ss
|
|
|
|
|
* @return 0推送失败,1推送成功
|
|
|
|
|
* 构建多个别名的推送参数
|
|
|
|
|
*/
|
|
|
|
|
public static int sendToAll2( String notification_title, String msg_title, String msg_content, String extrasparam, String time) {
|
|
|
|
|
int result = 0;
|
|
|
|
|
try {
|
|
|
|
|
PushPayload pushPayload= JpushClientUtil.buildPushObject_android_and_ios(notification_title,msg_title,msg_content,extrasparam);
|
|
|
|
|
ScheduleResult scheduleResult=jPushClient.createSingleSchedule("测试", time, pushPayload);
|
|
|
|
|
if(scheduleResult.getResponseCode()==200){
|
|
|
|
|
result=1;
|
|
|
|
|
}
|
|
|
|
|
logger.info("[极光推送]scheduleResult result is " + scheduleResult);
|
|
|
|
|
} catch (APIConnectionException e) {
|
|
|
|
|
logger.error("[极光推送]Connection error. Should retry later. ", e);
|
|
|
|
|
} catch (APIRequestException e) {
|
|
|
|
|
logger.error("[极光推送]Error response from JPush server. Should review and fix it. ", e);
|
|
|
|
|
logger.info("[极光推送]HTTP Status: " + e.getStatus());
|
|
|
|
|
logger.info("[极光推送]Error Code: " + e.getErrorCode());
|
|
|
|
|
logger.info("[极光推送]Error Message: " + e.getErrorMessage());
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
private static PushSendParam buildPushParamWithAliases(List<String> aliases, String title, String content, Map<String, String> extras) {
|
|
|
|
|
Map<String, Object> pushBody = buildCommonPushBody(title, content, extras);
|
|
|
|
|
Map map= new HashMap<>();
|
|
|
|
|
map.put("alias", aliases);
|
|
|
|
|
pushBody.put("audience", map);
|
|
|
|
|
return BeanUtil.toBean(pushBody, PushSendParam.class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static PushPayload buildPushObject_android_and_ios(String notification_title, String msg_title, String msg_content, String extrasparam) {
|
|
|
|
|
return PushPayload.newBuilder()
|
|
|
|
|
.setPlatform(Platform.android_ios())
|
|
|
|
|
.setAudience(Audience.all())
|
|
|
|
|
.setNotification(Notification.newBuilder()
|
|
|
|
|
.setAlert(msg_content)
|
|
|
|
|
.addPlatformNotification(AndroidNotification.newBuilder()
|
|
|
|
|
.setAlert(msg_content)
|
|
|
|
|
.setTitle(notification_title)
|
|
|
|
|
//此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
|
|
|
|
|
.addExtra("url",extrasparam)
|
|
|
|
|
.build()
|
|
|
|
|
)
|
|
|
|
|
.addPlatformNotification(IosNotification.newBuilder()
|
|
|
|
|
//传一个IosAlert对象,指定apns title、title、subtitle等
|
|
|
|
|
.setAlert(msg_content)
|
|
|
|
|
//直接传alert
|
|
|
|
|
//此项是指定此推送的badge自动加1
|
|
|
|
|
.incrBadge(1)
|
|
|
|
|
//此字段的值default表示系统默认声音;传sound.caf表示此推送以项目里面打包的sound.caf声音来提醒,
|
|
|
|
|
// 如果系统没有此音频则以系统默认声音提醒;此字段如果传空字符串,iOS9及以上的系统是无声音提醒,以下的系统是默认声音
|
|
|
|
|
.setSound("sound.caf")
|
|
|
|
|
//此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
|
|
|
|
|
.addExtra("url",extrasparam)
|
|
|
|
|
//此项说明此推送是一个background推送,想了解background看:http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification
|
|
|
|
|
// .setContentAvailable(true)
|
|
|
|
|
.build()
|
|
|
|
|
)
|
|
|
|
|
.build()
|
|
|
|
|
)
|
|
|
|
|
//Platform指定了哪些平台就会像指定平台中符合推送条件的设备进行推送。 jpush的自定义消息,
|
|
|
|
|
// sdk默认不做任何处理,不会有通知提示。建议看文档http://docs.jpush.io/guideline/faq/的
|
|
|
|
|
// [通知与自定义消息有什么区别?]了解通知和自定义消息的区别
|
|
|
|
|
.setMessage(Message.newBuilder()
|
|
|
|
|
.setMsgContent(msg_content)
|
|
|
|
|
.setTitle(msg_title)
|
|
|
|
|
.addExtra("url",extrasparam)
|
|
|
|
|
.build())
|
|
|
|
|
.setOptions(Options.newBuilder()
|
|
|
|
|
//此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义
|
|
|
|
|
.setApnsProduction(ApnsProduction)
|
|
|
|
|
//此字段是给开发者自己给推送编号,方便推送者分辨推送记录
|
|
|
|
|
.setSendno(1)
|
|
|
|
|
//此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天,单位为秒
|
|
|
|
|
.setTimeToLive(86400)
|
|
|
|
|
.build()
|
|
|
|
|
)
|
|
|
|
|
.build();
|
|
|
|
|
/**
|
|
|
|
|
* 构建全量推送的参数
|
|
|
|
|
*/
|
|
|
|
|
private static PushSendParam buildPushParamWithAll(String title, String content, Map<String, String> extras) {
|
|
|
|
|
Map<String, Object> pushBody = buildCommonPushBody(title, content, extras);
|
|
|
|
|
pushBody.put("audience", "all");
|
|
|
|
|
return BeanUtil.toBean(pushBody, PushSendParam.class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static PushPayload buildPushObject_all_alias_alertWithTitle(List<String> alias,String notification_title, String msg_title, String msg_content, String extrasparam) {
|
|
|
|
|
//创建一个IosAlert对象,可指定APNs的alert、title等字段
|
|
|
|
|
//IosAlert iosAlert = IosAlert.newBuilder().setTitleAndBody("title", "alert body").build();
|
|
|
|
|
return PushPayload.newBuilder()
|
|
|
|
|
//指定要推送的平台,all代表当前应用配置了的所有平台,也可以传android等具体平台
|
|
|
|
|
.setPlatform(Platform.all())
|
|
|
|
|
//指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration id
|
|
|
|
|
.setAudience(Audience.alias(alias))
|
|
|
|
|
//.setAudience(Audience.registrationId(registrationId))
|
|
|
|
|
//jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发
|
|
|
|
|
.setNotification(Notification.newBuilder()
|
|
|
|
|
//指定当前推送的android通知
|
|
|
|
|
.addPlatformNotification(AndroidNotification.newBuilder()
|
|
|
|
|
.setAlert(msg_content)
|
|
|
|
|
.setTitle(notification_title)
|
|
|
|
|
//此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
|
|
|
|
|
.addExtra("url",extrasparam)
|
|
|
|
|
.build())
|
|
|
|
|
//指定当前推送的iOS通知
|
|
|
|
|
.addPlatformNotification(IosNotification.newBuilder()
|
|
|
|
|
//传一个IosAlert对象,指定apns title、title、subtitle等
|
|
|
|
|
.setAlert(msg_content)
|
|
|
|
|
//直接传alert
|
|
|
|
|
//此项是指定此推送的badge自动加1
|
|
|
|
|
.incrBadge(1)
|
|
|
|
|
//此字段的值default表示系统默认声音;传sound.caf表示此推送以项目里面打包的sound.caf声音来提醒,
|
|
|
|
|
// 如果系统没有此音频则以系统默认声音提醒;此字段如果传空字符串,iOS9及以上的系统是无声音提醒,以下的系统是默认声音
|
|
|
|
|
.setSound("sound.caf")
|
|
|
|
|
//此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
|
|
|
|
|
.addExtra("url",extrasparam)
|
|
|
|
|
//此项说明此推送是一个background推送,想了解background看:http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification
|
|
|
|
|
//取消此注释,消息推送时ios将无法在锁屏情况接收
|
|
|
|
|
// .setContentAvailable(true)
|
|
|
|
|
.build())
|
|
|
|
|
.build())
|
|
|
|
|
//Platform指定了哪些平台就会像指定平台中符合推送条件的设备进行推送。 jpush的自定义消息,
|
|
|
|
|
// sdk默认不做任何处理,不会有通知提示。建议看文档http://docs.jpush.io/guideline/faq/的
|
|
|
|
|
// [通知与自定义消息有什么区别?]了解通知和自定义消息的区别
|
|
|
|
|
.setMessage(Message.newBuilder()
|
|
|
|
|
.setMsgContent(msg_content)
|
|
|
|
|
.setTitle(msg_title)
|
|
|
|
|
.addExtra("url",extrasparam)
|
|
|
|
|
.build())
|
|
|
|
|
.setOptions(Options.newBuilder()
|
|
|
|
|
//此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义
|
|
|
|
|
.setApnsProduction(ApnsProduction)
|
|
|
|
|
//此字段是给开发者自己给推送编号,方便推送者分辨推送记录
|
|
|
|
|
.setSendno(1)
|
|
|
|
|
//此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天;
|
|
|
|
|
.setTimeToLive(86400)
|
|
|
|
|
.build())
|
|
|
|
|
.build();
|
|
|
|
|
/**
|
|
|
|
|
* 构建 Registration ID 推送的参数
|
|
|
|
|
*/
|
|
|
|
|
private static PushSendParam buildPushParamWithRegistrationId(List<String> regId, String title, String content, Map<String, String> extras) {
|
|
|
|
|
Map<String, Object> pushBody = buildCommonPushBody(title, content, extras);
|
|
|
|
|
Map map= new HashMap<>();
|
|
|
|
|
map.put("registration_id", regId);
|
|
|
|
|
pushBody.put("audience", map);
|
|
|
|
|
return BeanUtil.toBean(pushBody, PushSendParam.class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static PushPayload buildPushObject_all_registrationIds_alertWithTitle(List<String> registrationIds,String notification_title, String msg_title, String msg_content, String extrasparam) {
|
|
|
|
|
//创建一个IosAlert对象,可指定APNs的alert、title等字段
|
|
|
|
|
//IosAlert iosAlert = IosAlert.newBuilder().setTitleAndBody("title", "alert body").build();
|
|
|
|
|
return PushPayload.newBuilder()
|
|
|
|
|
//指定要推送的平台,all代表当前应用配置了的所有平台,也可以传android等具体平台
|
|
|
|
|
.setPlatform(Platform.all())
|
|
|
|
|
//指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration id
|
|
|
|
|
.setAudience(Audience.registrationId(registrationIds))
|
|
|
|
|
//jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发
|
|
|
|
|
.setNotification(Notification.newBuilder()
|
|
|
|
|
//指定当前推送的android通知
|
|
|
|
|
.addPlatformNotification(AndroidNotification.newBuilder()
|
|
|
|
|
.setAlert(msg_content)
|
|
|
|
|
.setTitle(notification_title)
|
|
|
|
|
//此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
|
|
|
|
|
.addExtra("url",extrasparam)
|
|
|
|
|
.build())
|
|
|
|
|
//指定当前推送的iOS通知
|
|
|
|
|
.addPlatformNotification(IosNotification.newBuilder()
|
|
|
|
|
//传一个IosAlert对象,指定apns title、title、subtitle等
|
|
|
|
|
.setAlert(msg_content)
|
|
|
|
|
//直接传alert
|
|
|
|
|
//此项是指定此推送的badge自动加1
|
|
|
|
|
.incrBadge(1)
|
|
|
|
|
//此字段的值default表示系统默认声音;传sound.caf表示此推送以项目里面打包的sound.caf声音来提醒,
|
|
|
|
|
// 如果系统没有此音频则以系统默认声音提醒;此字段如果传空字符串,iOS9及以上的系统是无声音提醒,以下的系统是默认声音
|
|
|
|
|
.setSound("sound.caf")
|
|
|
|
|
//此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
|
|
|
|
|
.addExtra("url",extrasparam)
|
|
|
|
|
//此项说明此推送是一个background推送,想了解background看:http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification
|
|
|
|
|
//取消此注释,消息推送时ios将无法在锁屏情况接收
|
|
|
|
|
// .setContentAvailable(true)
|
|
|
|
|
.build())
|
|
|
|
|
.build())
|
|
|
|
|
//Platform指定了哪些平台就会像指定平台中符合推送条件的设备进行推送。 jpush的自定义消息,
|
|
|
|
|
// sdk默认不做任何处理,不会有通知提示。建议看文档http://docs.jpush.io/guideline/faq/的
|
|
|
|
|
// [通知与自定义消息有什么区别?]了解通知和自定义消息的区别
|
|
|
|
|
.setMessage(Message.newBuilder()
|
|
|
|
|
.setMsgContent(msg_content)
|
|
|
|
|
.setTitle(msg_title)
|
|
|
|
|
.addExtra("url",extrasparam)
|
|
|
|
|
.build())
|
|
|
|
|
.setOptions(Options.newBuilder()
|
|
|
|
|
//此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义
|
|
|
|
|
.setApnsProduction(ApnsProduction)
|
|
|
|
|
//此字段是给开发者自己给推送编号,方便推送者分辨推送记录
|
|
|
|
|
.setSendno(1)
|
|
|
|
|
//此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天;
|
|
|
|
|
.setTimeToLive(86400)
|
|
|
|
|
.build())
|
|
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static PushPayload buildPushObject_android_all_alertWithTitle(String notification_title, String msg_title, String msg_content, String extrasparam) {
|
|
|
|
|
List<String> rids=new ArrayList<>();
|
|
|
|
|
return PushPayload.newBuilder()
|
|
|
|
|
//指定要推送的平台,all代表当前应用配置了的所有平台,也可以传android等具体平台
|
|
|
|
|
.setPlatform(Platform.android())
|
|
|
|
|
//指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration id
|
|
|
|
|
.setAudience(Audience.all())
|
|
|
|
|
//jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发
|
|
|
|
|
.setNotification(Notification.newBuilder()
|
|
|
|
|
//指定当前推送的android通知
|
|
|
|
|
.addPlatformNotification(AndroidNotification.newBuilder()
|
|
|
|
|
.setAlert(msg_content)
|
|
|
|
|
.setTitle(notification_title)
|
|
|
|
|
//此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
|
|
|
|
|
.addExtra("url",extrasparam)
|
|
|
|
|
.build())
|
|
|
|
|
.build()
|
|
|
|
|
)
|
|
|
|
|
//Platform指定了哪些平台就会像指定平台中符合推送条件的设备进行推送。 jpush的自定义消息,
|
|
|
|
|
// sdk默认不做任何处理,不会有通知提示。建议看文档http://docs.jpush.io/guideline/faq/的
|
|
|
|
|
// [通知与自定义消息有什么区别?]了解通知和自定义消息的区别
|
|
|
|
|
.setMessage(Message.newBuilder()
|
|
|
|
|
.setMsgContent(msg_content)
|
|
|
|
|
.setTitle(msg_title)
|
|
|
|
|
.addExtra("url",extrasparam)
|
|
|
|
|
.build())
|
|
|
|
|
.setOptions(Options.newBuilder()
|
|
|
|
|
//此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义
|
|
|
|
|
.setApnsProduction(ApnsProduction)
|
|
|
|
|
//此字段是给开发者自己给推送编号,方便推送者分辨推送记录
|
|
|
|
|
.setSendno(1)
|
|
|
|
|
//此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天,单位为秒
|
|
|
|
|
.setTimeToLive(86400)
|
|
|
|
|
.build())
|
|
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* 构建通用的推送 body (包含通知和公共参数)
|
|
|
|
|
*/
|
|
|
|
|
private static Map<String, Object> buildCommonPushBody(String title, String content, Map<String, String> extras) {
|
|
|
|
|
Map<String, Object> pushBody = new HashMap<>();
|
|
|
|
|
pushBody.put("platform", "all"); // 全平台
|
|
|
|
|
|
|
|
|
|
// 构建通知体
|
|
|
|
|
Map<String, Object> notification = new HashMap<>();
|
|
|
|
|
notification.put("alert", content);
|
|
|
|
|
|
|
|
|
|
// Android 通知
|
|
|
|
|
Map<String, Object> android = new HashMap<>();
|
|
|
|
|
android.put("alert", content);
|
|
|
|
|
android.put("title", title);
|
|
|
|
|
// //点击消息跳转至
|
|
|
|
|
// Map<String, String> intent= new HashMap<>();
|
|
|
|
|
// String intentUrl = String.format(
|
|
|
|
|
// "intent:#Intent;action=%s;component=%s/%s;S.userId=%s;end",
|
|
|
|
|
// customAction, targetPackage, targetActivity, extrasParam
|
|
|
|
|
// );
|
|
|
|
|
// intent.put("url",intentUrl);
|
|
|
|
|
// android.put("intent",intent);
|
|
|
|
|
if (extras != null) {
|
|
|
|
|
android.put("extras", extras);
|
|
|
|
|
}
|
|
|
|
|
notification.put("android", android);
|
|
|
|
|
|
|
|
|
|
// iOS 通知
|
|
|
|
|
Map<String, Object> ios = new HashMap<>();
|
|
|
|
|
ios.put("alert", title);
|
|
|
|
|
ios.put("sound", "default");
|
|
|
|
|
ios.put("badge", "+1");
|
|
|
|
|
if (extras != null) {
|
|
|
|
|
ios.put("extras", extras);
|
|
|
|
|
}
|
|
|
|
|
notification.put("ios", ios);
|
|
|
|
|
|
|
|
|
|
private static PushPayload buildPushObject_ios_all_alertWithTitle( String notification_title, String msg_title, String msg_content, String extrasparam) {
|
|
|
|
|
return PushPayload.newBuilder()
|
|
|
|
|
//指定要推送的平台,all代表当前应用配置了的所有平台,也可以传android等具体平台
|
|
|
|
|
.setPlatform(Platform.ios())
|
|
|
|
|
//指定推送的接收对象,all代表所有人,也可以指定已经设置成功的tag或alias或该应应用客户端调用接口获取到的registration id
|
|
|
|
|
.setAudience(Audience.all())
|
|
|
|
|
//jpush的通知,android的由jpush直接下发,iOS的由apns服务器下发,Winphone的由mpns下发
|
|
|
|
|
.setNotification(Notification.newBuilder()
|
|
|
|
|
//指定当前推送的android通知
|
|
|
|
|
.addPlatformNotification(IosNotification.newBuilder()
|
|
|
|
|
//传一个IosAlert对象,指定apns title、title、subtitle等
|
|
|
|
|
.setAlert(msg_content)
|
|
|
|
|
//直接传alert
|
|
|
|
|
//此项是指定此推送的badge自动加1
|
|
|
|
|
.incrBadge(1)
|
|
|
|
|
//此字段的值default表示系统默认声音;传sound.caf表示此推送以项目里面打包的sound.caf声音来提醒,
|
|
|
|
|
// 如果系统没有此音频则以系统默认声音提醒;此字段如果传空字符串,iOS9及以上的系统是无声音提醒,以下的系统是默认声音
|
|
|
|
|
.setSound("sound.caf")
|
|
|
|
|
//此字段为透传字段,不会显示在通知栏。用户可以通过此字段来做一些定制需求,如特定的key传要指定跳转的页面(value)
|
|
|
|
|
.addExtra("url",extrasparam)
|
|
|
|
|
//此项说明此推送是一个background推送,想了解background看:http://docs.jpush.io/client/ios_tutorials/#ios-7-background-remote-notification
|
|
|
|
|
// .setContentAvailable(true)
|
|
|
|
|
.build())
|
|
|
|
|
.build()
|
|
|
|
|
)
|
|
|
|
|
//Platform指定了哪些平台就会像指定平台中符合推送条件的设备进行推送。 jpush的自定义消息,
|
|
|
|
|
// sdk默认不做任何处理,不会有通知提示。建议看文档http://docs.jpush.io/guideline/faq/的
|
|
|
|
|
// [通知与自定义消息有什么区别?]了解通知和自定义消息的区别
|
|
|
|
|
.setMessage(Message.newBuilder()
|
|
|
|
|
.setMsgContent(msg_content)
|
|
|
|
|
.setTitle(msg_title)
|
|
|
|
|
.addExtra("url",extrasparam)
|
|
|
|
|
.build())
|
|
|
|
|
.setOptions(Options.newBuilder()
|
|
|
|
|
//此字段的值是用来指定本推送要推送的apns环境,false表示开发,true表示生产;对android和自定义消息无意义
|
|
|
|
|
.setApnsProduction(ApnsProduction)
|
|
|
|
|
//此字段是给开发者自己给推送编号,方便推送者分辨推送记录
|
|
|
|
|
.setSendno(1)
|
|
|
|
|
//此字段的值是用来指定本推送的离线保存时长,如果不传此字段则默认保存一天,最多指定保留十天,单位为秒
|
|
|
|
|
.setTimeToLive(86400)
|
|
|
|
|
.build())
|
|
|
|
|
.build();
|
|
|
|
|
pushBody.put("notification", notification);
|
|
|
|
|
|
|
|
|
|
// 选项
|
|
|
|
|
Map<String, Object> options = new HashMap<>();
|
|
|
|
|
options.put("time_to_live", 86400);
|
|
|
|
|
options.put("apns_production", apnsProduction);
|
|
|
|
|
pushBody.put("options", options);
|
|
|
|
|
|
|
|
|
|
return pushBody;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Boolean sendMessage(String phone,String notification_title,String msg_content){
|
|
|
|
|
List<String> strings=new ArrayList<>();
|
|
|
|
|
strings.add(phone);
|
|
|
|
|
String msg_title="";//消息内容标题
|
|
|
|
|
String extrasparam="";//扩展字段
|
|
|
|
|
int i= sendToAlias(strings,notification_title,msg_title,msg_content,extrasparam);
|
|
|
|
|
System.out.println(i==0?"0推送失败":"1推送成功");
|
|
|
|
|
return i==0?false:true;
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* 统一发送和异常处理
|
|
|
|
|
*/
|
|
|
|
|
private static void sendPush(PushSendParam param, String type, String target) {
|
|
|
|
|
try {
|
|
|
|
|
// 调用 PushApi 的 send 方法进行推送
|
|
|
|
|
PushSendResult result = pushApi.send(param);
|
|
|
|
|
log.info("极光推送成功 | type: {} | target: {} | msgId: {}", type, target, result.getMessageId());
|
|
|
|
|
} catch (ApiErrorException e) {
|
|
|
|
|
// 处理 API 错误,可获取错误码、HTTP状态码和限流信息
|
|
|
|
|
int httpStatus = e.getStats();
|
|
|
|
|
int errorCode = e.getApiError().getError().getCode();
|
|
|
|
|
String errorMessage = e.getApiError().getError().getMessage();
|
|
|
|
|
log.error("极光推送失败 | type: {} | target: {} | httpStatus: {} | errorCode: {} | error: {}",
|
|
|
|
|
type, target, httpStatus, errorCode, errorMessage, e);
|
|
|
|
|
// 此处可接入重试机制或报警
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|