
This video is only available to subscribers. Start a subscription today to get access to this and 472 other videos.
watchOS Notifications - Part 1
Episode
#256
|
16 minutes
| published on
February 16, 2017
| Uses watchOS-3, Xcode-8.2
Subscribers Only
In this episode Dory Glauberman covers how to set up notifications in your application on both the iPhone and Apple Watch using UNUserNotificationCenter. It highlights best practices for requesting notification authorization and demonstrates how to fire a sample notification for the Beer Button watch app.
This episode is part of a series: Up to Speed with watchOS.
1. Watch Connectivity 17 min |
2. Digital Crown 11 min |
3. The Dock 14 min |
4. watchOS Notifications - Part 1 16 min |
5. watchOS Notifications - Part 2 16 min |
This week's guest author is Dory Glauberman. Dory works for Mutual Mobile in Austin, TX. You can find Dory on Twitter.
Episode Links
Requesting Authorization for Notifications
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
if granted {
let category = UNNotificationCategory(identifier: "BeerButtonOrderDelivery", actions: [], intentIdentifiers: [], options: [])
center.setNotificationCategories(Set([category]))
} else {
print("No permission" + error.debugDescription)
}
}
Determining Authorization Status Without Prompting User
let center = UNUserNotificationCenter.current()
center.delegate = self
center.getNotificationSettings { (settings) in
if settings.authorizationStatus == .notDetermined {
// user has not seen apple notification authorization prompt
}
}
Handle Notifications Received While App is in Foreground
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
// this shows the notification, alternatively you could change app state based on the notification or ignore the notification
completionHandler([.alert, .sound])
}