极光推送后台开发文档:https://docs.jiguang.cn//jpush/server/push/rest_api_v3_push/
跟安卓商量好,只能启用安卓端已经集成的厂商通道:
以下封装的的推送工具类(工具类中也只能集成,安卓端已经集成的厂商通道——maps里面的厂商):
package com.hpm.blog.jdpush;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import cn.jiguang.common.resp.APIConnectionException;
import cn.jiguang.common.resp.APIRequestException;
import cn.jiguang.common.resp.DefaultResult;
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.AndroidNotification;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import com.google.gson.JsonObject;
/**
* 极光推送消息
*
* */
public class JPushUtil {
private static String masterSecret = "";
private static String appKey = "";
private static JPushClient jPushClient = new JPushClient(masterSecret,appKey);
/**
* 推送给设备标识参数的用户
* @param registrationId 设备标识
* @param notification_title 通知内容标题
* @param msg_title 消息内容标题
* @param msg_content 消息内容
* @param extrasparam 扩展字段
* @return 0推送失败,1推送成功
*/
public static int sendToRegistrationId( String registrationId,String notification_title, String msg_title, String msg_content, String extrasparam) {
int result = 0;
try {
//ospush 表示强制厂商通道, jpush 表示强制极光通道,secondary_push 表示优先走极光,极光不在线再走厂商
Map<String, String> map = new HashMap<String, String>();
map.put("distribution","secondary_push");
Map<String,Map<String, String>> maps = new HashMap<String,Map<String, String>>();
maps.put("xiaomi",map);
maps.put("huawei",map);
maps.put("meizu",map);
maps.put("oppo",map);
maps.put("vivo",map);
// maps.put("fcm",map);
PushPayload pushPayload= JPushUtil.buildPushObject_all_registrationId_alertWithTitle(registrationId,notification_title,msg_title,msg_content,extrasparam,maps);
System.out.println(pushPayload);
PushResult pushResult=jPushClient.sendPush(pushPayload);
/* //ios推送vnps通知
Map map = new HashMap();
map.put(msg_content,notification_title);
map.put("sound",msg_title);
jPushClient.sendIosNotificationWithAlias(msg_title,map,registrationId);*/
System.out.println(pushResult);
if(pushResult.getResponseCode()==200){
result=1;
}
} catch (APIConnectionException e) {
e.printStackTrace();
} catch (APIRequestException e) {
e.printStackTrace();
}
return result;
}
/**
* 发送给所有安卓用户
* @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= JPushUtil.buildPushObject_android_all_alertWithTitle(notification_title,msg_title,msg_content,extrasparam);
System.out.println(pushPayload);
PushResult pushResult=jPushClient.sendPush(pushPayload);
System.out.println(pushResult);
if(pushResult.getResponseCode()==200){
result=1;
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 发送给所有IOS用户
* @param notification_title 通知内容标题
* @param msg_title 消息内容标题
* @param msg_content 消息内容
* @param extrasparam 扩展字段
* @return 0推送失败,1推送成功
*/
public static int sendToAllIos(String notification_title, String msg_title, String msg_content, String extrasparam) {
int result = 0;
try {
PushPayload pushPayload= JPushUtil.buildPushObject_ios_all_alertWithTitle(notification_title,msg_title,msg_content,extrasparam);
System.out.println(pushPayload);
PushResult pushResult=jPushClient.sendPush(pushPayload);
System.out.println(pushResult);
if(pushResult.getResponseCode()==200){
result=1;
}
}