Closed tm1000 closed 1 year ago
Hey @tm1000, I am not still sure yet. I felt the neccessity of notification (not only in Discord but also with Whatsapp), so added in the Roadmap.
I will try to explore the solution in my free times, if you find any: then please forward those here. That will be great.
@hamza72x
Ok I actually figured out a way to do it but you'll have to expose the IPC interface which right now you don't expose. Might need to have an additional flag to expose this. External urls dont expose it outright but you can trick Tauri into exposing it. (see: https://github.com/tauri-apps/tauri/issues/5088)
To expose IPC change (you dont have this in your code because you use WebViewBuilder from wry but theres probably a similar way to do it):
tauri::WindowUrl::External("https://www.bennish.net/web-notifications.html".parse().unwrap())
Into:
tauri::WindowUrl::App("https://www.bennish.net/web-notifications.html".into())
Now there is a valid IPC channel
Here's how I'm doing it from create-tauri-app (and looking at some of your code here). I'm not saying any of this is perfect but it does work by overloading Notification itself with a custome class that interfaces with the IPC channel. This bypasses the "denied" messages I was getting on remote pages. Notifications work after this
Hopefully the below code will help you out.
main.rs
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
#[tauri::command]
fn greet(name: &str) -> String {
format!("Hello, {}! You've been greeted from Rust!", name)
}
fn main() {
tauri::Builder::default()
.setup(|app| {
const INIT_SCRIPT: &str = r#"
function uid() {
return window.crypto.getRandomValues(new Uint32Array(1))[0];
}
function transformCallback(callback, once = false) {
const identifier = uid();
const prop = `_${identifier}`;
Object.defineProperty(window, prop, {
value: (result) => {
if (once) {
Reflect.deleteProperty(window, prop);
}
return callback === null || callback === void 0 ? void 0 : callback(result);
},
writable: false,
configurable: true
});
return identifier;
}
async function invoke(cmd, args = {}) {
return new Promise((resolve, reject) => {
const callback = transformCallback((e) => {
resolve(e);
Reflect.deleteProperty(window, `_${error}`);
}, true);
const error = transformCallback((e) => {
reject(e);
Reflect.deleteProperty(window, `_${callback}`);
}, true);
window.__TAURI_IPC__(Object.assign({ cmd,
callback,
error }, args));
});
}
window.__TAURI_IPC__ = (message) => window.ipc.postMessage(JSON.stringify(message))
Notification = class Notification {
static permission = "default"
constructor(title, options = {}) {
invoke(
'tauri',
{
__tauriModule: 'Notification',
message: {
cmd: 'notification',
options: {
title: title,
...options
}
}
}
)
}
static async requestPermission() {
const response = invoke(
'tauri',
{
__tauriModule: 'Notification',
message: {
cmd: 'requestNotificationPermission'
}
}
)
Notification.permission = response;
return response;
}
}
setTimeout(async () => {
const response = await invoke(
'tauri',
{
__tauriModule: 'Notification',
message: {
cmd: 'isNotificationPermissionGranted'
}
}
)
if(response) {
Notification.permission = "granted"
} else {
Notification.permission = "denied"
}
}, 1000)
"#;
let _local_window = tauri::WindowBuilder::new(
app,
"local",
tauri::WindowUrl::App("https://www.bennish.net/web-notifications.html".into())
).initialization_script(INIT_SCRIPT).title("local").build()?;
Ok(())
})
.invoke_handler(tauri::generate_handler![greet])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}
Thank you @tm1000, I will take a look and update you later insha'allah and maybe will just implement if I can manage to make it work.
Alternatively I was going to perhaps write a bug report against Tauri itself because webrtc works correctly when using external urls but again notifications doesn't without the above hack
@tm1000 added your code with https://github.com/hamza72x/web2app/pull/21, WhatsApp notification is working fine, but not Discord. Have to check further.
I noticed you added
Desktop Notification (useful for Discord like app)
I've been trying to get this to work with Tauri itself on remote URLs and I've had no success. I continue to get denied. When I run https://www.bennish.net/web-notifications.html through your app it fails with the same reasoning.
In a regular Tauri app notifications work if serving through localhost. So its confusing they dont work when handed a remote url (specifically example/api works)
Was wondering your thoughts on this? Or if there's anything I can do to help