• uni app push 发送透传通知 支持安卓和苹果(最终版)


    后台

    1. package co.yixiang.common.utils;
    2. import co.yixiang.modules.yinsheng.utils.StringUtil;
    3. import co.yixiang.utils.StringUtils;
    4. import com.alibaba.fastjson.JSONObject;
    5. import com.gexin.rp.sdk.base.IPushResult;
    6. import com.gexin.rp.sdk.base.impl.AppMessage;
    7. import com.gexin.rp.sdk.base.notify.Notify;
    8. import com.gexin.rp.sdk.base.payload.APNPayload;
    9. import com.gexin.rp.sdk.dto.GtReq;
    10. import com.gexin.rp.sdk.http.IGtPush;
    11. import com.gexin.rp.sdk.template.TransmissionTemplate;
    12. import java.io.IOException;
    13. import java.util.*;
    14. public class UniPushUtil {
    15. // STEP1:获取应用基本信息
    16. private static String appId = "";
    17. private static String appKey = "";
    18. private static String masterSecret = "";
    19. private static String url = "http://sdk.open.api.igexin.com/apiex.htm";
    20. public static boolean uniPush(String title,String content,Integer type,Integer contentId){
    21. Map map=new HashMap();
    22. map.put("type",type);
    23. map.put("contentId",contentId);
    24. map.put("title",title);
    25. map.put("content",content);
    26. JSONObject jsonObj=new JSONObject(map);
    27. String s = jsonObj.toString();
    28. IGtPush push = new IGtPush(url, appKey, masterSecret);
    29. TransmissionTemplate template = new TransmissionTemplate();
    30. template.setAppId(appId);
    31. template.setAppkey(appKey);
    32. template.setTransmissionType(2);// // 透传消息设置;1:立即启动APP;2:客户端收到消息后需要自行处理,如果设置为1,对用户使用不友好,不推荐使用
    33. template.setTransmissionContent(s);
    34. Notify notify = new Notify();
    35. notify.setTitle(title);
    36. notify.setContent(content);
    37. notify.setIntent("intent:#Intent;action=android.intent.action.oppopush;launchFlags=0x14000000;component=uni.UNI91FE773/io.dcloud.PandoraEntry;S.UP-OL-SU=true;S.title="+title+";S.content="+content+";S.payload="+s+";end");
    38. notify.setType(GtReq.NotifyInfo.Type._intent);
    39. template.set3rdNotifyInfo(notify);//设置第三方通知
    40. APNPayload apnpayload = new APNPayload();
    41. apnpayload.setSound("");
    42. apnpayload.setAlertMsg(getDictionaryAlertMsg((String)map.get("title"),(String)map.get("content")));
    43. Set> set = map.entrySet();
    44. for (Map.Entry entry:set) {
    45. apnpayload.addCustomMsg(entry.getKey(),entry.getValue());
    46. }
    47. template.setAPNInfo(apnpayload);
    48. List appIds = new ArrayList();
    49. appIds.add(appId);
    50. AppMessage message = new AppMessage();
    51. message.setData(template);
    52. message.setAppIdList(appIds);
    53. message.setOffline(true);
    54. message.setOfflineExpireTime(1000 * 600); // 时间单位为毫秒
    55. IPushResult ret = push.pushMessageToApp(message);
    56. String result = (String)ret.getResponse().get("result");
    57. return StringUtils.equals(result,"ok")?true:false;
    58. //System.out.println(ret.getResponse().toString());
    59. }
    60. public static void main(String[] args) throws IOException {
    61. String title="我是标题9";
    62. String content="我是内容9";
    63. Integer type=0;
    64. Integer contentId=311;
    65. Map map=new HashMap();
    66. map.put("type",type);
    67. map.put("contentId",contentId);
    68. map.put("title",title);
    69. map.put("content",content);
    70. JSONObject jsonObj=new JSONObject(map);
    71. String s = jsonObj.toString();
    72. IGtPush push = new IGtPush(url, appKey, masterSecret);
    73. /*Style0 style = new Style0();
    74. style.setTitle(title);
    75. style.setText(content);
    76. style.setRing(true); // 设置响铃
    77. style.setVibrate(true); // 设置震动*/
    78. // STEP4:选择通知模板
    79. /*StartActivityTemplate template = new StartActivityTemplate ();
    80. template.setAppId(appId);
    81. template.setAppkey(appKey);
    82. template.setStyle(style);
    83. String intent = "intent:#Intent;action=android.intent.action.oppopush;launchFlags=0x14000000;component=uni.UNI91FE773/io.dcloud.PandoraEntry;S.UP-OL-SU=true;S.title="+title+";S.content="+content+";S.payload="+s+";end";
    84. template.setIntent(intent);*/
    85. TransmissionTemplate template = new TransmissionTemplate();
    86. template.setAppId(appId);
    87. template.setAppkey(appKey);
    88. template.setTransmissionType(2);// // 透传消息设置;1:立即启动APP;2:客户端收到消息后需要自行处理,如果设置为1,对用户使用不友好,不推荐使用
    89. template.setTransmissionContent(s);
    90. Notify notify = new Notify();
    91. notify.setTitle(title);
    92. notify.setContent(content);
    93. notify.setIntent("intent:#Intent;action=android.intent.action.oppopush;launchFlags=0x14000000;component=uni.UNI91FE773/io.dcloud.PandoraEntry;S.UP-OL-SU=true;S.title="+title+";S.content="+content+";S.payload="+s+";end");
    94. notify.setType(GtReq.NotifyInfo.Type._intent);
    95. template.set3rdNotifyInfo(notify);//设置第三方通知
    96. APNPayload apnpayload = new APNPayload();
    97. apnpayload.setSound("");
    98. //apnpayload.setAlertMsg(new APNPayload.SimpleAlertMsg("hello"));
    99. apnpayload.setAlertMsg(getDictionaryAlertMsg((String)map.get("title"),(String)map.get("content")));
    100. //传送的附加信息,用于解析
    101. /*apnpayload.addCustomMsg("info","测试参数");
    102. apnpayload.addCustomMsg("info1","测试参数2");*/
    103. Set> set = map.entrySet();
    104. for (Map.Entry entry:set) {
    105. apnpayload.addCustomMsg(entry.getKey(),entry.getValue());
    106. }
    107. template.setAPNInfo(apnpayload);
    108. // STEP5:定义"AppMessage"类型消息对象,设置推送消息有效期等推送参数
    109. List appIds = new ArrayList();
    110. appIds.add(appId);
    111. AppMessage message = new AppMessage();
    112. message.setData(template);
    113. message.setAppIdList(appIds);
    114. message.setOffline(true);
    115. message.setOfflineExpireTime(1000 * 600); // 时间单位为毫秒
    116. /* try {
    117. message.setPushTime("202110211505");
    118. } catch (ParseException e) {
    119. e.printStackTrace();
    120. }*/
    121. // STEP6:执行推送
    122. IPushResult ret = push.pushMessageToApp(message);
    123. System.out.println(ret.getResponse().toString());
    124. }
    125. /**
    126. * 苹果消息文字设置
    127. * @return
    128. */
    129. private static APNPayload.DictionaryAlertMsg getDictionaryAlertMsg(String title,String body){
    130. APNPayload.DictionaryAlertMsg alertMsg = new APNPayload.DictionaryAlertMsg();
    131. alertMsg.setTitle(title);
    132. alertMsg.setBody(body);
    133. alertMsg.setActionLocKey("ActionLockey");
    134. //显示关闭和查看两个按钮的消息;
    135. alertMsg.setLocKey("LocKey");
    136. alertMsg.addLocArg("loc-args");
    137. alertMsg.setLaunchImage("launch-image");
    138. // iOS8.2以上版本支持
    139. alertMsg.setTitleLocKey("TitleLocKey");
    140. alertMsg.addTitleLocArg("TitleLocArg");
    141. return alertMsg;
    142. }
    143. }

    uni app 端

    1. //#ifdef APP-PLUS
    2. plus.push.addEventListener("click", function(msg) {
    3. //console.log("click:"+JSON.stringify(msg));
    4. //console.log(msg.payload);
    5. if(msg.payload.type==0){
    6. setTimeout(function() {
    7. uni.navigateTo({
    8. url:"/pages/shop/GoodsCon/index?id="+msg.payload.contentId,
    9. })
    10. }, 1000)
    11. }else{
    12. setTimeout(function() {
    13. uni.navigateTo({
    14. url:"/pages/shop/news/NewsDetail/index?id="+msg.payload.contentId,
    15. })
    16. }, 1000)
    17. }
    18. //获得个推用户信息
    19. //let pinf = plus.push.getClientInfo();
    20. //console.log("pinf:"+JSON.stringify(pinf));
    21. }, false);
    22. plus.push.addEventListener("receive", function(msg) {
    23. //console.log("receive:"+JSON.stringify(msg));
    24. //console.log(msg.payload);
    25. //获得个推用户信息
    26. //let pinf = plus.push.getClientInfo();
    27. //console.log("pinf:"+JSON.stringify(pinf));
    28. var platform = uni.getSystemInfoSync().platform;
    29. if(platform == "ios"){
    30. //ios平台应用在前台时,不能收到通知消息,只能走透传,在创建一条本地消息
    31. if (msg.type == "receive"){// 这里判断触发的来源,否则一直推送。
    32. var content = JSON.parse(msg.content);
    33. plus.push.createMessage(content.content, JSON.stringify(msg.payload), { title: content.title });
    34. }
    35. }else if (platform == 'android'){
    36. //console.log("content:"+msg.content);
    37. //console.log("title:"+msg.title);
    38. plus.push.createMessage(msg.content, JSON.stringify(msg.payload), { title: msg.title });
    39. /* var options = {cover:false};
    40. var str = ":欢迎使用Html5 Plus创建本地消息!";
    41. plus.push.createMessage(str, "LocalMSG", options); */
    42. }
    43. }, false);
    44. plus.push.getClientInfoAsync((info) => {
    45. let cid = info["clientid"];
    46. console.log("cid:"+cid);
    47. });
    48. // #endif

    前端代码放在 App.vue -> onLaunch 方法内

  • 相关阅读:
    Java设计模式之解释器模式
    JavaScript基础语法(数据类型)
    P8198 [传智杯 #4 决赛] 背单词的小智 —二分答案
    【Windows系统5分钟搭建Linux环境】
    怎样实现纯前端百万行数据秒级响应
    网格搜索和交叉验证
    Postman进阶篇(十一)-在脚本中使用pm对象访问接口请求(pm.request.*)
    SpringCloud微服务实战——搭建企业级开发框架(四十七):【移动开发】整合uni-app搭建移动端快速开发框架-添加Axios并实现登录功能
    Qt常用的多线程使用方式
    金九银十招聘季, APP测试面试题助你拿高薪Offer
  • 原文地址:https://blog.csdn.net/qq_31683775/article/details/126266756