Commit 73c629636aac5ba6e47a54c83a9fc8a6e9550bf6
1 parent
2b422cef
新增极光推送功能及设备id存储到Redis的逻辑
Showing
6 changed files
with
165 additions
and
0 deletions
sl-express-ms-base-service/pom.xml
| ... | ... | @@ -103,6 +103,12 @@ |
| 103 | 103 | <groupId>org.springframework.boot</groupId> |
| 104 | 104 | <artifactId>spring-boot-starter-data-redis</artifactId> |
| 105 | 105 | </dependency> |
| 106 | + <!-- jiguang-sdk --> | |
| 107 | + <dependency> | |
| 108 | + <groupId>io.github.jpush</groupId> | |
| 109 | + <artifactId>jiguang-sdk</artifactId> | |
| 110 | + <version>5.2.13</version> | |
| 111 | + </dependency> | |
| 106 | 112 | </dependencies> |
| 107 | 113 | |
| 108 | 114 | <build> | ... | ... |
sl-express-ms-base-service/src/main/java/com/sl/ms/base/config/JPushConfig.java
0 → 100644
| 1 | +package com.sl.ms.base.config; | |
| 2 | + | |
| 3 | +import cn.jiguang.sdk.api.PushApi; | |
| 4 | +import feign.Logger; | |
| 5 | +import lombok.extern.slf4j.Slf4j; | |
| 6 | +import org.springframework.context.annotation.Bean; | |
| 7 | +import org.springframework.context.annotation.Configuration; | |
| 8 | + | |
| 9 | +@Slf4j | |
| 10 | +@Configuration | |
| 11 | +public class JPushConfig { | |
| 12 | + | |
| 13 | + @Bean | |
| 14 | + public PushApi pushApi() { | |
| 15 | + return new PushApi.Builder() | |
| 16 | + // .setClient(new feign.okhttp.OkHttpClient(okHttpClient)) // sdk默认使用的feign-okhttp,可自定义,可选 | |
| 17 | + // .setOptions(new Request.Options(10, TimeUnit.SECONDS, 60, TimeUnit.SECONDS, true)) // 可自定义超时参数,可选 | |
| 18 | + // .setRetryer(new Retryer.Default(100, SECONDS.toMillis(1), 5)) // 可自定义重试参数,可选 | |
| 19 | + // .setLoggerLevel(Logger.Level.FULL) // 可自定义日志打印级别,可选 | |
| 20 | + .setAppKey("6f7829173a8ccb6c34653947") // 必填 | |
| 21 | + .setMasterSecret("d9f1963450bcd235eb2d3175") // 必填 | |
| 22 | + .setLoggerLevel(Logger.Level.FULL) | |
| 23 | + .build(); | |
| 24 | + } | |
| 25 | +} | ... | ... |
sl-express-ms-base-service/src/main/java/com/sl/ms/base/service/base/impl/MessageServiceImpl.java
| ... | ... | @@ -3,6 +3,12 @@ package com.sl.ms.base.service.base.impl; |
| 3 | 3 | import cn.hutool.core.bean.BeanUtil; |
| 4 | 4 | import cn.hutool.core.collection.CollUtil; |
| 5 | 5 | import cn.hutool.core.util.ObjectUtil; |
| 6 | +import cn.jiguang.sdk.api.PushApi; | |
| 7 | +import cn.jiguang.sdk.bean.push.PushSendParam; | |
| 8 | +import cn.jiguang.sdk.bean.push.PushSendResult; | |
| 9 | +import cn.jiguang.sdk.bean.push.audience.Audience; | |
| 10 | +import cn.jiguang.sdk.bean.push.message.notification.NotificationMessage; | |
| 11 | +import cn.jiguang.sdk.constants.ApiConstants; | |
| 6 | 12 | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| 7 | 13 | import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; |
| 8 | 14 | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| ... | ... | @@ -13,15 +19,20 @@ import com.sl.ms.base.domain.base.MessageAddDTO; |
| 13 | 19 | import com.sl.ms.base.domain.base.MessageDTO; |
| 14 | 20 | import com.sl.ms.base.domain.base.MessageQueryDTO; |
| 15 | 21 | import com.sl.ms.base.domain.constants.MessageConstants; |
| 22 | +import com.sl.ms.base.domain.enums.MessageBussinessTypeEnum; | |
| 16 | 23 | import com.sl.ms.base.entity.base.MessageEntity; |
| 17 | 24 | import com.sl.ms.base.mapper.base.MessageMapper; |
| 18 | 25 | import com.sl.ms.base.service.base.MessageService; |
| 19 | 26 | import com.sl.transport.common.exception.SLException; |
| 20 | 27 | import com.sl.transport.common.util.PageResponse; |
| 28 | +import lombok.extern.slf4j.Slf4j; | |
| 29 | +import org.springframework.data.redis.core.StringRedisTemplate; | |
| 21 | 30 | import org.springframework.stereotype.Service; |
| 22 | 31 | import org.springframework.transaction.annotation.Transactional; |
| 23 | 32 | |
| 33 | +import javax.annotation.Resource; | |
| 24 | 34 | import java.time.LocalDateTime; |
| 35 | +import java.util.Arrays; | |
| 25 | 36 | import java.util.Collections; |
| 26 | 37 | import java.util.List; |
| 27 | 38 | import java.util.stream.Collectors; |
| ... | ... | @@ -30,8 +41,15 @@ import java.util.stream.Collectors; |
| 30 | 41 | * 消息相关业务 |
| 31 | 42 | */ |
| 32 | 43 | @Service |
| 44 | +@Slf4j | |
| 33 | 45 | public class MessageServiceImpl extends ServiceImpl<MessageMapper, MessageEntity> implements MessageService { |
| 34 | 46 | |
| 47 | + @Resource | |
| 48 | + private StringRedisTemplate stringRedisTemplate; | |
| 49 | + | |
| 50 | + @Resource | |
| 51 | + private PushApi pushApi; | |
| 52 | + | |
| 35 | 53 | /** |
| 36 | 54 | * 新增消息 |
| 37 | 55 | * |
| ... | ... | @@ -46,6 +64,34 @@ public class MessageServiceImpl extends ServiceImpl<MessageMapper, MessageEntity |
| 46 | 64 | this.save(entity); |
| 47 | 65 | } |
| 48 | 66 | |
| 67 | + private void sendJPush(MessageAddDTO messageAddDTO) { | |
| 68 | + if (messageAddDTO.getBussinessType().equals(MessageBussinessTypeEnum.COURIER.getCode())) { | |
| 69 | + String redisKey = "courier:device:" + messageAddDTO.getUserId(); | |
| 70 | + String deviceId = stringRedisTemplate.opsForValue().get(redisKey); | |
| 71 | + if (ObjectUtil.isNotEmpty(deviceId)) { | |
| 72 | + PushSendParam param = new PushSendParam(); | |
| 73 | + | |
| 74 | + NotificationMessage notificationMessage = new NotificationMessage(); | |
| 75 | + notificationMessage.setAlert(messageAddDTO.getTitle()); | |
| 76 | + param.setNotification(notificationMessage); | |
| 77 | + | |
| 78 | + // 目标人群 | |
| 79 | + Audience audience = new Audience(); | |
| 80 | + audience.setRegistrationIdList(Collections.singletonList(deviceId)); | |
| 81 | + | |
| 82 | + // 指定目标 | |
| 83 | + param.setAudience(audience); | |
| 84 | + | |
| 85 | + // 或者发送所有平台 | |
| 86 | + param.setPlatform(ApiConstants.Platform.ALL); | |
| 87 | + | |
| 88 | + // 发送 | |
| 89 | + PushSendResult result = pushApi.send(param); | |
| 90 | + log.info("result:{}", result); | |
| 91 | + } | |
| 92 | + } | |
| 93 | + } | |
| 94 | + | |
| 49 | 95 | /** |
| 50 | 96 | * 标记已读 |
| 51 | 97 | * | ... | ... |
sl-express-ms-base-service/src/test/java/com/sl/ms/base/jpush/JPushTest.java
0 → 100644
| 1 | +package com.sl.ms.base.jpush; | |
| 2 | + | |
| 3 | +import cn.jiguang.sdk.api.PushApi; | |
| 4 | +import cn.jiguang.sdk.bean.push.PushSendParam; | |
| 5 | +import cn.jiguang.sdk.bean.push.PushSendResult; | |
| 6 | +import cn.jiguang.sdk.bean.push.audience.Audience; | |
| 7 | +import cn.jiguang.sdk.bean.push.message.notification.NotificationMessage; | |
| 8 | +import cn.jiguang.sdk.constants.ApiConstants; | |
| 9 | +import lombok.extern.slf4j.Slf4j; | |
| 10 | +import org.junit.jupiter.api.Test; | |
| 11 | +import org.springframework.beans.factory.annotation.Autowired; | |
| 12 | +import org.springframework.boot.test.context.SpringBootTest; | |
| 13 | + | |
| 14 | +import java.util.Arrays; | |
| 15 | +import java.util.HashMap; | |
| 16 | +import java.util.Map; | |
| 17 | + | |
| 18 | +@Slf4j | |
| 19 | +@SpringBootTest | |
| 20 | +public class JPushTest { | |
| 21 | + @Autowired | |
| 22 | + private PushApi pushApi; | |
| 23 | + | |
| 24 | + @Test | |
| 25 | + public void send() { | |
| 26 | + PushSendParam param = new PushSendParam(); | |
| 27 | + // 通知内容 | |
| 28 | + NotificationMessage.Android android = new NotificationMessage.Android(); | |
| 29 | + android.setAlert("this is android alert"); | |
| 30 | + android.setTitle("this is android title"); | |
| 31 | + | |
| 32 | + NotificationMessage.IOS iOS = new NotificationMessage.IOS(); | |
| 33 | + Map<String, String> iOSAlert = new HashMap<>(); | |
| 34 | + iOSAlert.put("title", "this is iOS title"); | |
| 35 | + iOSAlert.put("subtitle", "this is iOS subtitle"); | |
| 36 | + iOS.setAlert(iOSAlert); | |
| 37 | + | |
| 38 | + Map<String, Object> extrasMap = new HashMap<>(); | |
| 39 | + Map<String, Object> extrasParamMap = new HashMap<>(); | |
| 40 | + extrasParamMap.put("key1", "value1"); | |
| 41 | + extrasParamMap.put("key2", "value2"); | |
| 42 | + extrasMap.put("params", extrasParamMap); | |
| 43 | + android.setExtras(extrasMap); | |
| 44 | + iOS.setExtras(extrasMap); | |
| 45 | + | |
| 46 | + NotificationMessage notificationMessage = new NotificationMessage(); | |
| 47 | + notificationMessage.setAlert("this is alert"); | |
| 48 | + notificationMessage.setAndroid(android); | |
| 49 | + notificationMessage.setIos(iOS); | |
| 50 | + param.setNotification(notificationMessage); | |
| 51 | + | |
| 52 | + // 目标人群 | |
| 53 | + Audience audience = new Audience(); | |
| 54 | + audience.setRegistrationIdList(Arrays.asList("170976fa8ba46cae79f", "170976fa8ba46cae79f")); | |
| 55 | + // 指定目标 | |
| 56 | + param.setAudience(audience); | |
| 57 | + | |
| 58 | + // 或者发送所有人 | |
| 59 | +// param.setAudience(ApiConstants.Audience.ALL); | |
| 60 | + | |
| 61 | + // 或者发送所有平台 | |
| 62 | + param.setPlatform(ApiConstants.Platform.ALL); | |
| 63 | + | |
| 64 | + // 短信补充 | |
| 65 | + // param.setSmsMessage(); | |
| 66 | + | |
| 67 | + // 回调 | |
| 68 | + // param.setCallback(); | |
| 69 | + | |
| 70 | + // 发送 | |
| 71 | + PushSendResult result = pushApi.send(param); | |
| 72 | + log.info("result:{}", result); | |
| 73 | + } | |
| 74 | +} | ... | ... |
sl-express-ms-web-courier/src/main/java/com/sl/ms/web/courier/service/impl/LoginServiceImpl.java
| ... | ... | @@ -8,6 +8,7 @@ import com.sl.ms.web.courier.vo.login.AccountLoginVO; |
| 8 | 8 | import com.sl.ms.web.courier.vo.login.LoginVO; |
| 9 | 9 | import com.sl.transport.common.vo.R; |
| 10 | 10 | import lombok.extern.slf4j.Slf4j; |
| 11 | +import org.springframework.data.redis.core.StringRedisTemplate; | |
| 11 | 12 | import org.springframework.stereotype.Service; |
| 12 | 13 | |
| 13 | 14 | import javax.annotation.Resource; |
| ... | ... | @@ -18,6 +19,9 @@ public class LoginServiceImpl implements LoginService { |
| 18 | 19 | @Resource |
| 19 | 20 | private AuthTemplate authTemplate; |
| 20 | 21 | |
| 22 | + @Resource | |
| 23 | + private StringRedisTemplate stringRedisTemplate; | |
| 24 | + | |
| 21 | 25 | /** |
| 22 | 26 | * 根据用户名和密码进行登录 |
| 23 | 27 | * |
| ... | ... | @@ -34,6 +38,14 @@ public class LoginServiceImpl implements LoginService { |
| 34 | 38 | return R.error(loginResult.getMsg()); |
| 35 | 39 | } |
| 36 | 40 | |
| 41 | + // 设备 id 存储到 Redis | |
| 42 | + String deviceId = "170976fa8ba46cae79f"; //accountLoginVO.getDeviceId(); | |
| 43 | + if (deviceId != null) { | |
| 44 | + Long userId = loginResult.getData().getUser().getId(); | |
| 45 | + String redisKey = "courier:device:" + userId; | |
| 46 | + stringRedisTemplate.opsForValue().set(redisKey, deviceId); | |
| 47 | + } | |
| 48 | + | |
| 37 | 49 | return R.success(new LoginVO(loginResult.getData().getToken().getToken())); |
| 38 | 50 | } |
| 39 | 51 | } | ... | ... |
sl-express-ms-web-courier/src/main/java/com/sl/ms/web/courier/vo/login/AccountLoginVO.java