Last active
February 8, 2022 15:48
-
-
Save mackankowski/0b2672b5f28301510923be024a32d147 to your computer and use it in GitHub Desktop.
User Notification Center (iOS, Swift)
This file contains hidden or 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
import HealthKit | |
import UserNotifications | |
import Combine | |
@main | |
class AppDelegate: UIResponder, UIApplicationDelegate { | |
unc application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { | |
// ask the user for notification permissions | |
let center = UNUserNotificationCenter.current() | |
center.requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in | |
if error != nil { | |
// handle the error here. | |
} | |
//you're autorized - do whatever you want | |
} | |
} | |
} |
This file contains hidden or 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
// call it whenever you're sure that notifications are authorized | |
func scheduleNotification() { | |
let center = UNUserNotificationCenter.current() | |
let content = UNMutableNotificationContent() | |
content.title = "Notification title" | |
content.body = "Notification body" | |
let intervalInSeconds = 10 | |
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: intervalInSeconds, repeats: false) | |
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger) | |
center.add(request) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment