You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Tauri应用中Notification模块无法发送通知的问题求助

Tauri + Svelte 通知模块无响应问题

我用Tauri和Svelte开发桌面应用,使用notification模块时遇到问题,已经在tauri.conf.json中开启了所有API权限。

问题现象

  1. 以下代码能正常执行,第10行打印的permissionGranted值为true,但sendNotification完全没效果:
const notify = async () => {
    let permissionGranted = await isPermissionGranted();
    if (!permissionGranted) {
        const permission = await requestPermission();
        permissionGranted = permission === "granted";
    }
    if (permissionGranted) {
        sendNotification("Tauri is awesome!");
        sendNotification({ title: "TAURI", body: "Tauri is awesome!" });
        console.log(permissionGranted);
    }
}

notify();
  1. 直接在控制台调用原生Notifications API也没反应,创建的Notification对象为空:
Notification.permission; // "granted"
const n = new Notification("New Message", { body: "hello" }); // {} 空对象

环境与版本信息

Environment
  › 系统:Mac OS 12.4.0 X64
  › Node.js: 17.2.0
  › npm: 8.13.2
  › pnpm: 6.11.0
  › yarn: 1.22.15
  › rustup: 1.25.1
  › rustc: 1.62.0
  › cargo: 1.62.0
  › Rust 工具链: stable-x86_64-apple-darwin 

Packages
  › @tauri-apps/cli [NPM]: 1.0.5
  › @tauri-apps/api [NPM]: 1.0.2
  › tauri [RUST]: 1.0.5,
  › tauri-build [RUST]: 1.0.4,
  › tao [RUST]: 0.12.2,
  › wry [RUST]: 0.19.0,

App
  › 构建类型: bundle
  › CSP: 未设置
  › distDir: ../public
  › devPath: http://localhost:8080/
  › 框架: Svelte
  › 打包工具: Rollup
  › wry [RUST]: 0.19.0,

tauri.conf.json 配置

{
  "$schema": "../node_modules/@tauri-apps/cli/schema.json",
  "build": {
    "beforeBuildCommand": "npm run build",
    "beforeDevCommand": "npm run dev",
    "devPath": "http://localhost:8080",
    "distDir": "../public"
  },
  "package": {
    "productName": "d",
    "version": "0.1.0"
  },
  "tauri": {
    "allowlist": {
      "all": true
    },
    "bundle": {
      "active": true,
      "category": "DeveloperTool",
      "copyright": "",
      "deb": {
        "depends": []
      },
      "externalBin": [],
      "icon": [
        "icons/32x32.png",
        "icons/128x128.png",
        "icons/128x128@2x.png",
        "icons/icon.icns",
        "icons/icon.ico"
      ],
      "identifier": "com.tauri.dev",
      "longDescription": "",
      "macOS": {
        "entitlements": null,
        "exceptionDomain": "",
        "frameworks": [],
        "providerShortName": null,
        "signingIdentity": null
      },
      "resources": [],
      "shortDescription": "",
      "targets": "all",
      "windows": {
        "certificateThumbprint": null,
        "digestAlgorithm": "sha256",
        "timestampUrl": ""
      }
    },
    "security": {
      "csp": null
    },
    "updater": {
      "active": false
    },
    "windows": [
      {
        "fullscreen": false,
        "height": 600,
        "resizable": true,
        "title": "d",
        "width": 800
      }
    ]
  }
}

解决方案

针对 macOS 上的这个问题,按以下步骤排查修复:

  1. 检查系统通知权限
    打开「系统设置」→「通知」,找到你的应用(名称为d),确保已开启允许通知,且横幅/提醒样式未设置为「无」。

  2. 统一Tauri依赖版本
    当前@tauri-apps/api(1.0.2)和@tauri-apps/cli(1.0.5)版本不一致,存在已知的通知模块bug。执行命令升级到最新稳定版:

    npm install @tauri-apps/cli@latest @tauri-apps/api@latest
    

    之后重新构建应用:

    npm run tauri build
    
  3. 确认API导入正确性
    确保导入的是Tauri提供的通知API,避免和浏览器原生API混淆:

    import { isPermissionGranted, requestPermission, sendNotification } from '@tauri-apps/api/notification';
    
  4. 添加macOS权限声明
    在tauri.conf.json的tauri.bundle.macOS字段下补充权限描述,让系统明确应用的通知需求:

    "macOS": {
      "entitlements": null,
      "exceptionDomain": "",
      "frameworks": [],
      "providerShortName": null,
      "signingIdentity": null,
      "infoPlist": {
        "NSUserNotificationUsageDescription": "需要发送通知提醒用户关键信息"
      }
    }
    
  5. 测试打包后的应用
    macOS对未签名的开发版应用通知权限有额外限制,建议打包成正式应用后再测试通知功能。


内容的提问来源于stack exchange,提问作者Kakamotobi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 22:18:29