Skip to content

Instantly share code, notes, and snippets.

@aovestdipaperino
Last active November 1, 2021 21:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aovestdipaperino/b7353a39b5e92568b0364a8e879d0f24 to your computer and use it in GitHub Desktop.
Save aovestdipaperino/b7353a39b5e92568b0364a8e879d0f24 to your computer and use it in GitHub Desktop.
How to send discreet notifications
// The author disclaims copyright to this source code. In place of
// a legal notice, here is a blessing:
//
// May you do good and not evil.
// May you find forgiveness for yourself and forgive others.
// May you share freely, never taking more than you give.
class YourViewController : UIViewController {
// add this method.
func sendDiscreetNotification(message: String) {
if (! UIAccessibility.isGuidedAccessEnabled) { return }
// Clear existing notifications from the stack.
let center = UNUserNotificationCenter.current()
center.removeAllDeliveredNotifications()
center.removePendingNotificationRequests(withIdentifiers: ["unique_id"])
// Create and show a new notification on an accessory.
let content = UNMutableNotificationContent()
content.body = message
content.subtitle = ""
content.title = "a title"
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 0.001, repeats: false)
let request = UNNotificationRequest(identifier: "unique_id", content: content, trigger: trigger)
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
}
extension YourViewController : UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert,.sound])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment