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: '已发送模拟通知' });
    }

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

相关推荐
吴传逞1 天前
记一次uniapp微信小程序开发scss变量失效的问题
微信小程序·uni-app·scss
2501_915921432 天前
小团队如何高效完成 uni-app iOS 上架,从分工到工具组合的实战经验
android·ios·小程序·uni-app·cocoa·iphone·webview
2501_916008892 天前
uni-app iOS 文件管理与 itools 配合实战,多工具协作的完整流程
android·ios·小程序·https·uni-app·iphone·webview
!win !2 天前
uni-app项目Tabbar实现切换icon动效
小程序·uni-app
xw52 天前
uni-app项目Tabbar实现切换icon动效
前端·uni-app
2501_916007472 天前
uni-app iOS 文件调试常见问题与解决方案:结合 itools、克魔、iMazing 的实战经验
android·ios·小程序·https·uni-app·iphone·webview
豆豆(设计前端)2 天前
使用 Uni-app 打包 外链地址APK 及 iOS 注意事项
ios·uni-app
jingling5552 天前
uniapp | 解决组件样式不生效问题
前端·css·uni-app·html·学习方法
^Rocky2 天前
微信小程序(uniapp)实现连接蓝牙
微信小程序·uni-app·蓝牙连接
2501_915918412 天前
uni-app 项目 iOS 上架踩坑经验总结 从证书到审核的避坑指南
android·ios·小程序·https·uni-app·iphone·webview