debug: add detailed logging for browser notifications
- Log notification permission status - Log critical event detection - Log notification creation attempts - Log notification failures with error details - Help troubleshoot why notifications aren't showing
This commit is contained in:
@@ -213,6 +213,8 @@ pub fn provide_torrent_store() {
|
||||
global_stats.set(stats);
|
||||
}
|
||||
AppEvent::Notification(n) => {
|
||||
log::info!("📬 Received notification: {} - {}", n.level == shared::NotificationLevel::Success, n.message);
|
||||
|
||||
// Show toast notification
|
||||
show_toast_with_signal(notifications, n.level.clone(), n.message.clone());
|
||||
|
||||
@@ -225,6 +227,7 @@ pub fn provide_torrent_store() {
|
||||
|| n.level == shared::NotificationLevel::Error;
|
||||
|
||||
if is_critical {
|
||||
log::info!("🔴 Critical notification detected: {}", n.message);
|
||||
let title = match n.level {
|
||||
shared::NotificationLevel::Success => "✅ VibeTorrent",
|
||||
shared::NotificationLevel::Error => "❌ VibeTorrent",
|
||||
|
||||
@@ -34,7 +34,9 @@ pub async fn request_notification_permission() -> bool {
|
||||
/// Check if browser notifications are supported and permitted
|
||||
pub fn is_notification_supported() -> bool {
|
||||
let window = web_sys::window().expect("no global window");
|
||||
js_sys::Reflect::has(&window, &JsValue::from_str("Notification")).unwrap_or(false)
|
||||
let supported = js_sys::Reflect::has(&window, &JsValue::from_str("Notification")).unwrap_or(false);
|
||||
log::debug!("📢 Notification API supported: {}", supported);
|
||||
supported
|
||||
}
|
||||
|
||||
/// Get current notification permission status
|
||||
@@ -58,11 +60,15 @@ pub fn get_notification_permission() -> String {
|
||||
pub fn show_browser_notification(title: &str, body: &str, icon: Option<&str>) -> bool {
|
||||
// Check permission first
|
||||
let permission = get_notification_permission();
|
||||
log::info!("📢 Notification permission: {}", permission);
|
||||
|
||||
if permission != "granted" {
|
||||
log::warn!("Notification permission not granted: {}", permission);
|
||||
log::warn!("❌ Notification permission not granted: {}", permission);
|
||||
return false;
|
||||
}
|
||||
|
||||
log::info!("✅ Permission granted, creating notification: {}", title);
|
||||
|
||||
// Create notification options
|
||||
let opts = NotificationOptions::new();
|
||||
opts.set_body(body);
|
||||
@@ -72,20 +78,24 @@ pub fn show_browser_notification(title: &str, body: &str, icon: Option<&str>) ->
|
||||
opts.set_require_interaction(false);
|
||||
opts.set_silent(Some(false));
|
||||
|
||||
log::info!("🔧 Notification options created");
|
||||
|
||||
// Create and show notification
|
||||
match Notification::new_with_options(title, &opts) {
|
||||
Ok(notification) => {
|
||||
log::info!("Browser notification shown: {}", title);
|
||||
log::info!("🔔 Browser notification shown: {}", title);
|
||||
|
||||
let title_owned = title.to_string();
|
||||
// Auto-close after 5 seconds
|
||||
let _ = gloo_timers::callback::Timeout::new(5000, move || {
|
||||
log::info!("⏰ Notification auto-closed: {}", title_owned);
|
||||
notification.close();
|
||||
}).forget();
|
||||
|
||||
true
|
||||
}
|
||||
Err(e) => {
|
||||
log::error!("Failed to create notification: {:?}", e);
|
||||
log::error!("❌ Failed to create notification: {:?}", e);
|
||||
false
|
||||
}
|
||||
}
|
||||
@@ -93,6 +103,8 @@ pub fn show_browser_notification(title: &str, body: &str, icon: Option<&str>) ->
|
||||
|
||||
/// Show notification only if enabled in settings and permission granted
|
||||
pub fn show_notification_if_enabled(title: &str, body: &str) -> bool {
|
||||
log::info!("📬 Checking if notification should be shown: {}", title);
|
||||
|
||||
// Check localStorage for user preference
|
||||
let window = web_sys::window().expect("no global window");
|
||||
let storage = window.local_storage().ok().flatten();
|
||||
@@ -104,9 +116,17 @@ pub fn show_notification_if_enabled(title: &str, body: &str) -> bool {
|
||||
.flatten()
|
||||
.unwrap_or("true".to_string());
|
||||
|
||||
log::info!("💾 Browser notification enabled in settings: {}", enabled);
|
||||
|
||||
if enabled == "true" {
|
||||
return show_browser_notification(title, body, None);
|
||||
let result = show_browser_notification(title, body, None);
|
||||
log::info!("📬 Notification result: {}", if result { "✅ shown" } else { "❌ failed" });
|
||||
return result;
|
||||
} else {
|
||||
log::info!("📭 Browser notifications disabled in settings");
|
||||
}
|
||||
} else {
|
||||
log::warn!("⚠️ localStorage not available");
|
||||
}
|
||||
|
||||
false
|
||||
|
||||
Reference in New Issue
Block a user