Last active
November 1, 2021 21:20
-
-
Save aovestdipaperino/b7353a39b5e92568b0364a8e879d0f24 to your computer and use it in GitHub Desktop.
How to send discreet notifications
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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