一. 环境
<repositories>
<repository>
<id>getui-nexus</id>
<url>http://mvn.gt.igexin.com/nexus/content/repositories/releases/</url>
</repository>
</repositories>
<dependencies>
<!-- 推送 -->
<dependency>
<groupId>com.gexin.platform</groupId>
<artifactId>gexin-rp-sdk-http</artifactId>
<version>4.1.1.4</version>
</dependency>
<!-- 推送 -->
</dependencies>
二. 个推代码–安卓
import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.base.notify.Notify;
import com.gexin.rp.sdk.exceptions.RequestException;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.TransmissionTemplate;
import java.util.Map;
/**
* @Desc //TODO 安卓推送
* @Date 14:58 2021/6/2
* @Param
* @return
**/
public class AppPush {
//定义常量, appId、appKey、masterSecret 采用本文档 "第二步 获取访问凭证 "中获得的应用配置
private static String url = "http://sdk.open.api.igexin.com/apiex.htm";
private static String appId = "********************";
private static String appKey = "********************";
private static String masterSecret = "********************";
static String CID = "********************";
/**
* 推送消息
*/
public static boolean pushMessage(String cid, String title, String content, String param) {
IGtPush push = new IGtPush(url, appKey, masterSecret);
// 使用透传模板
TransmissionTemplate template = new TransmissionTemplate();
// 1:收到通知直接激活app,2:客服端自行处理
template.setTransmissionType(2);
// 透传内容
template.setTransmissionContent(param);
template.setAppId(appId);
template.setAppkey(appKey);
String intent = "intent:#Intent;action=android.intent.action.oppopush;launchFlags=0x14000000;package=xxx;component=xxx;end";
Notify notify = new Notify();
// 通知栏显示标题
notify.setTitle(title);
// 通知栏内容
notify.setContent(content);
notify.setIntent(intent);
// 设置第三方通知
template.set3rdNotifyInfo(notify);
SingleMessage message = new SingleMessage();
message.setData(template);
// 设置消息离线
message.setOffline(true);
// 离线消息有效时间为7天
message.setOfflineExpireTime(1000 * 3600 * 24 * 7);
//添加要推送的终端
Target target = new Target();
target.setAppId(appId);
target.setClientId(cid);
IPushResult result;
Map<String, Object> response = null;
// 执行推送
try {
result = push.pushMessageToSingle(message, target);
} catch (RequestException e) {
e.printStackTrace();
result = push.pushMessageToSingle(message, target, e.getRequestId());
}
if (result != null) {
response = result.getResponse();
Object o = response.get("result");
if (o.toString().equals("ok")) {
System.out.println("-----------------------------安卓推送CID: "+cid+" >>>> 推送结果: "+ result.toString() +"-----------------------");
return true;
}
}
return false;
}
// public static void main(String[] args) {
// boolean b = pushMessage(CID,"通知栏标题","通知栏内容","3;;自定义标题;;自定义内容;;跳转链接");
// System.out.println(b);
// }
}
3.个推代码–ios
import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.AppMessage;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.base.payload.APNPayload;
import com.gexin.rp.sdk.exceptions.RequestException;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.TransmissionTemplate;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* @Desc //TODO ios推送
* @Date 9:57 2021/6/2
* @Param
* @return
**/
public class PushMessage{
// 1:获取应用基本信息
private static String url = "http://sdk.open.api.igexin.com/apiex.htm";
private static String cid = "*******************";
private static String appId = "*******************";
private static String appKey = "*******************";
private static String masterSecret = "*******************";
/**
*
* @Function: getAppPushAll
* @Description: 单推
* @version: v1.0.0
* @author: qiuyujiao
* @date: 2019年11月7日
*/
public static boolean getPushtoSingle(String cid,String content,String title,String data){
IGtPush push = new IGtPush(url, appKey, masterSecret);
TransmissionTemplate template = getTemplate(content, title,data);
SingleMessage message = new SingleMessage();
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(cid);
IPushResult ret = null;
Map<String, Object> response = null;
try {
ret = push.pushMessageToSingle(message, target);
} catch (RequestException e) {
e.printStackTrace();
ret = push.pushMessageToSingle(message, target, e.getRequestId());
}
if (ret != null) {
response = ret.getResponse();
Object o = response.get("result");
if (o.toString().equals("ok")) {
return true;
}
}
return false;
}
/**
*
* @Function: getTemplate
* @Description: 通知模板
* @version: v1.0.0
* @author: qiuyujiao
* @date: 2019年11月7日
*/
public static TransmissionTemplate getTemplate(String content,String title,String data) {
TransmissionTemplate template = new TransmissionTemplate();
template.setTransmissionContent(data);//透传的内容,开发者自行处理
template.setAppId(appId);
template.setAppkey(appKey);
// 透传消息设置,1为强制启动应用,客户端接收到消息后就会立即启动应用;2为等待应用启动
template.setTransmissionType(2);
APNPayload payload = new APNPayload();
// 字典模式使用APNPayload.DictionaryAlertMsg
payload.setAlertMsg(getDictionaryAlertMsg(content,title));
template.setAPNInfo(payload);
return template;
}
/**
*
* @Function: getDictionaryAlertMsg
* @Description: 【通知样式】iOS通知样式设置
* ios一定要用改方法调试,平板上才会显示推送的消息
* @version: v1.0.0
* @author: qiuyujiao
* @date: 2019年11月7日
*/
private static APNPayload.DictionaryAlertMsg getDictionaryAlertMsg(String content,String title) {
APNPayload.DictionaryAlertMsg alertMsg = new APNPayload.DictionaryAlertMsg();
alertMsg.setBody(content);
alertMsg.setActionLocKey("ActionLockey");
alertMsg.setLocKey("LocKey");
alertMsg.addLocArg("loc-args");
alertMsg.setLaunchImage("launch-image");
// iOS8.2以上版本支持
alertMsg.setTitle(title);
alertMsg.setTitleLocKey("TitleLocKey");
alertMsg.addTitleLocArg("TitleLocArg");
return alertMsg;
}
/**
*
* @Function: getAppPushAll
* @Description: 群推
* @version: v1.0.0
* @author: qiuyujiao
* @date: 2019年11月7日
*/
public static void getAppPushAll(String content,String title,String data){
IGtPush push = new IGtPush(url, appKey, masterSecret);
TransmissionTemplate template = getTemplate(content, title,data);
AppMessage message = new AppMessage();
message.setData(template);
message.setOffline(true);
// 离线有效时间,单位为毫秒
message.setOfflineExpireTime(24 * 1000 * 3600);
List<String> appIdList = new ArrayList<String>();
appIdList.add(appId);
message.setAppIdList(appIdList);
message.setOffline(true);
// 离线有效时间,单位为毫秒
message.setOfflineExpireTime(24 * 1000 * 3600);
IPushResult ret = push.pushMessageToApp(message);
System.out.println(ret.getResponse().toString());
}
// public static void main(String[] args) throws IOException {
// System.out.println(getPushtoSingle("*******************","测试内容","测试标题","3;;自定义标题;;自定义内容;;跳转链接"));
getAppPushAll();
// }
}
注: ios版本: 在app离线状态下,能接到通知,不能接到透传消息!