Commit e822867b by giaogiao

完成apns推送证书数据库动态配置

parent b4e1cb20
......@@ -45,7 +45,7 @@ public class IosApnsBase64Test {
InputStream inputStream2 = new ByteArrayInputStream(decode);
IosPush.push(inputStream2, Boolean.FALSE, deviceToken, alertTitle, alertBody,
IosPush.push("123456", inputStream2, Boolean.FALSE, deviceToken, alertTitle, alertBody,
contentAvailable, customProperty, badge
, DeliveryPriority.IMMEDIATE, PushType.ALERT, topicBundleId,
"default");
......
package io.geekidea.springbootplus.test;
import cn.hutool.core.codec.Base64;
import com.wecloud.im.entity.ImIosApns;
import com.wecloud.im.service.ImIosApnsService;
import com.wecloud.im.ws.sender.IosPush;
import io.geekidea.springbootplus.framework.shiro.util.SnowflakeUtil;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.io.InputStream;
/**
* 生成apns字符串 并存入数据库
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class SaveApnsTest {
@Autowired
private ImIosApnsService imIosApnsService;
@Test
public void test() throws Exception {
String apnsCertificatePath = "frogsell_push.p12";
String topicBundleId = "com.jdw.frogsell";
String pwd = "123456";
InputStream certificate = IosPush.getApnsCertificate(apnsCertificatePath);
String encode = Base64.encode(certificate);
ImIosApns imIosApns = new ImIosApns();
imIosApns.setId(SnowflakeUtil.getId());
imIosApns.setFkAppId(0L);
imIosApns.setApnsFileValue(encode);
imIosApns.setEnv(1);
imIosApns.setBundleId(topicBundleId);
imIosApns.setPwd(pwd);
imIosApnsService.save(imIosApns);
}
}
......@@ -33,7 +33,7 @@ public class ImIosApns extends BaseEntity {
@ApiModelProperty("应用appid")
private Long fkAppId;
@ApiModelProperty("Base62(apns.p12)")
@ApiModelProperty("Base64(apns.p12)")
private String apnsFileValue;
@ApiModelProperty("环境,正式1,测试0")
......
......@@ -27,7 +27,7 @@ public class ImIosApnsQueryVo implements Serializable {
@ApiModelProperty("应用appid")
private Long fkAppId;
@ApiModelProperty("Base62(apns.p12)")
@ApiModelProperty("Base64(apns.p12)")
private String apnsFileValue;
@ApiModelProperty("环境,正式1,测试0")
......
......@@ -48,6 +48,9 @@ public interface ImIosApnsService extends BaseService<ImIosApns> {
*/
ImIosApnsQueryVo getImIosApnsById(Long id) throws Exception;
ImIosApns getImIosApnsByAppId(Long appId);
/**
* 获取分页对象
*
......
package com.wecloud.im.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wecloud.im.entity.ImIosApns;
import com.wecloud.im.mapper.ImIosApnsMapper;
import com.wecloud.im.param.ImIosApnsQueryVo;
......@@ -7,6 +8,8 @@ import com.wecloud.im.service.ImIosApnsService;
import io.geekidea.springbootplus.framework.common.service.impl.BaseServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
......@@ -18,6 +21,7 @@ import org.springframework.transaction.annotation.Transactional;
*/
@Slf4j
@Service
@CacheConfig(cacheNames = "apns")
public class ImIosApnsServiceImpl extends BaseServiceImpl<ImIosApnsMapper, ImIosApns> implements ImIosApnsService {
@Autowired
......@@ -46,6 +50,14 @@ public class ImIosApnsServiceImpl extends BaseServiceImpl<ImIosApnsMapper, ImIos
return imIosApnsMapper.getImIosApnsById(id);
}
@Override
@Cacheable(key = "#p0")
public ImIosApns getImIosApnsByAppId(Long appId) {
return this.getOne(new QueryWrapper<ImIosApns>().lambda().eq(
ImIosApns::getFkAppId, appId
));
}
// @Override
// public Paging<ImIosApnsQueryVo> getImIosApnsPageList(ImIosApnsPageParam imIosApnsPageParam) throws Exception {
// Page<ImIosApnsQueryVo> page;
......
......@@ -79,13 +79,11 @@ public class IosPush {
* @param topicBundleId undleId
* @param sound rtc= "call.caf"; 否则为default
*/
public static void push(InputStream inputStream, Boolean productFlag, final String deviceToken, String alertTitle, String alertBody,
public static void push(String certificatePassword, InputStream inputStream, Boolean productFlag, final String deviceToken, String alertTitle, String alertBody,
boolean contentAvailable, Map<String, Object> customProperty,
int badge, DeliveryPriority priority, PushType pushType, String topicBundleId,
String sound) {
String certificatePassword = "123456";
if (deviceToken == null || "".equals(deviceToken)) {
logger.error("deviceToken=null");
return;
......
package com.wecloud.im.ws.sender;
import cn.hutool.core.codec.Base64;
import com.turo.pushy.apns.DeliveryPriority;
import com.turo.pushy.apns.PushType;
import com.wecloud.im.entity.ImApplication;
import com.wecloud.im.entity.ImClient;
import com.wecloud.im.entity.ImIosApns;
import com.wecloud.im.push.PushUtils;
import com.wecloud.im.service.ImIosApnsService;
import com.wecloud.im.ws.model.request.PushModel;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
/**
* 异步系统推送
......@@ -24,6 +32,9 @@ import java.util.HashMap;
@Slf4j
public class PushTask {
@Autowired
private ImIosApnsService imIosApnsService;
/**
* 谷歌推送地址
*/
......@@ -97,46 +108,9 @@ public class PushTask {
} else if (imApplication.getAndroidPushChannel() == 2) {
//firebase:2
log.info("firebase");
log.info("android_firebase");
String jsonStr = null;
try {
URL url = new URL(API_URL_FCM);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setConnectTimeout(10000);
conn.setRequestMethod("POST");
//不设置默认发送文本格式。设置就是json
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Authorization", "key=" + imApplication.getFirebaseSecret());
JSONObject json = new JSONObject();
//推送到哪台客户端机器
json.put("to", imClientReceiver.getDeviceToken());
JSONObject info = new JSONObject();
info.put("title", pushModel.getTitle());
info.put("body", pushModel.getSubTitle());
//数据消息data 通知消息 notification
json.put("notification", info);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
jsonStr = json.toString();
wr.write(jsonStr);
wr.flush();
InputStream inputStream = conn.getInputStream();
InputStreamReader in = new InputStreamReader(inputStream);
BufferedReader reader = new BufferedReader(in);
String line = reader.readLine();
log.info(line);
wr.close();
reader.close();
} catch (Exception e) {
log.error("FCM push failure: " + jsonStr, e);
}
firebase(pushModel, imClientReceiver, imApplication);
} else if (imApplication.getAndroidPushChannel() == 3) {
// 信鸽3
......@@ -145,7 +119,6 @@ public class PushTask {
} else {
log.info("没有找到推送类型");
return;
}
}
......@@ -173,54 +146,80 @@ public class PushTask {
log.info("firebase");
String jsonStr = null;
try {
URL url = new URL(API_URL_FCM);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setConnectTimeout(10000);
conn.setRequestMethod("POST");
//不设置默认发送文本格式。设置就是json
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Authorization", "key=" + imApplication.getFirebaseSecret());
JSONObject json = new JSONObject();
//推送到哪台客户端机器
json.put("to", imClientReceiver.getDeviceToken());
JSONObject info = new JSONObject();
info.put("title", pushModel.getTitle());
info.put("body", pushModel.getSubTitle());
//数据消息data 通知消息 notification
json.put("notification", info);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
jsonStr = json.toString();
wr.write(jsonStr);
wr.flush();
InputStream inputStream = conn.getInputStream();
InputStreamReader in = new InputStreamReader(inputStream);
BufferedReader reader = new BufferedReader(in);
String line = reader.readLine();
log.info(line);
wr.close();
reader.close();
} catch (Exception e) {
log.error("FCM push failure: " + jsonStr, e);
}
firebase(pushModel, imClientReceiver, imApplication);
} else if (imApplication.getIosPushChannel() == 3) {
// apns原生:3
log.info("apns原生");
log.info("apns原生");
apnsPush(pushModel, imClientReceiver, imApplication);
} else {
log.info("没有找到推送类型");
return;
}
}
public void firebase(PushModel pushModel, ImClient imClientReceiver, ImApplication imApplication) {
String jsonStr = null;
try {
URL url = new URL(API_URL_FCM);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setConnectTimeout(10000);
conn.setRequestMethod("POST");
//不设置默认发送文本格式。设置就是json
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("Authorization", "key=" + imApplication.getFirebaseSecret());
JSONObject json = new JSONObject();
//推送到哪台客户端机器
json.put("to", imClientReceiver.getDeviceToken());
JSONObject info = new JSONObject();
info.put("title", pushModel.getTitle());
info.put("body", pushModel.getSubTitle());
//数据消息data 通知消息 notification
json.put("notification", info);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
jsonStr = json.toString();
wr.write(jsonStr);
wr.flush();
InputStream inputStream = conn.getInputStream();
InputStreamReader in = new InputStreamReader(inputStream);
BufferedReader reader = new BufferedReader(in);
String line = reader.readLine();
log.info(line);
wr.close();
reader.close();
} catch (Exception e) {
log.error("FCM push failure: " + jsonStr, e);
}
}
private void apnsPush(PushModel pushModel, ImClient imClientReceiver, ImApplication imApplication) {
// 查询apns证书
ImIosApns apns = imIosApnsService.getImIosApnsByAppId(imApplication.getId());
Map<String, Object> customProperty = new HashMap<String, Object>(1);
String deviceToken = imClientReceiver.getDeviceToken();
String alertTitle = pushModel.getTitle();
String alertBody = pushModel.getSubTitle();
int badge = 1;
String topicBundleId = apns.getBundleId();
String certificatePassword = apns.getPwd();
boolean contentAvailable = false;
String sound = "default";
// 解码
byte[] decode = Base64.decode(apns.getApnsFileValue());
InputStream inputStream2 = new ByteArrayInputStream(decode);
IosPush.push(certificatePassword, inputStream2, Boolean.FALSE, deviceToken, alertTitle, alertBody,
contentAvailable, customProperty, badge
, DeliveryPriority.IMMEDIATE, PushType.ALERT, topicBundleId,
sound);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment