天天看點

uniapp 監聽通知欄消息插件(支援白名單、黑名單、過濾) Ba-NotifyListener

簡介(下載下傳位址)

Ba-NotifyListener 是一款實時監聽通知欄消息的uniapp插件。支援白名單、黑名單;支援監聽消息移除;支援自定義過濾條件(如短信驗證碼)等。

  • 支援監聽所有通知欄消息(包含id、ticker、标題、内容、時間等等内容)
  • 支援白名單(需要監聽的應用,設定後其他應用都不監聽)
  • 支援黑名單(不想監聽的應用,不設定白名單時有效)
  • 支援自定義内容過濾條件(如過濾短信驗證碼,預設過濾4~8位數字)

相關插件

應用消息通知插件(多種樣式,新增支援常駐通知模式) Ba-Notify(文檔)

應用未讀角标插件 Ba-Shortcut-Badge (文檔)

短信監聽(驗證碼) Ba-Sms(文檔)

截圖展示

uniapp 監聽通知欄消息插件(支援白名單、黑名單、過濾) Ba-NotifyListener

使用方法

script

中引入元件

script

中調用(示例參考,可根據自己業務和調用方法自行修改)

const listener = uni.requireNativePlugin('Ba-NotifyListener'); //本插件
	const notify = uni.requireNativePlugin('Ba-Notify'); //應用通知插件(https://ext.dcloud.net.cn/plugin?id=9231)
	export default {
		data() {
			return {
				whiteList: [ //白名單
					'com.ba.UniTemp',
					'com.tencent.mobileqq', //qq
					'com.tencent.mm', //微信
					'com.android.mms', //短信
					'com.android.incallui', //來電
				],
				blackList: [ //黑名單
					'com.ba.UniTemp',
				],
				msgList: []
			}
		},
		methods: {
			setListener(flag = 0) { //設定監聽
				let params = {};
				if (flag == 1) {
					params = {
						whiteList: this.whiteList,
					    //regex: "(\\d{4,8})",正規表達式,預設為比對4-8位的數字
					};
				} else if (flag == 2) {
					params = {
						blackList: this.blackList,
					    //regex: "(\\d{4,8})",正規表達式,預設為比對4-8位的數字
					};
				}
				listener.setListener(params, res => {
					console.log(res);
					if (res.ok && res.data) {
						if (res.data.listenType == 1) {
							this.msgList.push('收到消息:');
							this.msgList.push(JSON.stringify(res.data));
							if (res.data.result)
								this.msgList.push("解析結果:" + res.data.result);

						}
						if (res.data.listenType == 2) {
							this.msgList.push('移除消息:');
							this.msgList.push(JSON.stringify(res.data));
						}
					}
					uni.showToast({
						title: res.msg,
						icon: "none",
						duration: 3000
					})
				});
			},
			isHasPermission() { //是否開啟擷取通知權限
				listener.isHasPermission(res => {
					console.log(res);
					uni.showToast({
						title: res.ok ? (res.data && res.data.isHasPermission ? '已開啟' : '未開啟') : res.msg,
						icon: "none",
						duration: 3000
					})
				});
			},
			openPermission() { //跳轉到通知權限設定界面(僅判斷未打開時跳轉)
				listener.openPermission(res => {
					let msg = res.msg;
					if (res.data && res.data.isHasPermission) {
						msg = res.data.isHasPermission ? "權限打開" : "權限關閉";
					}
					uni.showToast({
						title: msg,
						icon: "none",
						duration: 3000
					})
				});
			},
			setPermission() { //跳轉到通知權限設定界面(開不開都跳轉)
				listener.setPermission(res => {
					console.log(res);
					let msg = res.msg;
					if (res.data && res.data.isHasPermission) {
						msg = res.data.isHasPermission ? "權限打開" : "權限關閉";
					}
					uni.showToast({
						title: msg,
						icon: "none",
						duration: 3000
					})
				});
			},
			sendNotify() {//用于測試通知
				notify.show({
						channelID: '0',
						channelName: '普通通知',
						ID: 1,
						notifyType: 0,
						ticker: 'Ticker',
						title: 'title',
						content: "【某某應用】驗證碼:708563。尊敬的使用者,您正在...,我們不會向您索要此驗證碼,切勿告知他人!",
						extend: "附加參數",
					},
					(res) => {
						console.log(res)
					});
			}
		}
	}		
           

