es.davy.ai

Preguntas y respuestas de programación confiables

¿Tienes una pregunta?

Si tienes alguna pregunta, puedes hacerla a continuación o ingresar lo que estás buscando.

¿Cómo puedo agregar la opción “Reproducir sonidos para notificaciones” para mi aplicación en la configuración de “Notificaciones & Enfoque” de macOS?

Mi aplicación maneja correctamente las notificaciones con los siguientes bloques de código:

swift
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { _, _ in }

y:

swift
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    return completionHandler([.alert, .sound])
}

y:

UNUserNotificationCenter.current().getNotificationSettings { (settings) in
    if settings.authorizationStatus == .authorized {
        let content = UNMutableNotificationContent()
        let id = "MB"
        let trigger = UNCalendarNotificationTrigger(dateMatching: DateComponents(calendar: Calendar.current), repeats: false)

        if self.appModel.isNotifyingOnBreak {
            content.title = "A message:"
            content.body = self.appModel.notificationContent
        }

        content.sound = .default
        let request = UNNotificationRequest(identifier: id, content: content, trigger: trigger)

        UNUserNotificationCenter.current().add(request)
    }
}

Cuando miro mis aplicaciones en la configuración de “Notificaciones y Enfoque” de macOS, puedo ver mi aplicación en la barra lateral izquierda con “Sonidos, Alertas”. Sin embargo, en la sección derecha, no hay opción para activar o desactivar el sonido de las notificaciones.

¿Cómo puedo agregar la opción de “Reproducir sonido para las notificaciones”, como existe para la aplicación “Mensajes” y muchas otras aplicaciones?

Tags:  , , ,

Answer

  1. Avatar for davy.ai

    Para agregar la opción “Reproducir sonidos para notificaciones”, debes agregar un archivo de sonido al paquete de tu aplicación y especificarlo en el contenido de la notificación.

    Primero, agrega el archivo de sonido a tu proyecto arrastrándolo al navegador de proyectos de Xcode. Asegúrate de seleccionar “Copiar elementos si es necesario” y agregarlo al destino de tu aplicación.

    Luego, actualiza el contenido de tu notificación para incluir el sonido:

    if self.appModel.isNotifyingOnBreak {
        content.title = "Un mensaje:"
        content.body = self.appModel.notificationContent
        content.sound = UNNotificationSound(named: UNNotificationSoundName(rawValue: "nombre_tu_archivo_de_sonido.mp3"))
    } else {
        content.sound = .default // o cualquier otro sonido del sistema por defecto
    }
    

    Asegúrate de reemplazar “nombre_tu_archivo_de_sonido.mp3” con el nombre del archivo de sonido que agregaste a tu proyecto.

    Después de realizar estos cambios, la opción “Reproducir sonidos para notificaciones” debería aparecer en la configuración de Notificaciones y Enfoque para tu aplicación, y los usuarios podrán activar o desactivar el sonido.

Comments are closed.