当前位置:   article > 正文

uniapp消息推送-个推V2_uniapp通过个推开发平台进行消息推送

uniapp通过个推开发平台进行消息推送

V1只能在纯公网环境下使用,V2没有限制

开发前必读-个推文档中心

  1. <dependency>
  2. <groupId>com.getui.push</groupId>
  3. <artifactId>restful-sdk</artifactId>
  4. <version>1.0.0.8</version>
  5. </dependency>
  6. ------------------------------------------------------------------------------------
  7. package org.jeecg.transport.push;
  8. import com.getui.push.v2.sdk.ApiHelper;
  9. import com.getui.push.v2.sdk.GtApiConfiguration;
  10. import com.getui.push.v2.sdk.api.PushApi;
  11. import lombok.Data;
  12. import org.springframework.beans.factory.annotation.Value;
  13. import org.springframework.boot.context.properties.ConfigurationProperties;
  14. import org.springframework.context.annotation.Bean;
  15. import org.springframework.stereotype.Component;
  16. /**
  17. * 个推应用信息
  18. */
  19. @Data
  20. @Component
  21. public class GeTuiConfig {
  22. @Value("${uniPush.AppID}")
  23. private String appID;
  24. @Value("${uniPush.AppKey}")
  25. private String appKey;
  26. @Value("${uniPush.AppSecret}")
  27. private String appSecret;
  28. @Value("${uniPush.MasterSecret}")
  29. private String masterSecret;
  30. @Value("${uniPush.Host}")
  31. private String host;
  32. /**
  33. * 个推接口实例化
  34. * @return
  35. */
  36. @Bean(name = "onlyPushApi")
  37. public PushApi pushApi() {
  38. GtApiConfiguration apiConfiguration = new GtApiConfiguration();
  39. //填写应用配置,参数在“Uni Push”下的“应用配置”页面中获取
  40. apiConfiguration.setAppId(appID);
  41. apiConfiguration.setAppKey(appKey);
  42. apiConfiguration.setMasterSecret(masterSecret);
  43. //实例化ApiHelper对象,用于创建接口对象
  44. ApiHelper apiHelper = ApiHelper.build(apiConfiguration);
  45. //创建对象,建议复用。目前有PushApi、StatisticApi、UserApi
  46. PushApi pushApi = apiHelper.creatApi(PushApi.class);
  47. return pushApi;
  48. }
  49. }
  50. -----------------------------------------------------------------------------------------
  51. package org.jeecg.transport.push;
  52. import com.alibaba.fastjson.JSON;
  53. import com.alibaba.fastjson.JSONObject;
  54. import com.getui.push.v2.sdk.api.PushApi;
  55. import com.getui.push.v2.sdk.common.ApiResult;
  56. import com.getui.push.v2.sdk.dto.req.Audience;
  57. import com.getui.push.v2.sdk.dto.req.AudienceDTO;
  58. import com.getui.push.v2.sdk.dto.req.Settings;
  59. import com.getui.push.v2.sdk.dto.req.message.PushChannel;
  60. import com.getui.push.v2.sdk.dto.req.message.PushDTO;
  61. import com.getui.push.v2.sdk.dto.req.message.PushMessage;
  62. import com.getui.push.v2.sdk.dto.req.message.android.AndroidDTO;
  63. import com.getui.push.v2.sdk.dto.req.message.android.GTNotification;
  64. import com.getui.push.v2.sdk.dto.req.message.android.ThirdNotification;
  65. import com.getui.push.v2.sdk.dto.req.message.android.Ups;
  66. import com.getui.push.v2.sdk.dto.req.message.ios.Alert;
  67. import com.getui.push.v2.sdk.dto.req.message.ios.Aps;
  68. import com.getui.push.v2.sdk.dto.req.message.ios.IosDTO;
  69. import com.getui.push.v2.sdk.dto.res.TaskIdDTO;
  70. import lombok.extern.slf4j.Slf4j;
  71. import org.springframework.beans.factory.annotation.Autowired;
  72. import org.springframework.stereotype.Component;
  73. import org.springframework.stereotype.Service;
  74. import java.util.List;
  75. import java.util.Map;
  76. /**
  77. * 个推工具类
  78. */
  79. @Slf4j
  80. @Service
  81. public class GeTuiUtils {
  82. @Autowired
  83. PushApi pushApi;
  84. /**
  85. * 单cid推送
  86. *
  87. * @param cid
  88. * @param title
  89. * @param content
  90. * @return
  91. */
  92. public boolean pushToSingleByCid(String cid, String title, String content,String payload) {
  93. //推送消息体
  94. PushDTO<Audience> pushDTO = this.buildPushDTO(title, content,payload);
  95. //设置接收人信息
  96. Audience audience = new Audience();
  97. pushDTO.setAudience(audience);
  98. audience.addCid(cid);// cid
  99. //进行cid单推
  100. ApiResult<Map<String, Map<String, String>>> apiResult = pushApi.pushToSingleByCid(pushDTO);
  101. if (apiResult.isSuccess()) {
  102. log.info("Push succeeded. cid:" + cid + ",msg:" + apiResult.getMsg() + ",data:" + apiResult.getData());
  103. return true;
  104. } else {
  105. log.info("Push failed. cid:" + cid + ",code:" + apiResult.getCode() + ", msg: " + apiResult.getMsg());
  106. return false;
  107. }
  108. }
  109. /**
  110. * cid批量推送
  111. *
  112. * @param cidList
  113. * @param title
  114. * @param content
  115. * @return
  116. */
  117. public boolean pushListByCid(List<String> cidList, String title, String content, String payload) {
  118. //批量发送
  119. AudienceDTO audienceDTO = new AudienceDTO();
  120. PushDTO<Audience> pushDTO = this.buildPushDTO(title, content,payload);
  121. //创建消息
  122. ApiResult<TaskIdDTO> createApiResult = pushApi.createMsg(pushDTO);
  123. if (!createApiResult.isSuccess()) {
  124. log.info("批量推送:创建消息失败" + createApiResult.getMsg());
  125. return false;
  126. }
  127. // 设置接收人信息
  128. Audience audience = new Audience();
  129. pushDTO.setAudience(audience);
  130. audience.setCid(cidList);
  131. audienceDTO.setAudience(audience);
  132. audienceDTO.setTaskid(createApiResult.getData().getTaskId());
  133. audienceDTO.setAsync(true);
  134. ApiResult<Map<String, Map<String, String>>> apiResult = pushApi.pushListByCid(audienceDTO);
  135. if (apiResult.isSuccess()) {
  136. System.out.println("Batch push succeeded. msg:" + apiResult.getMsg() + ",data:" + apiResult.getData());
  137. return true;
  138. } else {
  139. System.out.println("Batch push failed. code:" + apiResult.getCode() + ", msg: " + apiResult.getMsg());
  140. return false;
  141. }
  142. }
  143. /**
  144. * 构建推送消息体
  145. *
  146. * @param title
  147. * @param content
  148. * @return
  149. */
  150. private PushDTO<Audience> buildPushDTO(String title, String content,String payload) {
  151. PushDTO<Audience> pushDTO = new PushDTO<>();
  152. // 设置推送参数
  153. pushDTO.setRequestId(System.currentTimeMillis() + "");
  154. pushDTO.setGroupName("test-group");
  155. //配置推送条件
  156. Settings settings = new Settings();
  157. pushDTO.setSettings(settings);
  158. //消息有效期,走厂商消息需要设置该值
  159. settings.setTtl(3600000);
  160. //安卓在线通道走个推推送时的消息体(在线通道不支持ios)
  161. PushMessage pushMessage = new PushMessage();
  162. pushDTO.setPushMessage(pushMessage);
  163. //通知消息
  164. JSONObject trans = new JSONObject();
  165. trans.put("title", title);
  166. trans.put("content", content);
  167. trans.put("payload", payload);
  168. pushMessage.setTransmission(JSON.toJSONString(trans));
  169. //设置离线推送时的消息体
  170. PushChannel pushChannel = new PushChannel();
  171. //安卓离线厂商通道推送的消息体
  172. AndroidDTO androidDTO = new AndroidDTO();
  173. Ups ups = new Ups();
  174. //通知消息
  175. ThirdNotification thirdNotification = new ThirdNotification();
  176. ups.setNotification(thirdNotification);
  177. thirdNotification.setTitle(title);
  178. thirdNotification.setBody(content);
  179. thirdNotification.setClickType("intent");
  180. thirdNotification.setIntent("intent://com.getui.push/detail?#Intent;scheme=gtpushscheme;launchFlags=0x4000000;package=com.getui.demo;component=com.getui.demo/com.getui.demo.DemoActivity;S.payload=payloadStr;end");
  181. androidDTO.setUps(ups);
  182. pushChannel.setAndroid(androidDTO);
  183. //ios离线apn通道推送的消息体
  184. Alert alert = new Alert();
  185. alert.setTitle(title);
  186. alert.setBody(content);
  187. Aps aps = new Aps();
  188. aps.setContentAvailable(0);//0表示普通通知消息(默认为0);1表示静默推送(无通知栏消息),静默推送时不需要填写其他参数。苹果建议1小时最多推送3条静默消息
  189. aps.setSound("default");//自定义铃声:系统铃声设置为:default; 无声设置为:com.gexin.ios.silence,或不填
  190. aps.setAlert(alert);
  191. IosDTO iosDTO = new IosDTO();
  192. iosDTO.setAps(aps);
  193. iosDTO.setType("notify");
  194. iosDTO.setPayload(payload);
  195. pushChannel.setIos(iosDTO);
  196. pushDTO.setPushChannel(pushChannel);
  197. return pushDTO;
  198. }
  199. }
  1. App.vue
  2. -----------------------------------------------------------------------------------------
  3. <script>
  4. import permision from "@/common/permission.js"
  5. import { action } from '@/utils/request.js';
  6. export default {
  7. onLaunch: function() {
  8. console.log('App Launch')
  9. const that = this;
  10. plus.push.setAutoNotification(true);
  11. uni.startLocationUpdate({
  12. "type":"wgs84",
  13. success: res => {
  14. },
  15. fail(res) {
  16. console.log(`startLocationUpdate fail: ${JSON.stringify(res)}`);
  17. }
  18. });
  19. uni.onLocationChange(function(res2) {
  20. if (uni.getStorageSync('userInfo')) {
  21. var currentTime = new Date().getTime();
  22. //获取上次执行的时间
  23. var oldTime = uni.getStorageSync('oldTime');
  24. //判断当前的位置是否和上次位置不一致,并且间隔时间超过5s
  25. if(currentTime - oldTime>5000){
  26. //缓存当前执行的时间
  27. uni.setStorageSync('oldTime',currentTime);
  28. //将位置信息上传后台的自己的代码
  29. let userinfo = JSON.parse(uni.getStorageSync('userInfo'));
  30. var info = plus.push.getClientInfo();
  31. var clientId = info.clientid;
  32. action('post', '/hardware/acceptJSON', {
  33. fun: 'location',
  34. realname: userinfo.realname,
  35. username: userinfo.username,
  36. lng: res2.longitude,
  37. lat: res2.latitude,
  38. clientId: clientId
  39. }, false)
  40. }
  41. }
  42. });
  43. plus.push.addEventListener("click", function(msg) {
  44. if(permision.isIOS) {
  45. console.log("ios:click收到"+JSON.parse(JSON.stringify(msg)).payload);
  46. var obj = JSON.parse(JSON.stringify(msg)).payload;
  47. that.toPushMessagePage(obj.data ? obj.data:obj);
  48. } else {
  49. console.log("安卓:click收到"+JSON.parse(JSON.stringify(msg)));
  50. var obj = JSON.parse(JSON.stringify(msg)).payload;
  51. that.toPushMessagePage(obj.data ? obj.data:obj);
  52. }
  53. });
  54. plus.push.addEventListener('receive',(message)=>{
  55. console.log("receive收到")
  56. //收到透传消息,执行该事件
  57. let payload = message.payload;//自定义内容获取
  58. let text = message.payload.text;
  59. if(permision.isIOS) {
  60. console.log("ios:receive收到")
  61. let payload = message.payload;//自定义内容获取
  62. let text = message.content;
  63. that.createLocalMessage(text,payload);
  64. } else {
  65. console.log("安卓收到")
  66. that.toPushMessagePage(payload);
  67. }
  68. })
  69. },
  70. onShow: function() {
  71. },
  72. methods: {
  73. createLocalMessage(content,data) {
  74. console.log("ios开始创建本地消息了")
  75. plus.push.createMessage(content,{
  76. "data": data
  77. });
  78. },
  79. toPushMessagePage(payload) {
  80. if(typeof payload == 'object') {
  81. payload = JSON.stringify(payload)
  82. }
  83. uni.setStorageSync("obj", payload);
  84. uni.navigateTo({
  85. url: `../pointposition/push`
  86. })
  87. },
  88. },
  89. onHide: function() {
  90. console.log('App Hide')
  91. }
  92. }
  93. </script>
  94. <style>
  95. /*每个页面公共css */
  96. @import './common/uni.css';
  97. page {
  98. width: 100%;
  99. height: 100%;
  100. background-color: #F5F6FA;
  101. }
  102. .content {
  103. background-color: #F5F6FA;
  104. }
  105. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/274530
推荐阅读
相关标签
  

闽ICP备14008679号