直接上代码:
service类:
package com.admin.service; import java.util.List; import com.admin.model.JPushChatModel; public interface JPushApiClient { // 极光别名推送通知 public void jPush(List<String> alias, JPushChatModel jpModel); //极光推送设备ID public void jPush_rigstId(List<String> rigstId, JPushChatModel jpModel); }
impl类:
package com.admin.service.impl; import java.util.ArrayList; import java.util.List; import java.util.UUID; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.admin.model.JPushChatModel; import com.admin.model.Message; import com.admin.service.JPushApiClient; import com.admin.service.MessageServiceI; import com.web.util.TimeUtils; import cn.jpush.api.JPushClient; import cn.jpush.api.push.PushResult; 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; @Service public class JPushApiClientImpl implements JPushApiClient { // private static Logger logger=Logger.getLogger(JPushApiClientImpl.class); //String jPushAppKey = "76d4f4592bd5909e666ea527"; //String jPushMasterSecret = "c9acc22b69d867b5913064c4"; //2018-3-14 极光的Key String jPushAppKey = "3e4d3836be******892b21e"; String jPushMasterSecret = "f48ae182a*****2050791e7"; @Autowired private MessageServiceI messageServiceI; @Override public void jPush(List<String> alias, JPushChatModel jpModel) { Message message=new Message(); StringBuffer buffer=new StringBuffer(); try { if(jpModel!=null){ if(jpModel.getTitle()!=null){ buffer.append("|title:"+jpModel.getTitle()+"|,"); } if(jpModel.getContent()!=null){ buffer.append(",|content:"+jpModel.getContent()+"|"); } if(jpModel.getType()!=null){ buffer.append(",|type:"+jpModel.getType()+"|"); } if(jpModel.getDataId()!=null){ buffer.append(",|dataId:"+jpModel.getDataId()+"|"); } buffer.append(",|alias{"); for(int i=0;i<alias.size();i++){ if(i==alias.size()-1){ buffer.append(alias.get(i)); }else{ buffer.append(alias.get(i)+","); } } buffer.append("}|"); message.setId(UUID.randomUUID().toString()); message.setTime(TimeUtils.getTime("yyyy-MM-dd HH:mm:ss")); } JPushClient jpushClient = new JPushClient(jPushMasterSecret, jPushAppKey, 3); PushPayload payload = alias_buildPushObject_ios_audienceMore_messageWithExtras( alias, jpModel); PushResult result = jpushClient.sendPush(payload); buffer.append(",|推送成功:"+result+"|"); message.setMessage(buffer.toString()); messageServiceI.save(message); // logger.debug("推送日志:" + result); } catch (Exception e) { // if(jpModel!=null){ // logger.debug("------推送异常:type:"+jpModel.getType()+"|title:"+jpModel.getTitle()+"|dataId:"+jpModel.getDataId()+"|Error:" +e.getMessage()+"------"); // } buffer.append(",|推送异常:"+e.getMessage()+"|"); message.setMessage(buffer.toString()); messageServiceI.save(message); e.printStackTrace(); } } public void jPush_rigstId(List<String> rigstId, JPushChatModel jpModel) { Message message=new Message(); StringBuffer buffer=new StringBuffer(); try { if(jpModel!=null){ if(jpModel.getTitle()!=null){ buffer.append("|title:"+jpModel.getTitle()+"|"); } if(jpModel.getContent()!=null){ buffer.append(",|content:"+jpModel.getContent()+"|"); } if(jpModel.getType()!=null){ buffer.append(",|type:"+jpModel.getType()+"|"); } if(jpModel.getDataId()!=null){ buffer.append(",|dataId:"+jpModel.getDataId()+"|"); } buffer.append(",|alias{"); for(int i=0;i<rigstId.size();i++){ if(i==rigstId.size()-1){ buffer.append(rigstId.get(i)); }else{ buffer.append(rigstId.get(i)+","); } } buffer.append("}|"); message.setId(UUID.randomUUID().toString()); message.setTime(TimeUtils.getTime("yyyy-MM-dd HH:mm:ss")); } JPushClient jpushClient = new JPushClient(jPushMasterSecret, jPushAppKey, 3); PushPayload payload =rigstId_buildPushObject_ios_audienceMore_messageWithExtras( rigstId, jpModel); PushResult result = jpushClient.sendPush(payload); buffer.append(",|推送成功:"+result+"|"); message.setMessage(buffer.toString()); messageServiceI.save(message); // logger.info("推送日志:" + result); } catch (Exception e) { // logger.error("------推送异常:type:"+jpModel.getType()+"|title:"+jpModel.getTitle()+"|content"+jpModel.getContent()+"|dataId:"+jpModel.getDataId()+"|Error:" +e.getMessage()+"------"); buffer.append(",|推送异常:"+e.getMessage()+"|"); message.setMessage(buffer.toString()); messageServiceI.save(message); e.printStackTrace(); } } public static PushPayload alias_buildPushObject_ios_audienceMore_messageWithExtras( List<String> alias, JPushChatModel model) { return PushPayload .newBuilder() .setPlatform(Platform.all()) .setAudience(Audience.alias(alias)) .setNotification( Notification .newBuilder() .addPlatformNotification( IosNotification .newBuilder() .setAlert(model.getContent()) .setBadge(5) .addExtra("type", model.getType()) // 跳转类型type .addExtra("content", model.getContent()) .addExtra("title", model.getTitle()) .addExtra("sendUserId", model.getSendUserId()) .addExtra("url", model.getUrl()) .addExtra("dataId", model.getDataId()) // 文章ID .build()) .addPlatformNotification( AndroidNotification .newBuilder() .setAlert(model.getContent()) .setTitle(model.getTitle()) .addExtra("type", model.getType()) .addExtra("content", model.getContent()) .addExtra("title", model.getTitle()) .addExtra("sendUserId", model.getSendUserId()) .addExtra("url", model.getUrl()) .addExtra("dataId", model.getDataId()) .build()).build()) .setOptions( Options.newBuilder().setApnsProduction(true).build()) .build(); } public static PushPayload rigstId_buildPushObject_ios_audienceMore_messageWithExtras( List<String> rigstId, JPushChatModel model) { return PushPayload .newBuilder() .setPlatform(Platform.all()) .setAudience(Audience.registrationId(rigstId)) .setNotification( Notification .newBuilder() .addPlatformNotification( IosNotification .newBuilder() .setAlert(model.getContent()) .setBadge(5) .addExtra("type", model.getType()) // 跳转类型type .addExtra("content", model.getContent()) .addExtra("title", model.getTitle()) .addExtra("sendUserId", model.getSendUserId()) .addExtra("url", model.getUrl()) .addExtra("dataId", model.getDataId()) // 文章ID .build()) .addPlatformNotification( AndroidNotification .newBuilder() .setAlert(model.getContent()) .setTitle(model.getTitle()) .addExtra("type", model.getType()) .addExtra("content", model.getContent()) .addExtra("title", model.getTitle()) .addExtra("sendUserId", model.getSendUserId()) .addExtra("url", model.getUrl()) .addExtra("dataId", model.getDataId()) .build()).build()) .setOptions( Options.newBuilder().setApnsProduction(false).build())//这里设置的Options.newBuilder().setApnsProduction(false)是false适应iOS的测试环境,Android随意.设置的为true是iOS的生产环境. .build(); } }
这里设置的.setOptions(Options.newBuilder().setApnsProduction(false).build())设置false是适应iOS的测试环境,是true是iOS的生产环境,Android随意.
测试main方法:
public static void main(String[] args) { List<String> alias = new ArrayList<>(); alias.add("141*************9");//直接可以写 JPushChatModel jpModel = new JPushChatModel(); jpModel.setTitle("title"); jpModel.setContent("test测试"); jpModel.setType("测试"); new JPushApiClientImpl().jPush_rigstId(alias, jpModel); }这里需要注意:极光推送一次推送是限制的,我这边是1000人以下,所有我推送了900一次推.代码:
package com.admin.controller; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.admin.model.JPushChatModel; import com.admin.model.LoginUser; import com.admin.service.JPushApiClient; @Component public class XwxJpushUtil { @Autowired private JPushApiClient jPushApiClient; public void CareUser(JPushChatModel chat) { String sql = "select registrationId from user where registrationId is not null";//查询用户表的registrationId值 Map<String, Object> params = new HashMap<String, Object>(1); List<LoginUser> users = lcjh2Biz.findBySqlBuildObject(sql, params, LoginUser.class, null, null); List<String> reigsts = new ArrayList<String>(users.size()); for (LoginUser loginUser2 : users) { reigsts.add(loginUser2.getRegistrationId()); } int listSize = reigsts.size(); int toIndex = 900; int keyToken = 0; for (int i = 0; i < reigsts.size(); i += 900) { if (i + 900 > listSize) { // 作用为toIndex最后没有900条数据则剩余几条newList中就装几条 toIndex = listSize - i; } List newList = reigsts.subList(i, i + toIndex); jPushApiClient.jPush_rigstId(newList, chat); keyToken++; } } }
推送的model类:
package com.admin.model; import java.util.Date; /** * 消息推送 * * @author Mr.ShyMe * */ public class JPushChatModel { private String type; private String title; private String content; private String sendUserId; private String url; private String dataId; // 文章ID private String wzType; // 文章类型 public String getType() { return type; } public void setType(String type) { this.type = type; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getSendUserId() { return sendUserId; } public void setSendUserId(String sendUserId) { this.sendUserId = sendUserId; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getDataId() { return dataId; } public void setDataId(String dataId) { this.dataId = dataId; } public String getWzType() { return wzType; } public void setWzType(String wzType) { this.wzType = wzType; } }
推送解决