java实现极光别名设置_极光推送消息——Alias别称方式(Andirod)

package com.test.controller.common;

importstaticcom.yjl.util.UploadUtil.getErrorMsg;

importstaticcom.yjl.util.UploadUtil.getYjlResponseModel;

importstaticcom.yjl.util.UploadUtil.isSuccess;

importstaticcom.yjl.util.UploadUtil.setErrorMsg;

importstaticcom.yjl.util.UploadUtil.setSuccess;

importstaticcom.yjl.util.UploadUtil.setYjlResponseModel;

import cn.jiguang.common.ClientConfig;

import cn.jiguang.common.resp.APIConnectionException;

import cn.jiguang.common.resp.APIRequestException;

import cn.jpush.api.JPushClient;

import cn.jpush.api.push.PushResult;

import cn.jpush.api.push.model.PushPayload;

import com.google.gson.Gson;

import com.yjl.entity.common.JPushDeviceBean;

import com.yjl.entity.common.YJLResponseModel;

import com.yjl.util.HttpClientUtil;

import com.yjl.util.JPushDevice;

import java.io.UnsupportedEncodingException;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.codec.binary.Base64;

import org.apache.commons.lang3.StringUtils;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;

import org.springframework.web.bind.annotation.ResponseBody;

@Controller

@RequestMapping("/JPushDevice")public classJPushDeviceController{private static Logger logger = LoggerFactory.getLogger(JPushDeviceController.class);//极光访问URL

private static final String URL = "https://device.jpush.cn";//极光用户名

private static final String APPKEY ="极光平台生成的app的appkey";//极光密码

private static final String MASTERSECRET ="极光平台生成的app的mastersecret";//1.推送一个人

@RequestMapping(value = "getOneJPushDevice", method =RequestMethod.POST)

@ResponseBodypublicPushResult getOneJPushDevice(@RequestBody JPushDeviceBean j) {

String aliases=getAliases(j.getAlias_value());//一个别名

PushResult pResult = newPushResult();//别名;

if(StringUtils.isNotBlank(aliases)){//极光推送

JPushClient jpushClient = new JPushClient(MASTERSECRET,APPKEY,null,ClientConfig.getInstance());

PushPayload payload=JPushDevice.oneName(aliases, j.getTitle(), j.getFsmessagecontent(),j.getExtrasMap(),j.getExtra());try{

pResult=jpushClient.sendPush(payload);

System.out.println("Got result 推送一个人 - 3" +pResult );

Thread.sleep(5000);//请求结束后,调用 NettyHttpClient 中的 close 方法,否则进程不会退出。

jpushClient.close();

}catch(APIConnectionException e) {

e.printStackTrace();

}catch(APIRequestException e) {

e.printStackTrace();

}catch(InterruptedException e) {

e.printStackTrace();

}

}returnpResult;

}//2.推送多人//先循环判断别名

@RequestMapping(value = "getAllJPushDevice", method =RequestMethod.POST)

@ResponseBodypublicYJLResponseModel getAllJPushDevice(@RequestBody JPushDeviceBean j) {

setYjlResponseModel(newYJLResponseModel());

setErrorMsg("");

setSuccess(false);

List ulist =j.getUsername();//判断过得别名数组

if(null!=ulist && !ulist.isEmpty()){

String[] username= ulist.toArray(newString[ulist.size()]);//转化为数组//极光推送

JPushClient jpushClient = new JPushClient(MASTERSECRET,APPKEY,null, ClientConfig.getInstance());

PushPayload payload=JPushDevice.AllName(username, j.getTitle(), j.getFsmessagecontent(), j.getCount());//用户别名数组1000个一次,标题,内容,角标数

PushResult pResult;try{

pResult=jpushClient.sendPush(payload);

setSuccess(true);

System.out.println("Got result -" + newGson().toJson(pResult) );try{

Thread.sleep(5000);

}catch(InterruptedException e) {

e.printStackTrace();

}//请求结束后,调用 NettyHttpClient 中的 close 方法,否则进程不会退出。

jpushClient.close();

}catch(APIConnectionException e) {

e.printStackTrace();

}catch(APIRequestException e) {

e.printStackTrace();

}finally{

getYjlResponseModel().setErrorMsg(getErrorMsg());

getYjlResponseModel().setSuccess(isSuccess());

}

}returngetYjlResponseModel();

}//查询别名是否存在

public staticString getAliases(String aliases){

String alia="";

JPushDeviceBean j= newJPushDeviceBean();

j.setAlias_value(aliases);

YJLResponseModel map=getJPushDeviceAliases(j);//查询是否含有别名

Map ma = (Map)map.getData();if(ma.containsKey("registration_ids")){

List list = (List)ma.get("registration_ids");//查询registration_ids的值不为空则含有该别名

if(null!=list && !list.isEmpty()){

System.out.println("查询别名----"+newGson().toJson(list));//在此内可调用极光的推送方法

alia =aliases;//如果存在就给返回该别名

}

}returnalia;

}//--------------------查询别名的方法-------------------------------------------//查询别名(找不到统计项就是 null,否则为统计项的值。)

@RequestMapping(value = "getJPushDeviceAliases", method =RequestMethod.POST)

@ResponseBodypublic staticYJLResponseModel getJPushDeviceAliases(@RequestBody JPushDeviceBean ht) {

setYjlResponseModel(newYJLResponseModel());

setErrorMsg("");

setSuccess(false);

JSONObject response= null;try{

StringBuilder url= new StringBuilder(URL).append("/v3/aliases/"+ht.getAlias_value());

String authorValue=getBase64();

Map para = new HashMap<>();

response=HttpClientUtil.doGet(url.toString(), authorValue, para);if (StringUtils.isNotEmpty((String)response.get("error"))) {

logger.info("getAliases:url+params----" + url +para);throw newException();

}

setSuccess(true);

}catch(Exception e) {

e.printStackTrace();

logger.error("getAliases:url+params----" +e);

}finally{

getYjlResponseModel().setErrorMsg(getErrorMsg());

getYjlResponseModel().setSuccess(isSuccess());

getYjlResponseModel().setData(response);

}returngetYjlResponseModel();

}//String转base64

public staticString getBase64(){

Base64 base64= newBase64();

String base64Sign="";

Stringstring = APPKEY+":"+MASTERSECRET;try{

base64Sign= base64.encodeToString(string.getBytes("UTF-8"));

}catch(UnsupportedEncodingException e) {

e.printStackTrace();

}return "Basic"+base64Sign;

}//测试一下

public static voidmain(String[] args) {

JPushDeviceController JPushDeviceController= newJPushDeviceController();

JPushDeviceBean jd= newJPushDeviceBean();

jd.setAlias_value("40");//此处为待推送的用户id

jd.setTitle("推送消息");

jd.setFsmessagecontent("测试一下服务器端");

PushResult pushResult=JPushDeviceController.getOneJPushDevice(jd);

System.out.println(pushResult);

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值