package com.ks.app.service.manager;
|
|
import com.ks.app.entity.SystemEnum;
|
import com.ks.app.entity.config.AppJumpType;
|
import com.ks.app.entity.config.SystemConfigKey;
|
import com.ks.app.service.inter.config.SystemConfigService;
|
import com.ks.app.vo.AcceptData;
|
import com.ks.lib.common.exception.ParamsException;
|
import com.ks.push.exception.BPushDeviceTokenException;
|
import com.ks.push.exception.BPushTaskException;
|
import com.ks.push.pojo.DO.*;
|
import com.ks.push.service.BDeviceTokenService;
|
import com.ks.push.service.BPushTaskService;
|
import com.ks.push.utils.JPushUtil;
|
import org.apache.dubbo.config.annotation.Reference;
|
import org.springframework.stereotype.Component;
|
|
import javax.annotation.Resource;
|
import java.math.BigDecimal;
|
import java.util.*;
|
import java.util.stream.Collectors;
|
|
/**
|
* @author hxh
|
* @title: PushManager
|
* @description: 推送管理器
|
* @date 2022/4/29 17:33
|
*/
|
@Component
|
public class PushManager {
|
|
@Reference(version = "1.0", check = false)
|
private BDeviceTokenService bdeviceTokenService;
|
|
@Reference(version = "1.0", check = false)
|
private BPushTaskService bPushTaskService;
|
|
@Resource
|
private SystemConfigService systemConfigService;
|
|
/**
|
* @return void
|
* @author hxh
|
* @description 保存token
|
* @date 17:36 2022/4/29
|
* @param: acceptData
|
* @param: uid
|
* @param: regId
|
**/
|
public void saveToken(AcceptData acceptData, Long uid, String regId) throws BPushDeviceTokenException, ParamsException {
|
BPushDeviceToken token = new BPushDeviceToken();
|
if (uid != null) {
|
token.setUid(uid + "");
|
}
|
token.setType(PushPlatform.jpush);
|
token.setAppCode(systemConfigService.getValueCache(acceptData.getSystem(), SystemConfigKey.androidPushAppCode));
|
token.setVersionCode(acceptData.getVersion());
|
token.setToken(regId);
|
token.setDeviceId(acceptData.getUtdId());
|
token.setBuildVersion(acceptData.getOsVersion());
|
token.setBuildModel(acceptData.getDeviceType());
|
token.setCreateTime(new Date());
|
bdeviceTokenService.save(token);
|
}
|
|
|
/**
|
* @return void
|
* @author hxh
|
* @description 绑定用户ID
|
* @date 17:40 2022/4/29
|
* @param: system
|
* @param: uid
|
* @param: deviceId
|
**/
|
public void bindUid(SystemEnum system, Long uid, String deviceId) {
|
bdeviceTokenService.bindUid(systemConfigService.getValueCache(system, SystemConfigKey.androidPushAppCode), deviceId, uid + "");
|
}
|
|
/**
|
* @return void
|
* @author hxh
|
* @description 解绑用户ID
|
* @date 17:41 2022/4/29
|
* @param: system
|
* @param: deviceId
|
**/
|
public void unBind(SystemEnum system, String deviceId) {
|
bdeviceTokenService.unBindUid(systemConfigService.getValueCache(system, SystemConfigKey.androidPushAppCode), deviceId);
|
}
|
|
|
/**
|
* @return void
|
* @author hxh
|
* @description 推送签到
|
* @date 16:40 2022/5/7
|
* @param: system
|
* @param: uid
|
**/
|
public void pushSignIn(SystemEnum system, List<Long> uidList) throws BPushTaskException {
|
if (uidList == null || uidList.size() == 0) {
|
return;
|
}
|
Map<String, String> extras = new HashMap<>();
|
extras.put("type", AppJumpType.signIn.name());
|
String title = "签到提醒";
|
String content = "今日你还未签到,签到最高可领150金币!";
|
push(system, uidList, title, content, extras);
|
}
|
|
/**
|
* @return void
|
* @author hxh
|
* @description 推送金币结算
|
* @date 16:41 2022/5/7
|
* @param: system
|
* @param: uid
|
* @param: goldCorn
|
* @param: money
|
**/
|
public void pushGoldCornSettle(SystemEnum system, Long uid, int goldCorn, BigDecimal money) throws BPushTaskException {
|
Map<String, String> extras = new HashMap<>();
|
extras.put("type", AppJumpType.extract.name());
|
String title = "到账提醒";
|
String content = String.format("昨日金币折算成功,入账%s元", money.setScale(2).toString());
|
push(system, Arrays.asList(new Long[]{uid}), title, content, extras);
|
}
|
|
/**
|
* @return void
|
* @author hxh
|
* @description 推送内容
|
* @date 16:55 2022/5/7
|
* @param: system
|
* @param: uid
|
* @param: title
|
* @param: content
|
* @param: extras
|
**/
|
private void push(SystemEnum system, List<Long> uidList, String title, String content, Map<String, String> extras) throws BPushTaskException {
|
BPushTask bPushTask = new BPushTask();
|
BPushMessage message = new BPushMessage();
|
message.setMessage(false);
|
message.setExtras(extras);
|
message.setAndroidActivity(getPushIntent(system));
|
message.setTitle(title);
|
message.setContent(content);
|
|
|
bPushTask.setMessage(message);
|
bPushTask.setAppCode(systemConfigService.getValueCache(system, SystemConfigKey.androidPushAppCode));
|
|
BPushFilter filter = new BPushFilter();
|
if (uidList == null || uidList.size() == 0) {
|
//推送30天内还活跃的用户
|
filter.setMinActiveTime(new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24L * 30));
|
} else {
|
filter.setUidList(uidList.stream().map(item -> item + "").collect(Collectors.toList()));
|
}
|
bPushTask.setFilter(filter);
|
String taskId = bPushTaskService.createTask(bPushTask);
|
bPushTaskService.startPush(taskId);
|
}
|
|
private String getPushIntent(SystemEnum system) {
|
return JPushUtil.createIntent(systemConfigService.getValueCache(system, SystemConfigKey.androidPushActivity), system.getPackageName());
|
}
|
|
|
}
|