精易论坛

标题: uniapp监测手机通知栏 nvue组件 [打印本页]

作者: 浅浅`    时间: 2021-9-28 10:17
标题: uniapp监测手机通知栏 nvue组件
功能说明
自动监听通知栏推送消息,返回消息标题内容接收时间包名
可以实现`拦截功能`,但是为了防止滥用此功能。插件暂未开通此功能,如果需要请联系我。

经实测试:
1、插件无法监听到短信的内容,短信只能获取到通知的titile,这个问题在下个版本修复。
2、华为mate20机型(华为其他机型暂未发现)闪退问题,其他主流机型没有发现这个问题,打包的时候各开发者把支持的cpu类型全部勾选或离线打包。

新版本 问题1、2已经修复
插件方法
方法名
返回值
说明

init()
初始化方法

readNotificationBar()
值布尔
是否获取通知栏权限 true已经获取 false未获取 1.0.2版本已改为同步

toSetting()
跳转到设置界面

getNotification()
json对象
开始监听通知栏内容 回调方法可以获取到监听到的内容 只需要调用一次即可

isIgnoringBatteryOptimizations
值布尔
是否开启白名单:true已开启 false未开启

requestIgnoreBatteryOptimizations
去设置白名单

cancelAll()
清空通知栏消息(包括其他apk发送的消息)2.3版本增加

代码演示
  1. <template>
  2.     <view>
  3.         {{msg}}
  4.         <view>
  5.             <view v-for="(item, index) in list" :key="index">
  6.               <text>监听到内容:{{item.content}}</text><br><text>监听到标题:{{item.title}}</text>
  7.               <br /><hr />
  8.             </view>
  9.           </view>
  10.         <button type="default" @click="init">初始化</button>
  11.         <button type="default" @click="test">是否开启获取通知权限</button>
  12.         <button type="default" @click="set">跳转到设置界面</button>
  13.         <button type="default" @click="start">开始监听</button>
  14.         <button type="default" @click="clear">清空列表记录</button>
  15.         <button type="default" @click="cancelAll">清空所有通知栏消息</button>
  16.         <button type="default" @click="isIgnoringBatteryOptimizations">是否设置白名单</button>
  17.         <button type="default" @click="requestIgnoreBatteryOptimizations">去设置白名单</button>
  18.     </view>
  19. </template>

  20. <script>
  21.     const NoticeBarModule = uni.requireNativePlugin('lu-NoticeBarModule');

  22.     export default {
  23.         data() {
  24.             return {
  25.                 msg: '收到的通知内容会展示在这里',
  26.                 list: []
  27.             }
  28.         },
  29.         onLoad() {},
  30.         methods: {
  31.             init() {
  32.                 //初始化 <-- 需要初始化一下 然后调用一次start方法即可  多次调用也无所谓不影响。
  33.                 NoticeBarModule.init();
  34.             },
  35.             clear() {
  36.                 this.list = [];
  37.             },
  38.             test() {
  39.                 //var res = NoticeBarModule.readNotificationBar(e => {
  40.                 //  this.msg = JSON.stringify(e)
  41.                 //  uni.showToast({
  42.                 //      title: JSON.stringify(e),
  43.                 //      icon: 'none'
  44.                 //  });
  45.                 //});

  46.                 //新版本改为同步
  47.                 //TODO: 是否开启获取通知栏内容权限已改成同步返回。 1.0.2版本
  48.                 let res = NoticeBarModule.readNotificationBar();
  49.                 if (res) {
  50.                     //开启
  51.                     uni.showToast({
  52.                         title: '开启',
  53.                         icon: 'none'
  54.                     });
  55.                 } else {
  56.                     //未开启
  57.                     uni.showToast({
  58.                         title: '未开启',
  59.                         icon: 'none'
  60.                     });
  61.                 }
  62.             },
  63.             set() {
  64.                 NoticeBarModule.toSetting();
  65.             },
  66.             cancelAll() {
  67.                 NoticeBarModule.cancelAll(); //无任何返回值
  68.             },
  69.             start() {
  70.                 let _this = this;
  71.                 NoticeBarModule.getNotification(e => { //<-- e 就是监听到的通知栏消息
  72.                     _this.list.push(e);
  73.                     console.log(JSON.stringify(e));
  74.                     uni.showToast({
  75.                         title: JSON.stringify(e),
  76.                         icon: 'none'
  77.                     });
  78.                 });
  79.             },
  80.             isIgnoringBatteryOptimizations() {
  81.                 let res = NoticeBarModule.isIgnoringBatteryOptimizations();
  82.                 if (res) {
  83.                     //开启
  84.                     uni.showToast({
  85.                         title: '已开启白名单',
  86.                         icon: 'none'
  87.                     });
  88.                 } else {
  89.                     //未开启
  90.                     uni.showToast({
  91.                         title: '未开启白名单',
  92.                         icon: 'none'
  93.                     });
  94.                 }
  95.             },
  96.             requestIgnoreBatteryOptimizations() {
  97.                 //申请加入白名单 没有任何回调
  98.                 NoticeBarModule.requestIgnoreBatteryOptimizations()
  99.             }

  100.         }
  101.     }
  102. </script>

  103. <style>
  104. </style>
复制代码
使用本插件的成品项目:
码支fuapp源码 - 监控通知栏
https://125.confly.eu.org/forum.php?mod=viewthread&tid=14698091
(出处: 精易论坛)

插件下载


作者: 金金金    时间: 2021-9-30 18:07
谢谢分享
作者: w794631290    时间: 2021-10-3 17:20
54353453567567567




欢迎光临 精易论坛 (https://125.confly.eu.org/) Powered by Discuz! X3.4