Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class Notification {
var visibility: Int? = null
var number: Int? = null
var silent: Boolean? = null
var onlyAlertOnce: Boolean? = null

fun getSound(context: Context, defaultSound: Int): String? {
var soundPath: String? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ class TauriNotificationManager(
}

private fun trigger(notificationManager: NotificationManagerCompat, notification: Notification): Int {
dismissVisibleNotification(notification.id)
cancelTimerForNotification(notification.id)
buildNotification(notificationManager, notification)

Expand Down Expand Up @@ -203,7 +202,7 @@ class TauriNotificationManager(
}
}
mBuilder.setVisibility(notification.visibility ?: NotificationCompat.VISIBILITY_PRIVATE)
mBuilder.setOnlyAlertOnce(true)
mBuilder.setOnlyAlertOnce(notification.onlyAlertOnce ?: true)
mBuilder.setSmallIcon(notification.getSmallIcon(context, getDefaultSmallIcon(context)))
mBuilder.setLargeIcon(notification.getLargeIcon(context))
val iconColor = notification.getIconColor(config?.iconColor ?: "")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ object UnifiedPushNotifier {
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setOnlyAlertOnce(true)
.setOnlyAlertOnce(silent)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setGroup(GROUP_KEY)
.setContentIntent(
Expand Down
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,13 @@ impl<R: Runtime> NotificationsBuilder<R> {
self.data.silent = true;
self
}

/// Whether replacing a visible notification alerts again. Android only, `true` when unset.
#[must_use]
pub const fn only_alert_once(mut self, only_alert_once: bool) -> Self {
self.data.only_alert_once = Some(only_alert_once);
self
}
}

/// Extensions to [`tauri::App`], [`tauri::AppHandle`], [`tauri::WebviewWindow`], [`tauri::Webview`] and [`tauri::Window`] to access the notification APIs.
Expand Down
13 changes: 13 additions & 0 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ pub struct NotificationData {
pub(crate) auto_cancel: bool,
#[serde(default)]
pub(crate) silent: bool,
pub(crate) only_alert_once: Option<bool>,
}

fn default_id() -> i32 {
Expand Down Expand Up @@ -284,6 +285,7 @@ impl Default for NotificationData {
ongoing: false,
auto_cancel: false,
silent: false,
only_alert_once: None,
}
}
}
Expand Down Expand Up @@ -806,6 +808,17 @@ mod tests {
assert!(data.extra.is_empty());
}

#[test]
fn test_only_alert_once_uses_the_android_key() {
let data = NotificationData {
only_alert_once: Some(false),
..Default::default()
};
let json = serde_json::to_string(&data).expect("Failed to serialize notification data");
assert!(json.contains("\"onlyAlertOnce\":false"));
assert!(NotificationData::default().only_alert_once.is_none());
}

#[test]
fn test_notification_data_serialization() {
let data = NotificationData {
Expand Down
Loading