uniapp写app做测试手机通知栏展示内容

uniapp写app做测试手机通知栏展示内容

以下代码:只是个简单测试能不能给手机发送消息,能不能引导打开通知权限,能不能进行跳转的功能,

增加 notify.js 以下文件

复制代码
// 模拟本地通知功能
export function showNotification() {
  // 1. 检查通知权限(Android)
  // #ifdef APP-PLUS
  if (plus.os.name === "Android") {
    const main = plus.android.runtimeMainActivity();
    const NotificationManagerCompat = plus.android.importClass(
      "androidx.core.app.NotificationManagerCompat"
    );
    // 获取主Activity和包名
      // const main = plus.android.runtimeMainActivity();
      const pkName = main.getPackageName();
      
      // 检测权限状态
    const isEnabled = NotificationManagerCompat.from(main).areNotificationsEnabled();
      if (!isEnabled) {
        uni.showModal({
          title: "通知权限未开启",
          content: "请前往设置开启通知权限",
          confirmText: "去设置",
          success: () => {
            try {
              const Intent = plus.android.importClass("android.content.Intent");
              const Build = plus.android.importClass("android.os.Build");
              
              let intent = new Intent();
              if (Build.VERSION.SDK_INT >= 26) { // Android 8.0+
                intent.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
                intent.putExtra("android.provider.extra.APP_PACKAGE", pkName);
              } else { // 低版本通用方案
                const Settings = plus.android.importClass("android.provider.Settings");
                const Uri = plus.android.importClass("android.net.Uri");
                intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                intent.setData(Uri.fromParts("package", pkName, null));
              }
              main.startActivity(intent);
            } catch (e) {
              uni.showToast({ title: "跳转失败", icon: "none" });
              console.error(e);
            }
          }
        });
      }
  }
  // #endif

  // 2. 发送通知(固定内容)
  plus.push.createMessage(
    "这是固定通知内容", // 通知正文(死值)
    "系统通知",         // 通知标题(死值)
    {
      cover: true,      // 覆盖旧通知
      sound: "system",   // 使用系统提示音
	  payload: { path: "/pages/profile/index" }  // 携带跳转路径参数
    }
  );
}

增加点击事件 随便个地儿

复制代码
<button @click="triggerNotify">触发模拟通知</button>




import { showNotification } from './notify.js';



	const triggerNotify =()=> {
      showNotification();
      uni.showToast({ title: '已发送模拟通知' });
    }

这只是个简单测试能不能给手机发送消息,能不能引导打开通知页,能不能进行跳转的功能

相关推荐
云起SAAS1 小时前
私域直播系统UniApp源码 多商户商城+直播带货 微信小程序+H5+安卓iOS
android·微信小程序·uni-app·私域直播系统
专科3年的修炼2 天前
uni-app移动应用开发第四章
开发语言·javascript·uni-app
q5507071772 天前
uniapp/uniappx实现原生图片编辑涂鸦、贴图、滤镜、旋转、裁剪等
uni-app
计算机学姐3 天前
基于微信小程序的校园失物招领管理系统【uniapp+springboot+vue】
java·vue.js·spring boot·mysql·信息可视化·微信小程序·uni-app
2501_915921433 天前
HTTPS前端劫持 新一代流量劫持解决方案
前端·网络协议·ios·小程序·https·uni-app·iphone
爱怪笑的小杰杰3 天前
优化 UniApp 日历组件的多语言切换:告别 setLocale 引起的 App 重启
java·前端·uni-app
计算机学姐3 天前
基于微信小程序的宠物服务系统【uniapp+springboot+vue】
java·vue.js·spring boot·mysql·微信小程序·uni-app·宠物
2501_915909063 天前
iOS应用签名的三种方法全解析:从官方到第三方工具
android·ios·小程序·https·uni-app·iphone·webview
心中无石马4 天前
uniapp引入tailwindcss4.x
前端·css·uni-app
fix一个write十个4 天前
【uniApp开发】微信小程序 web-view 内嵌 H5 跳转支付踩坑实录
微信小程序·uni-app