uni-app是支持消息推送的,參考以下文檔:php
UniPush介紹vue
UniPush使用指南java
UniPush開通指南git
如何自定義推送通知的圖標?github
開源項目uniapp-adminjson
不一樣角色的用戶登錄App,收到不一樣的待辦提醒。即誰處理這個待辦任務,誰會收到這個提醒。對不一樣角色的用戶推送待辦消息api
由於uni-app的推送是集成了個推,因此查看個推文檔接入方案bash
由於後臺是java語言,因此查看java集成指南服務器
下載服務端SDK開發工具包,下載地址爲:www.getui.com/download/do…
導入"GETUI_SERVER_SDK\資源文件」目錄下的全部jar包
# uni-push
mvn install:install-file -Dfile="gexin-rp-fastjson-1.0.0.3.jar" -DgroupId=com.gexin.platform -DartifactId=gexin-rp-fastjson -Dversion=1.0.0.3 -Dpackaging=jar
mvn install:install-file -Dfile="gexin-rp-sdk-base-4.0.0.30.jar" -DgroupId=com.gexin.platform -DartifactId=gexin-rp-sdk-base -Dversion=4.0.0.30 -Dpackaging=jar
mvn install:install-file -Dfile="gexin-rp-sdk-http-4.1.0.5.jar" -DgroupId=com.gexin.platform -DartifactId=gexin-rp-sdk-http -Dversion=4.1.0.5 -Dpackaging=jar
mvn install:install-file -Dfile="gexin-rp-sdk-template-4.0.0.24.jar" -DgroupId=com.gexin.platform -DartifactId=gexin-rp-sdk-template -Dversion=4.0.0.24 -Dpackaging=jar
mvn install:install-file -Dfile="protobuf-java-2.5.0.jar" -DgroupId=com.google.protobuf -DartifactId=protobuf-java -Dversion=2.5.0 -Dpackaging=jar
複製代碼
<!-- uni push -->
<dependency>
<groupId>com.gexin.platform</groupId>
<artifactId>gexin-rp-sdk-base</artifactId>
<version>4.0.0.30</version>
</dependency>
<dependency>
<groupId>com.gexin.platform</groupId>
<artifactId>gexin-rp-sdk-template</artifactId>
<version>4.0.0.24</version>
</dependency>
<dependency>
<groupId>com.gexin.platform</groupId>
<artifactId>gexin-rp-sdk-http</artifactId>
<version>4.1.0.5</version>
</dependency>
<dependency>
<groupId>com.gexin.platform</groupId>
<artifactId>gexin-rp-fastjson</artifactId>
<version>1.0.0.3</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>2.5.0</version>
</dependency>
複製代碼
按照UniPush開通指南,開通UniPush,獲取appId、appKey等,編寫下面簡單demo,客戶端就會收到消息啦~
客戶端接收消息代碼,參考4.3
public class AppPush {
// STEP1:獲取應用基本信息
private static String appId = "";
private static String appKey = "";
private static String masterSecret = "";
private static String url = "http://sdk.open.api.igexin.com/apiex.htm";
public static void main(String[] args) throws IOException {
IGtPush push = new IGtPush(url, appKey, masterSecret);
Style0 style = new Style0();
// STEP2:設置推送標題、推送內容
style.setTitle("請輸入通知欄標題");
style.setText("請輸入通知欄內容");
// 註釋採用默認圖標
// style.setLogo("push.png"); // 設置推送圖標
// STEP3:設置響鈴、震動等推送效果
style.setRing(true); // 設置響鈴
style.setVibrate(true); // 設置震動
// STEP4:選擇通知模板
NotificationTemplate template = new NotificationTemplate();
template.setAppId(appId);
template.setAppkey(appKey);
template.setStyle(style);
// 點擊消息打開應用
template.setTransmissionType(1);
// 傳遞自定義消息
template.setTransmissionContent("自定義消息,能夠是json
字符串");
// STEP5:定義"AppMessage"類型消息對象,設置推送消息有效期等推送參數
List<String> appIds = new ArrayList<String>();
appIds.add(appId);
AppMessage message = new AppMessage();
message.setData(template);
message.setAppIdList(appIds);
message.setOffline(true);
message.setOfflineExpireTime(1000 * 600); // 時間單位爲毫秒
// STEP6:執行推送
IPushResult ret = push.pushMessageToApp(message);
System.out.println(ret.getResponse().toString());
}
}
複製代碼
/** * 處理推送消息 */
handlePush() {
// #ifdef APP-PLUS
const _self = this
const _handlePush = function(message) {
// 獲取自定義信息
let payload = message.payload
try {
// JSON解析
payload = JSON.parse(payload)
// 攜帶自定義信息跳轉應用頁面
uni.navigateTo({
url: '/pages/xxx?data=' + JSON.stringify(payload)
})
} catch(e) {}
}
// 事件處理
plus.push.addEventListener('click', _handlePush)
plus.push.addEventListener('receive', _handlePush)
// #endif
},
複製代碼
應用確實接收到了消息,可是全部角色的用戶都會接收到和本身不想關的待辦任務消息。這是違背需求的!因此基於此,做者研究了服務端發送到客戶端消息的原理:
實際上,消息無論是單推仍是羣推,推送的目標都是clientid,clientid標識每一個客戶端的身份
問題來了,怎麼獲取客戶端的clientid呢?
通過查詢資料,有一個api是getClientInfo方法,能夠獲取客戶端信息,可是必須條件編譯,由於是plus接口。
如下代碼,用戶登錄完成時,獲取客戶端信息(appid,appkey,clientid)、用戶信息(帳戶名、角色等)、其餘信息,向服務端提交api請求,保存客戶端clientid和角色的關聯信息。
// 保存clientid到服務器
// #ifdef APP-PLUS
const clientInfo = plus.push.getClientInfo()
let pushUser = {
clientid: clientInfo.clientid,
appid: clientInfo.appid,
appkey: clientInfo.appkey,
userName: '用戶名',
userRole: '用戶角色'
}
// 提交api請求,服務端保存客戶端角色信息
Vue.prototype.$minApi.savePushUser(pushUser)
// #endif
複製代碼
服務端接收到信息,根據本身的業務邏輯,保存或者更新,做者的處理邏輯時已經保存的clientid,不在新增,更新角色信息。
clientid和角色關係,數據庫表結構
改進消息發送方式,採用個推toList:簡稱「批量推」,指向制定的一批用戶推送消息
/** * @params pushMessage推送消息 * @params appPushList推送角色目標列表 */
public static void pushMessage(PushMessage pushMessage, List<AppPush> appPushList) {
IGtPush push = new IGtPush(url, appKey, masterSecret);
Style0 style = new Style0();
// STEP2:設置推送標題、推送內容
style.setTitle(pushMessage.getTitle());
style.setText(pushMessage.getContent());
// style.setLogo("push.png"); // 設置推送圖標
// STEP3:設置響鈴、震動等推送效果
style.setRing(true); // 設置響鈴
style.setVibrate(true); // 設置震動
// STEP4:選擇通知模板
NotificationTemplate template = new NotificationTemplate();
template.setAppId(appId);
template.setAppkey(appKey);
template.setStyle(style);
// 點擊消息打開應用
template.setTransmissionType(1);
// 傳遞自定義消息
template.setTransmissionContent(JSONUtil.toJsonStr(pushMessage));
// STEP5:定義"AppMessage"類型消息對象,設置推送消息有效期等推送參數
// 採用toList方案,定義ListMessage消息類型
// List<String> appIds = new ArrayList<String>();
// appIds.add(appId);
ListMessage message = new ListMessage();
message.setData(template);
// message.setAppIdList(appIds);
message.setOffline(true);
message.setOfflineExpireTime(1000 * 600); // 時間單位爲毫秒
String contentId = push.getContentId(message);
// 獲取推送目標
List targets = new ArrayList();
for (AppPush ap : appPushList) {
Target target = new Target();
target.setAppId(appId);
target.setClientId(ap.getClientid());
targets.add(target);
}
// STEP6:執行推送,不採用toApp方案,採用toList方案
// IPushResult ret = push.pushMessageToApp(message);
IPushResult ret = push.pushMessageToList(contentId, targets);
System.out.println(ret.getResponse().toString());
}
複製代碼
PushMessage類是一個model
public class PushMessage {
private String title;
private String content;
// 用戶角色
private String userRole;
// 其餘對象
// 省略,getter setter方法
}
複製代碼
AppPush類是數據庫表映射類
public class AppPush private String appid;//appid
private String appkey;//appkey
private String clientid;//clientid
private String userName;//帳戶
private String userRole;//用戶角色
// 其餘對象
// 省略,getter setter方法
}
複製代碼
在客戶端manifest.json文件中的sdkConfigs中添加以下配置,圖標本身添加
/* SDK配置 */
"sdkConfigs" : {
"push" : {
"unipush" : {
"icons": {
"push": {
"ldpi": "unpackage/res/icons/48x48.png",
"mdpi": "unpackage/res/icons/48x48.png",
"hdpi" : "unpackage/res/icons/72x72.png",
"xhdpi" : "unpackage/res/icons/96x96.png",
"xxhdpi" : "unpackage/res/icons/144x144.png",
"xxxhdpi" : "unpackage/res/icons/192x192.png"
},
"small": {
"ldpi": "unpackage/res/icons/18x18.png",
"mdpi": "unpackage/res/icons/24x24.png",
"hdpi": "unpackage/res/icons/36x36.png",
"xhdpi": "unpackage/res/icons/48x48.png",
"xxhdpi": "unpackage/res/icons/72x72.png"
}
}
}
}
},
複製代碼
轉載請註明:溜爸 » uni-app消息推送方案