接收解析处理美洽推送的开发着服务接口:
设置服务器地址,请使用美洽管理员帐号登录 美洽,在「设置」 -> 「SDK」中设置。
request.header.authorization 为数据签名
request.body 为消息数据
下面代码中,获取消息头的数据签名,和消息数据,把消息数据按照指定方式加密生成签名,再与消息头中的签名进行比对,如果比对成功,就解析body里面的数据(下面代码中str就是解析出来的body),由开发者服务器推送给用户APP;
代码如例:
import com.kojoker.yipinshe.app.service.MeiQiaService; import com.kojoker.yipinshe.app.service.SendCastService; import net.sf.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.BufferedReader; /** * 美洽 推送离线消息 接收处理接口 * Created by zhangzc on 2017/12/1. */ @Controller @RequestMapping(value = "/meiqia", produces = "application/json;charset=UTF-8") public class MeiQiaPushController { protected final Logger log = LoggerFactory.getLogger(this.getClass()); @Resource(name = "meiQiaService") MeiQiaService meiQiaService; @Resource(name = "sendCastService") SendCastService sendCastService; @RequestMapping(value = "push", method = {RequestMethod.POST, RequestMethod.GET}) public void meiqiaPush(HttpServletRequest request, HttpServletResponse response, BufferedReader br) { // 获取头信息的 Authorization,其中是美洽推送数据的签名 System.out.println("美洽推送接口"); try { String str = meiQiaService.doSign(request, br); if (str != null) { System.out.println("验证成功!!!"); JSONObject jsStrQuery = JSONObject.fromObject(str); // String contentType = jsStrQuery.getString("contentType");//消息内容类型 - text/photo/audio // String customizedData = jsStrQuery.getString("customizedData");//开发者上传的自定义的属性 // String messageId = jsStrQuery.getString("messageId");//消息 id String content = jsStrQuery.getString("content");//消息内容 // String messageTime = jsStrQuery.getString("messageTime");//发送时间 String fromName = jsStrQuery.getString("fromName");//发送人姓名 String deviceToken = jsStrQuery.getString("deviceToken");//发送对象设备的 deviceToken,格式为字符串 // String clientId = jsStrQuery.getString("clientId");//发送对象的顾客 id // String customizedId = jsStrQuery.getString("customizedId");//开发者传的自定义 id String deviceOS = jsStrQuery.getString("deviceOS");//设备系统 // String type = jsStrQuery0.getString("type");//消息类型 - mesage 普通消息 / welcome 欢迎消息 / ending 结束消息 / remark 评价消息 / 留言消息 if ("Android".equals(deviceOS)) { sendCastService.sendAndroidUnicast("1", fromName, content, deviceToken); } else { sendCastService.sendIOSUnicast("1", fromName, content, deviceToken); } } } catch (Exception e) { e.printStackTrace(); } } }
使用到的加密方法:
@Service("meiQiaService") public class MeiQiaServiceImpl implements MeiQiaService { protected final Logger log = LoggerFactory.getLogger(this.getClass()); private String key; @Override public String doSign(HttpServletRequest request, BufferedReader br) throws Exception { String Authorization = request.getHeader("Authorization"); String headerSign = Authorization.replaceAll("meiqia_sign:", ""); System.out.println("headerSign----------"+headerSign); String inputLine; String str = ""; try { while ((inputLine = br.readLine()) != null) { str += inputLine; } br.close(); } catch (IOException e) { System.out.println("IOException: " + e); } System.out.println("str:" + str); String sign = ""; MeiqiaSigner signer = new MeiqiaSigner(key); try { String sign0 = signer.sign(str); sign = sign0.replaceAll("meiqia_sign:",""); System.out.println("sing---------"+sign); } catch (SignatureException e) { e.printStackTrace(); } //对 http-body 进行 HMAC-SHA1 操作,然后就会得到 sign。 //将两个 sign 进行比对,如果一样,就说明该请求合法,可以进行其他操作,如果不对,就舍弃。 if (headerSign.equals(sign)) { System.out.println("对比成功"); return str; } else { return null; } } public String getKey() { return key; } public void setKey(String key) { this.key = key; } }
解析成功后(拿到消息内容,标题,deviceToken(设备唯一标识)):这里是调用友盟的推送接口(单播)进行推送的:
//单播 Android @Override public void sendAndroidUnicast(String messageType, String title, String content, String deviceToken) throws Exception { String str = "{\"messageType\":" + messageType + ",\"messageTime\":" + new Date().getTime() + ",\"messageTitle\":\"" + title + "\",\"messageContent\":\"" + content + "\"}"; AndroidUnicast unicast = new AndroidUnicast(andriodAppkey, andriodAppMasterSecret); unicast.setDeviceToken(deviceToken); unicast.setTicker(title); unicast.setTitle(title); unicast.setText(content); unicast.goAppAfterOpen(); unicast.setPlayLights(true);//消息闪灯 unicast.setPlayVibrate(true);//消息震动 unicast.setPlaySound(true);//消息声音 unicast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION); unicast.goCustomAfterOpen(str);//后续动作,自定义行为 if (modeType == "test") { unicast.setTestMode();//测试 } else if (modeType == "production") { unicast.setProductionMode();//正式 } unicast.setExtraField("test", "helloworld"); client.send(unicast); System.out.println("Andriod 发送成功"+title); } //单播 iOS @Override public void sendIOSUnicast(String messageType, String title, String content, String deviceToken) throws Exception { String str = "{\"messageType\":" + messageType + ",\"messageTime\":" + new Date().getTime() + ",\"messageTitle\":\"" + title + "\",\"messageContent\":\"" + content + "\"}"; IOSUnicast unicast = new IOSUnicast(iOSAppkey, iOSAppMasterSecret); unicast.setDeviceToken(deviceToken); unicast.setTitle(title);//标题 unicast.setBody(content);//内容 unicast.setDescription(title); unicast.setBadge(0); unicast.setSound("default"); unicast.setTestMode(); unicast.setCustomizedField("test", "helloworld"); unicast.setCustomizedField("key", str); client.send(unicast); System.out.println("iOS 发送成功:"+title); }