Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug] No command register_listener found for plugin #8068

Closed
why0123 opened this issue Oct 20, 2023 · 3 comments
Closed

[bug] No command register_listener found for plugin #8068

why0123 opened this issue Oct 20, 2023 · 3 comments
Labels
status: needs triage This issue needs to triage, applied to new issues type: bug

Comments

@why0123
Copy link

why0123 commented Oct 20, 2023

Describe the bug

I create a plugin directly in a project on my own, I have a requirement that the plugin returns specified data to the front-end via a trigger. When using addPluginListener it reports an error No command register_listener found for plugin . I found that the naming in @TauriPlugin is registerListener, and I'm not sure if the listener registration fails because the naming is not converted.

addPluginListener code and TauriPlugin Code

And I made a plugin directly in the source project, I don't know if this has an effect or not.
Here is the code:

Rust:

pub fn init<R: Runtime>() -> TauriPlugin<R> {
    Builder::<R>::new("PluginName")
        .setup(|_app, _api| {
            #[cfg(target_os = "android")]
            {
                let fullscreen = _api
                    .register_android_plugin("com.example.app", "PluginName")
                    .unwrap();
                _app.manage(fullscreen);
            }

            Ok(())
        })
        .build()
}

Kotlin:

@TauriPlugin(
  permissions = [
    Permission(strings = [Manifest.permission.POST_NOTIFICATIONS], alias = "postNotification")
  ]
)
class PluginName(private val activity: Activity) : Plugin(activity) {
    trigger('xxx',JSObject())
}

TS:

// So far it's working fine after I changed register_listener to registerListener
async function addPluginListener(
  plugin: string,
  event: string,
  cb: (payload) => void
): Promise<PluginListener> {
  const handler = new Channel()
  handler.onmessage = cb
  return invoke(`plugin:${plugin}|registerListener`, { event, handler }).then(
    () => new PluginListener(plugin, event, handler.id)
  )
}

addPluginListener('PluginName', 'xxx', (msg) => {
    console.log(msg)
})

Reproduction

No response

Expected behavior

No response

Platform and versions

[✔] Environment
    - OS: Windows 10.0.22621 X64
    ✔ WebView2: 118.0.2088.46
    ✔ MSVC: Visual Studio Community 2022
    ✔ rustc: 1.70.0 (90c541806 2023-05-31)
    ✔ Cargo: 1.70.0 (ec8a8a0ca 2023-04-25)
    ✔ rustup: 1.26.0 (5af9b9484 2023-04-05)
    ✔ Rust toolchain: stable-x86_64-pc-windows-msvc (default)
    - node: 16.15.1
    - pnpm: 8.4.0
    - yarn: 1.22.19
    - npm: 8.11.0

[-] Packages
    - tauri [RUST]: 2.0.0-alpha.15
    - tauri-build [RUST]: 2.0.0-alpha.9
    - wry [RUST]: 0.33.1
    - tao [RUST]: 0.22.2
    - @tauri-apps/api [NPM]: 2.0.0-alpha.8
    - @tauri-apps/cli [NPM]: 2.0.0-alpha.15

[-] App
    - build-type: bundle
    - CSP: unset
    - distDir: ../dist
    - devPath: http://localhost:3000/
    - framework: React
    - bundler: Vite

Stack trace

No response

Additional context

No response

@why0123 why0123 added status: needs triage This issue needs to triage, applied to new issues type: bug labels Oct 20, 2023
@rtiagom
Copy link

rtiagom commented Oct 20, 2024

This is a serious bug and should be handled ASAP. The mobile plugin event system is completely unusable as of 2.0.4

@wtto00
Copy link

wtto00 commented Oct 25, 2024

There was an error with registerListener in the Android catlog:

2024-10-25 20:06:27.084 31276-31276 Tauri/Console           com.tauri.dev                        E  File: http://tauri.localhost/ - Line 0 - Msg: Uncaught (in promise) app-events.registerListener not allowed. Command not found

My code is:

addPluginListener("plugin-name", "eventName", eventCallback);

Additionally, the trigger in Android did not activate in the JavaScript.

My plugin and example project depend on @tauri-apps/[email protected], while the example project relies on @tauri-apps/[email protected].


However, when I changed the code to register_listener, the error no longer appeared. But the trigger in Android still wasn't activated in the JavaScript.

const handler = new Channel();
handler.onmessage = eventCallback;
invoke("plugin:plugin-name|register_listener", { event: "eventName", handler }).then(
  () => new PluginListener("plugin-name", "eventName", handler.id)
);

Result:

Sorry, It's my bad, I didn't configure some settings correctly.

  • I need to add const COMMANDS: &[&str] = &["registerListener"]; in the plugin's build.rs.
  • I need to add permissions = ["allow-registerListener"] in the plugin's permissions/default.toml.
  • I need to add permissions: ["plugin-name:default"] in the project's src-tauri/capabilities/default.json.

After completing the above steps, addPluginListener and registerListener are working normally.

@lafleurdeboum
Copy link

lafleurdeboum commented Oct 29, 2024

Very helpful description of your issue, thank you ! I used it to benchmark mine (roughly the same).

Side note, you don't need to reimplement addPluginListener, it's in the core typescript :

import { addPluginListener } from '@tauri-apps/api/core';

I can confirm it does the same thing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: needs triage This issue needs to triage, applied to new issues type: bug
Projects
None yet
Development

No branches or pull requests

4 participants