api 清單

方法名 說明
setListener 設定監聽
isHasPermission 是否開啟擷取通知權限
openPermission 跳轉到通知權限設定界面(僅判斷未打開時跳轉)
setPermission 跳轉到通知權限設定界面(開不開都跳轉)

setListener 方法參數

設定監聽

屬性名 類型 必填 預設值 說明
whiteList Array false ‘’ 白名單
blackList Array false ‘’ 黑名單
regex String false ‘’ 正規表達式過濾短信内容,預設過濾4~8位數字驗證碼,如’(\d{4,8})’

監聽通知的參數

判斷 res.data 有内容(參照使用方法),監聽接收到的通知

屬性名 類型 說明
listenType Number 1 接收到通知 2 移除通知
packageName String 應用包名
id Number 通知 id
postTime long 時間(毫秒)
postTimeS String 時間(yyyy-MM-dd HH:mm:ss 格式)
tickerText String 通知 ticker,在頂部狀态欄中的提示資訊
title String 通知标題
content String 通知内容
result String 通知内容根據過濾規則,解析後的結果,如短信驗證碼
isOngoing Boolean 通知是否持續顯示,側滑不能删除
isClearable Boolean 點選通知,是否自動消失

isHasPermission、openPermission、setPermission方法 傳回參數

判斷 res.data 有内容(參照使用方法)

屬性名 類型 說明
isHasPermission Boolean 是否開啟擷取通知權限

系列插件

圖檔選擇插件 Ba-MediaPicker (文檔)

圖檔編輯插件 Ba-ImageEditor (文檔)

檔案選擇插件 Ba-FilePicker (文檔)

應用消息通知插件(多種樣式,新增支援常駐通知模式) Ba-Notify(文檔)

應用未讀角标插件 Ba-Shortcut-Badge (文檔)

應用開機自啟插件 Ba-Autoboot(文檔)

掃碼原生插件(毫秒級、支援多碼)Ba-Scanner-G(文檔)

掃碼原生插件 - 新(可任意自定義界面版本;支援連續掃碼;支援設定掃碼格式)Ba-Scanner(文檔)

動态修改狀态欄、導航欄背景色、字型顔色插件 Ba-AppBar(文檔)

原生sqlite本地資料庫管理 Ba-Sqlite(文檔)

安卓保活插件(采用多種主流技術) Ba-KeepAlive(文檔)

安卓快捷方式(桌面長按app圖示) Ba-Shortcut(文檔)

自定義圖檔水印(任意位置) Ba-Watermark(文檔)

最接近微信的圖檔壓縮插件 Ba-ImageCompressor(文檔)

視訊壓縮、視訊剪輯插件 Ba-VideoCompressor(文檔)

動态切換應用圖示、名稱(如新年、國慶等) Ba-ChangeIcon(文檔)

原生Toast彈窗提示(穿透所有界面、穿透原生;自定義顔色、圖示 ) Ba-Toast(文檔)

圖檔塗鴉、畫筆 Ba-ImagePaint(文檔)

pdf閱讀(手勢縮放、顯示頁數) Ba-Pdf(文檔)

聲音提示、震動提示、語音播報 Ba-Beep(文檔)

websocket原生服務(自動重連、心跳檢測) Ba-Websocket(文檔)

短信監聽(驗證碼) Ba-Sms(文檔)

智能安裝(自動更新) Ba-SmartUpgrade(文檔)

監聽系統廣播、自定義廣播 Ba-Broadcast(文檔)

監聽通知欄消息(支援白名單、黑名單、過濾) Ba-NotifyListener(文檔)

全局置灰、哀悼置灰(可動态、同時支援nvue、vue) Ba-Gray(文檔)

擷取裝置唯一辨別(OAID、AAID、IMEI等) Ba-IdCode(文檔)

繼續閱讀