- plus.push.createMessage()
- 因項(xiàng)目一直是運(yùn)行在內(nèi)網(wǎng),所以不支持使用uni-push等運(yùn)行在公網(wǎng)的第三方個(gè)推渠道。
那就只能使用plus.push.createMessage()
,示例代碼如下:
let content = "您有一條新的消息~";
let options = {
"cover" : false,
"when" : new Date(),
'title' : "通知消息"
};
let body = {
'id' : 'id',
'key': "key"
}
let payload = JSON.stringify(body);
plus.push.createMessage(content, payload, options);
參數(shù)解釋:
content: ( String 類型) 必選,消息顯示的內(nèi)容,在系統(tǒng)通知中心中顯示的文本內(nèi)容。
payload: ( String 類型 ) 可選,消息承載的數(shù)據(jù),可根據(jù)業(yè)務(wù)邏輯自定義數(shù)據(jù)格式。
option: ( MessageOptions ) 可選 創(chuàng)建消息的額外參數(shù),參考MessageOptions。
MessageOptions:JSON對(duì)象,獲客戶端創(chuàng)建本地消息的參數(shù):
① title: (String 類型 )推送消息的標(biāo)題。
② sound: (String 類型 )推送消息的提示音,顯示消息時(shí)的播放的提示音,可取值: “system”-表示使用系統(tǒng)通知提示音; “none”-表示不使用提示音; 默認(rèn)值為“system”。
③ cover: (Boolean 類型 )是否覆蓋上一次提示的消息??扇≈祎rue或false,true為覆蓋,false不覆蓋
④ when: (Date 類型 )消息上顯示的提示時(shí)間。
⑤ delay: (Number 類型 )提示消息延遲顯示的時(shí)間。當(dāng)設(shè)備接收到推送消息后,可不立即顯示,而是延遲一段時(shí)間顯示,延遲時(shí)間單位為s,默認(rèn)為0s,立即顯示。
- 實(shí)際開發(fā)過程中,使用MQTT或者WebSocket輪詢消息通知接口,拿到數(shù)據(jù),調(diào)用plus.push.createMessage()方法。
- plus.push.getAllMessage()
- 獲取所有信息
getAllMessage(){
return plus.push.getAllMessage();
}
- 獲取客戶端標(biāo)識(shí)
plus.push.getClientInfo()
- 捕獲點(diǎn)擊的通知欄消息內(nèi)容
plus.push.addEventListener( "click", function(msg){
console.log( "您點(diǎn)擊了: " + JSON.stringify(msg) );
// TODO...
}, false);
- 清空系統(tǒng)消息中心所有的推送消息。
plus.push.clear();
- 刪除系統(tǒng)消息中心指定的推送消息,可通過getAllMessage方法獲取所有的消息后進(jìn)行操作。
參數(shù):message: ( PushMessage ) 必填,要?jiǎng)h除的消息對(duì)象,可通過getAllMessage()方法來獲取消息。
plus.push.remove( message );
- 判斷客戶端是否開啟通知權(quán)限
enablPermission(){
let main = plus.android.runtimeMainActivity();
let pkName = main.getPackageName();
let uid = main.getApplicationInfo().plusGetAttribute("uid");
let NotificationManagerCompat = plus.android.importClass("android.support.v4.app.NotificationManagerCompat");
//android.support.v4升級(jí)為androidx
if (NotificationManagerCompat == null) {
NotificationManagerCompat = plus.android.importClass("androidx.core.app.NotificationManagerCompat");
}
let areNotificationsEnabled = NotificationManagerCompat.from(main).areNotificationsEnabled();
// 未開通‘允許通知'權(quán)限,則彈窗提醒開通,并點(diǎn)擊確認(rèn)后,跳轉(zhuǎn)到系統(tǒng)設(shè)置頁面進(jìn)行設(shè)置
if (!areNotificationsEnabled) {
uni.showModal({
title: '通知權(quán)限開啟提醒',
content: '您還沒有開啟通知權(quán)限,無法接受到消息通知,請(qǐng)前往設(shè)置!',
showCancel: false,
confirmText: '去設(shè)置',
success: function(res) {
if (res.confirm) {
let Intent = plus.android.importClass('android.content.Intent');
let Build = plus.android.importClass("android.os.Build");
//android 8.0引導(dǎo)
if (Build.VERSION.SDK_INT >= 26) {
let intent = new Intent('android.settings.APP_NOTIFICATION_SETTINGS');
intent.putExtra('android.provider.extra.APP_PACKAGE', pkName);
} else if (Build.VERSION.SDK_INT >= 21) { //android 5.0-7.0
let intent = new Intent('android.settings.APP_NOTIFICATION_SETTINGS');
intent.putExtra("app_package", pkName);
intent.putExtra("app_uid", uid);
} else { //(<21)其他--跳轉(zhuǎn)到該應(yīng)用管理的詳情頁
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
let uri = Uri.fromParts("package", mainActivity.getPackageName(), null);
intent.setData(uri);
}
// 跳轉(zhuǎn)到該應(yīng)用的系統(tǒng)通知設(shè)置頁
main.startActivity(intent);
}
}
});
}
}
- 通知消息的左上角及右邊圖標(biāo)跟隨App圖標(biāo)配置:
注意:該圖標(biāo)必須為透明,不然左上角會(huì)顯示一個(gè)黑塊。
示例圖片如下:
【補(bǔ)充】
若是不使用MQTT或者WebSocket等協(xié)議,只使用setInterval定時(shí)器來實(shí)現(xiàn)簡(jiǎn)單的消息在線通知的話,則可以參考如下示例:
-
- 登錄成功后,觸發(fā)獲取所有未讀消息接口
-
- 判斷是登錄時(shí)的請(qǐng)求,還是已經(jīng)登錄后使用setInterval定時(shí)器觸發(fā)的請(qǐng)求
-
- 如果是登錄時(shí)觸發(fā)的請(qǐng)求,則把獲取到的數(shù)據(jù)的length長(zhǎng)度存在本地或者vuex,便于使用setInterval定時(shí)器時(shí)進(jìn)行比對(duì),來更新數(shù)量,進(jìn)行差異的消息通知
示例代碼
如下(主要信息已用注釋方式標(biāo)出):文章來源:http://www.zghlxwxcb.cn/news/detail-555334.html
// 獲取消息列表,剛進(jìn)頁面時(shí),在鉤子內(nèi)觸發(fā)
getAllNotice() {
this.$Message.unread().then(({data}) => { // 請(qǐng)求未讀信息接口,供參考
this.messageData = data.data; // this.messageData接收獲取到的數(shù)據(jù)
if(uni.getStorageSync('unreadMsgQuantity')) { // 判斷是否已經(jīng)在本地存了未讀數(shù)量(非第一次請(qǐng)求時(shí)才會(huì)走這步)
if(uni.getStorageSync('unreadMsgQuantity') < this.messageData.length) { // 如果本地存的數(shù)據(jù)小于獲取到的數(shù)據(jù),說明接口有更新多的內(nèi)容
let showMsg = this.messageData.slice(0, this.messageData.length - uni.getStorageSync('unreadMsgQuantity')) // 只彈出多出的也就是新增的消息
if(showMsg.length > 0) {
showMsg.forEach((ele)=>{ // 在安卓通知欄進(jìn)行消息通知
let body = {
'rowId' : ele.rowId,
}
let payload = JSON.stringify(body);
let options = {
"cover" : false, // true:覆蓋上次,只保留最后一個(gè)
"when" : new Date(),
'title' : "通知消息"
};
// 發(fā)送消息通知
plus.push.createMessage(ele.content, payload, options);
})
}
}
// 在本地更新最新的數(shù)量
uni.setStorageSync('unreadMsgQuantity', this.messageData.length);
} else { // 第一次登陸時(shí)走的這步
if(this.messageData.length > 0) {
uni.setStorageSync('unreadMsgQuantity', this.messageData.length); // 獲取到所有的未讀信息進(jìn)行存儲(chǔ)
this.messageData.forEach((ele)=>{ // 在安卓通知欄進(jìn)行消息通知
let body = {
'rowId' : ele.rowId,
}
let payload = JSON.stringify(body);
let options = {
"cover" : false, // true:覆蓋上次,只保留最后一個(gè)
"when" : new Date(),
'title' : "通知消息"
};
// 發(fā)送消息通知
plus.push.createMessage(ele.content, payload, options);
})
}
}
this.getSetIntervalData(); // 觸發(fā)定時(shí)器
});
},
// 定時(shí)器函數(shù)
getSetIntervalData() {
setInterval(this.getAllNotice, 30 * 1000);
},
// 最后記得清除定時(shí)器(clearInterval),清除本地存儲(chǔ)(uni.removeStorageSync('unreadMsgQuantity'))
-
- 點(diǎn)擊通知欄消息進(jìn)入到對(duì)應(yīng)頁面
示例代碼
如下(主要信息已用注釋方式標(biāo)出):文章來源地址http://www.zghlxwxcb.cn/news/detail-555334.html
// 安卓通知欄提醒顯示,可放在app.vue--methods里
androidNoticeBar() {
// 捕獲點(diǎn)擊的通知欄消息內(nèi)容
plus.push.addEventListener( "click", function(msg){
const page=getCurrentPages()
let nowPage=page[page.length - 1].route // 獲取點(diǎn)擊消息時(shí),當(dāng)前所在的頁面
if(nowPage.indexOf('notice-page')>-1){ // 已經(jīng)在消息頁面時(shí),禁止跳轉(zhuǎn)
return;
}
if(uni.getStorageSync('token')) {
uni.navigateTo({ // 登陸成功后,點(diǎn)擊消息跳轉(zhuǎn)到消息列表頁
url:'../notice-page/index'
});
} else {
console.log('token缺失'); // 跳轉(zhuǎn)到登錄頁
}
}, false);
},
到了這里,關(guān)于uni-app使用plus本地推送通知欄信息,不使用第三方個(gè)推實(shí)現(xiàn)消息在線統(tǒng)一推送、消息通知(MQTT、WebSocket、setInterval定時(shí)器)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!