一、uni-app 端设置
1、打开 HBuilder X,在项目的 manifest.json 中找到 APP 模块配置,右侧将 Push(消息推送)勾选,会自动勾选 uniPush 服务。
2、点击 uniPush 下方的配置按钮(登录),点击 Uni Push 下的修改应用信息,将右侧内容填好,如果安卓使用的是公共测试证书的话,直接填 B0:1D:06:18:0D:00:3E:79:C7:B9:08:89:93:B8:E5:AE:7A:19:B0:DA:11:61:AA:09:7C:7F:39:8A:6F:51:4F:A7 即可。如果是自有证书,可以去搜查看证书的 sha256,将自己证书的 sha256 填入即可(一定要是 sha256,不要用 sha1)
输入完成后在 Uni Push 下的 Uni Push,右侧选择配置管理,应用配置,就可以看到所需的一些东西。
注:ios 需要去重新创建一个推送证书,太繁琐了,后续会发出发布 iosAPP 的教程。
到了这一基本就可以不用靠 Java 来进行简单的推送了,前提是 APP 必须是自定义基座或者云打包的 APP。不能使用公共基座的 APP。
3、点击 Uni Push 右侧“创建推送”下的“通知消息”,平台选择安卓(IOS 需要使用透传消息),输入通知标题和通知内容,目标用户选择 CID 用户,输入 CID 后点击预览。
CID 获取方式为:在 uni-app 端,使用自定义调试基座获取手机 CID。
// #ifdef APP-PLUS let clientId = plus.push.getClientInfo().clientId; console.log(clientId); // #endif
获取到 cid 后输入上方图片 CID 的位置,然后点击预览,在测试预览中使用刚刚拿到的 cid 进行测试,点击预览,先不用点击确定。
查看手机状态栏是否出现通知消息。
4、收到消息通知后在 uniapp 项目的 app.vue 中需要监听消息通知。
onLaunch: function() { // #ifdef APP-PLUS let _this = this; let clientInfo = plus.push.getClientInfo(); plus.push.addEventListener("click", function(msg) { uni.getStorage({ key: 'ttoken', success() { if (msg.hasOwnProperty("aps")) { if (msg.aps == null) { let payloadJSON = JSON.parse(msg.payload); if (payloadJSON.id != 0) { uni.navigateTo({ url: '/subPackage/pages/houses/newDetails?id=' + payloadJSON.id + '&houseName=' + encodeURI(payloadJSON.houseName) + '&planId=' + payloadJSON.planId }) } else { uni.switchTab({ url: '/pages/floor/index' }) } } else { if (msg.payload.id != 0) { uni.navigateTo({ url: '/subPackage/pages/houses/newDetails?id=' + msg.payload.id + '&houseName=' + encodeURI(msg.payload.houseName) + '&planId=' + msg.payload.planId }) } } } else { if (msg.payload.hasOwnProperty("id")) { if (msg.payload.id != 0) { uni.navigateTo({ url: '/subPackage/pages/houses/newDetails?id=' + msg.payload.id + '&houseName=' + encodeURI(msg.payload.houseName) + '&planId=' + msg.payload.planId, }) } else { uni.switchTab({ url: '/pages/floor/index' }) } } } }, fail() { uni.showToast({ title: '请先登录', icon: 'none', duration: 800 }) } }) }, false); plus.push.addEventListener("receive", function(msg) { if (msg.hasOwnProperty("type") && msg.payload != null) { var options = { cover: false }; plus.push.createMessage(msg.content, JSON.stringify(msg.payload), options); } }, false);
二、Java 端设置
1、pom.xml
添加
<repositories> <repository> <id>getui-nexus</id> <url>http://mvn.gt.getui.com/nexus/content/repositories/releases/</url> </repository> </repositories>
<dependency> <groupId>com.gexin.platform</groupId> <artifactId>gexin-rp-sdk-http</artifactId> <version>4.1.2.0</version> </dependency>
懒得解释直接贴代码
package com.fuyoust.base.utils.AppPush; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.fuyoust.base.model.TbAppClientInfo; import com.fuyoust.base.model.TbOfShanheHostBox.TbPlan; import com.gexin.rp.sdk.base.IPushResult; import com.gexin.rp.sdk.base.impl.ListMessage; import com.gexin.rp.sdk.base.impl.Target; import com.gexin.rp.sdk.base.notify.Notify; import com.gexin.rp.sdk.base.payload.APNPayload; import com.gexin.rp.sdk.dto.GtReq; import com.gexin.rp.sdk.http.IGtPush; import com.gexin.rp.sdk.template.StartActivityTemplate; import com.gexin.rp.sdk.template.TransmissionTemplate; import com.gexin.rp.sdk.template.style.Style0; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @author ZhangXiaoYu * @date 2021/4/1 10:47 */ public class SensorPush { private final String appId = ""; private final String appKey = ""; private final String masterSecret = ""; private final String url = "http://sdk.open.api.igexin.com/apiex.htm"; private final String appName = ""; private final String packageName = ""; private List<TbAppClientInfo> clientInfos; private String message; private TbPlan tbPlan; private String deviceName; public SensorPush(List<TbAppClientInfo> clientInfos, String message, TbPlan tbPlan, String deviceName) { this.clientInfos = clientInfos; this.message = message; this.tbPlan = tbPlan; this.deviceName = deviceName; } /** * 善禾传感器触发推送手机状态栏通知 * * @param: clientInfos:发送人群 * message:推送的消息 * tbPlan:设备所在房间 * deviceName:设备名称 * * @return contentId:消息唯一ID * pushType:推送的类型,toSingle为单推,toList为批量推,toApp为给全部用户推送 * expireTime:消息到期时间 */ public Map<String, Object> sensorMsgPushApp() { IGtPush push = new IGtPush(url, appKey, masterSecret); //1、设置消息主内容 String content = deviceName + ":" + message + "! 点此查看 >"; //2、获取模板 TransmissionTemplate template = getTemplate(tbPlan, content, getAPNPayload(tbPlan, content)); //3、创建消息 ListMessage message = getMessage(template); //4、筛选发送人群 List<Target> targets = getTargets(clientInfos); //5、发送 String contentId = push.getContentId(message); IPushResult iPushResult = push.pushMessageToList(contentId, targets); String resultStr = iPushResult.getResponse().toString(); Map<String, Object> resultMap = new HashMap<>(); resultMap.put("contentId", contentId); resultMap.put("pushType", "toList"); resultMap.put("expireTime", message.getOfflineExpireTime()); resultMap.put("result",resultStr); return resultMap; } /** * * @param clientInfos 发送人群 * @return Target列表 */ public List<Target> getTargets(List<TbAppClientInfo> clientInfos){ List<Target> targets = new ArrayList<>(); for (TbAppClientInfo client : clientInfos) { Target target = new Target(); target.setAppId(appId); target.setClientId(client.getClientId()); targets.add(target); } return targets; } /** * 创建消息 * * @param template 透传模板 * @return 消息 */ public ListMessage getMessage(TransmissionTemplate template){ ListMessage message = new ListMessage(); message.setData(template); message.setOffline(true);//消息是否离线 message.setOfflineExpireTime(3600000 * 24);//消息到期时间一天 return message; } /** * 创建模板 * * @param tbPlan 房间信息 * @param content 消息主体内容 * @param payload ios离线通知 * @return 透传模板 */ public TransmissionTemplate getTemplate(TbPlan tbPlan, String content, APNPayload payload) { TransmissionTemplate template = new TransmissionTemplate(); template.setAppkey(appKey); template.setAppId(appId); template.setTransmissionType(2); Map<String, Object> payloadMap = new HashMap<>(); Map<String, Object> contentMap = new HashMap<>(); if (tbPlan == null) { payloadMap.put("id", "0"); } else { payloadMap.put("id", tbPlan.getIdx()); payloadMap.put("houseName", tbPlan.getName()); payloadMap.put("planId", tbPlan.getId()); } String title = ""; contentMap.put("title", title); contentMap.put("content", content); contentMap.put("payload", payloadMap); JSONObject contentStr = new JSONObject(contentMap); template.setTransmissionContent(contentStr.toString()); template.setAPNInfo(payload); Notify notify = new Notify(); notify.setTitle(""); notify.setContent(content); String payloadStr = new JSONObject(payloadMap).toString(); notify.setPayload(payloadStr); notify.setType(GtReq.NotifyInfo.Type._intent); String intent = "intent:#Intent;action=android.intent.action.oppopush;launchFlags=0x14000000;component=" + packageName + "/io.dcloud.PandoraEntry;" + "S.UP-OL-SU=true;S.title=" + appName + ";S.content=" + message + ";S.payload=" + payloadStr + ";end"; notify.setIntent(intent); template.set3rdNotifyInfo(notify); return template; } /** * 创建ios离线APN通知 * * @param tbPlan 房间信息 * @param content 消息主体内容 * @return APNPayload */ public APNPayload getAPNPayload(TbPlan tbPlan, String content) { APNPayload payload = new APNPayload(); if (tbPlan == null) { payload.addCustomMsg("id", "0"); } else { payload.addCustomMsg("id", tbPlan.getIdx()); payload.addCustomMsg("houseName", tbPlan.getName()); payload.addCustomMsg("planId", tbPlan.getId()); } payload.setContentAvailable(0); payload.setSound("default"); payload.setAutoBadge("0"); payload.setAlertMsg(getAlertMsg(content)); return payload; } public APNPayload.DictionaryAlertMsg getAlertMsg(String content) { APNPayload.DictionaryAlertMsg alertMsg = new APNPayload.DictionaryAlertMsg(); alertMsg.setTitle(""); alertMsg.setBody(content); return alertMsg; } }
注:
1、模板选择
安卓和 ios 统一使用一个模板“TransmissionTemplate”,上方的 appid、appKey、masterSecret,在 UniPush 中获取。
2、APNPayload 是 ios 专用离线推送。
3、Notify 是配置安卓离线推送的,
三、厂家配置
依此申请各个平台的开发者账号,这里使用华为和 oppo 来举例。注意,需要 APP 上线在华为应用市场中。
1、华为
打开华为推送后台
https://developer.huawei.com/consumer/cn/devunion/openPlatform/html/memberCenter.html#/serviceCards/
选择“开发服务下的 PUSH”,进去后选择自己的 APP 项目,点击左上角的项目设置。框选中的就是华为推送需要的必须项。
注意将 agconnect-services.json 文件从华为推送那里下载出来,appId、appSecret 拿到。SHA256 证书指纹就是上面用到的 SHA256 证书指纹。
2、OPPO
https://open.oppomobile.com/newservice/capability?pagename=push
点击申请接入,一样需要自己的 APP 已经上线,这里说一下 OPPO 这边,如果你在华为应用商城上线的话,即使没有在 oppo 中上线,也是可以在 oppo 应用市场搜索到你的 APP 的,然后可以在 OPPO 开发者平台中使用包名和证书啥的将 APP 认领在自己的开发者账户下。
认领流程:https://open.oppomobile.com/wiki/doc#id=10182
点击消息推送,然后点击自己的 APP,将所需要的的秘钥拿到。
这边说的需要申请开通消息推送权限,跟着 oppo 的文档走就行,审核速度很快。
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于