tauri icon indicating copy to clipboard operation
tauri copied to clipboard

[bug] Notifications not working in MacOS

Open Kakamotobi opened this issue 2 years ago • 16 comments

Describe the bug

I'm using Tauri with Svelte on a MacOS. But I'm having trouble with the notification module.

All Tauri APIs are allowlisted in tauri.conf.json.

The code below runs and prints permissionGranted as true but the sendNotifications seem to have no effect.

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();

I also tried using the Notifications API in the Tauri console itself but nothing happens and the created Notification object has no properties.

Notification.permission; // "granted"
const n = new Notification("New Message", { body: "hello" }); // {} empty object

Reproduction

  1. npm create tauri-app and initiate with Svelte.
  2. cd into directory and npm install.
  3. Add in notifications logic to script.
  4. npm run tauri dev.
  5. Notifications are not sent.

Expected behavior

Notification sent to MacOS.

Platform and versions

Environment › OS: 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 toolchain: 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 › build-type: bundle › CSP: unset › distDir: ../public › devPath: http://localhost:8080/ › framework: Svelte › bundler: Rollup

Stack trace

No response

Additional context

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/[email protected]",
        "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
      }
    ]
  }
}

package.json

{
  "name": "tauri-notification-test",
  "version": "1.0.0",
  "private": true,
  "scripts": {
    "build": "rollup -c",
    "dev": "rollup -c -w",
    "start": "sirv public --no-clear",
    "tauri": "tauri"
  },
  "devDependencies": {
    "@rollup/plugin-commonjs": "^17.0.0",
    "@rollup/plugin-node-resolve": "^11.0.0",
    "@tauri-apps/cli": "^1.0.5",
    "rollup": "^2.3.4",
    "rollup-plugin-css-only": "^3.1.0",
    "rollup-plugin-livereload": "^2.0.0",
    "rollup-plugin-svelte": "^7.0.0",
    "rollup-plugin-terser": "^7.0.0",
    "svelte": "^3.0.0"
  },
  "dependencies": {
    "@tauri-apps/api": "^1.0.2",
    "sirv-cli": "^2.0.0"
  }
}

Kakamotobi avatar Aug 17 '22 10:08 Kakamotobi

I also tried cloning the tauri api example and tried sending the test notification but doesn't work.

I'm not sure but maybe it has something to do with the fact that I cannot find the folder (named by identifier) in Application Support like $HOME/Library/Application\ Support/com.your.bundleid/.tauri-settings.

I'm not getting any errors. It just doesn't do anything.

Kakamotobi avatar Aug 18 '22 17:08 Kakamotobi

The .tauri-settings file doesn't exist anymore, that one was for our own permissions prompt (which lead to duplicate prompts and was therefore removed)

FabianLars avatar Aug 18 '22 17:08 FabianLars

Oh ok. Do you have any idea what might be the problem?

Using either the Notification Web API or Tauri's Notification API, the permission is always "granted" for some reason, and requesting permission and sending notification don't do anything.

Kakamotobi avatar Aug 18 '22 17:08 Kakamotobi

Nope, the permissions/notifications on macos are generally quite wonky (in a tauri context), and for some reason it seems to be gotten worse in recent tauri versions. I don't have a mac so i can't really look into it and all the others currently work on mobile support to get people to alpha test asap.

FabianLars avatar Aug 18 '22 18:08 FabianLars

I see. I hope this issue also gets handled asap amidst your progressions with tauri. Thanks.

Kakamotobi avatar Aug 18 '22 18:08 Kakamotobi

This sounds like the most relevant upstream issue https://github.com/h4llow3En/mac-notification-sys/issues/33

FabianLars avatar Aug 18 '22 19:08 FabianLars

@Kakamotobi @FabianLars
After my attempts (env : MacOS 11.4 , Apple Silicon M1), I found that this API cannot work on dev mode, but the production environment behaves normally.

Locate the source code and find possible reasons is: if we have feature = "custom-protocol" then we use the app's identifier,otherwise use system's identifier(I'm not sure if that's right?) 图片

BUT the keypoint is: 图片

(attach the successful pictrue like this...)

图片

Moon1102 avatar Aug 19 '22 09:08 Moon1102

Oh! Nice finding!

Can you check in the system permissions settings if you disabled Notifications for the Terminal application?

FabianLars avatar Aug 19 '22 09:08 FabianLars

Sure. I'll try it right away

Moon1102 avatar Aug 19 '22 09:08 Moon1102

@FabianLars 图片 It seems that the notification can be triggered without opening the button. After installation, the permission button is closed by default, but the notification can pop up at the first time

Moon1102 avatar Aug 19 '22 09:08 Moon1102

@Moon1102 Nice. Thanks for letting me know. I just checked and yes, the notifications are working as expected in production.

Kakamotobi avatar Aug 19 '22 17:08 Kakamotobi

Notification doesn't show up for me on Mac recently even in production mode. Did anyone have the same problem?

HuakunShen avatar Nov 22 '23 07:11 HuakunShen

Yeah, in production mode it popped up with the permission window. But won't displayed any actual notifications.

yarrichar avatar Nov 23 '23 06:11 yarrichar

in production, the notifications are working. however, in dev mode, they are not. quite the bummer.

zilahir avatar Dec 10 '23 14:12 zilahir

In my case, notifications work in dev mode (though I haven't been able to figure out how to get sounds working) and they show up as coming from Terminal.

With prod builds, they seemingly didn't work at all but then I noticed that some of the notifications showed way later when a notification from another app arrived. This made me check the notification center and there I saw all of the notifications I sent. Meaning, they didn't show up on the screen, they went directly into the notification center. Without any DND/focus mode enabled.

I then went into the notification settings for the app and changed the style from Banners to Alerts, and that did make the notifications appear on the screen. They need to be manually dismissed, but at least they show up.

stancl avatar Jan 17 '24 13:01 stancl

I recently came across this issue on MacOS. It seems when working in dev mode, I'm unable to receive notifications, but once I've built a release or test in production with the proper permissions set, it all works as expected. A quick workaround for this is to run the application within a browser, which does limit some functionality. This is a good way to test notifications in a dev environment without needing to build, but obviously a bug nonetheless.

I assume this is because the notification channel is using the browser's permissions instead of the application's direct permissions, which are correctly configured in this scenario. Not sure how helpful this is, but I wanted to include it here to help with remediation in the future.

battleoverflow avatar Apr 23 '24 13:04 battleoverflow