Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mackankowski/0b2672b5f28301510923be024a32d147 to your computer and use it in GitHub Desktop.
Save mackankowski/0b2672b5f28301510923be024a32d147 to your computer and use it in GitHub Desktop.
User Notification Center (iOS, Swift)
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
}
}
}
// 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