나는 아직 아이폰 OS 10에서 알림을 사용하지 않은, 그래서 내가 나서서 학습으로 파악 나 자신을위한 경험, 이제 나는 당신에게 그것을 전달할 수 있습니다.
UILocalNotification
은 iOS 10에서 감가 상각되며 UserNotifications
프레임 워크로 대체되었습니다. 알림을 표시 할 수있는 사용자로부터 AppDelegate
GET 인증에서
및 UNUserNotificationCenterDelegate
으로 센터 대리자를 설정 :
현재
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let center = UNUserNotificationCenter.current()
center.delegate = self
let options: UNAuthorizationOptions = [.alert, .sound];
center.requestAuthorization(options: options) {
(granted, error) in
if !granted {
print("Something went wrong")
}
}
return true
}
사용자에게 알림 :
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
// Play sound and show alert to the user
completionHandler([.alert,.sound])
}
작업을 처리 :
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) {
// Determine the user action
switch response.actionIdentifier {
case UNNotificationDismissActionIdentifier:
print("Dismiss Action")
case UNNotificationDefaultActionIdentifier:
print("Default")
case "foo":
print("foo")
case "bar":
print("bar")
default:
print("Unknown action")
}
completionHandler()
}
원하는대로 수행하십시오. 앱의 모든 알림에 대한 모든 작업 및 카테고리를 설정합니다.
func setupActions() {
//create first action
let foo = UNNotificationAction(
identifier: "foo",
title: "foo"
)
//create second action
let bar = UNNotificationAction(
identifier: "bar",
title: "bar",
options: [.destructive]
)
//put the two actions into a category and give it an identifier
let cat = UNNotificationCategory(
identifier: "cat",
actions: [foo, bar],
intentIdentifiers: []
)
//add the category to the notification center
UNUserNotificationCenter.current().setNotificationCategories([cat])
}
그리고 마지막으로 실제 통지를 만드는 : 그들이 중심이 아닌 통지 자체에 할당하고 있기 때문에
func setupNotification() {
let content = UNMutableNotificationContent()
content.title = "Hello!"
content.body = "A message"
content.sound = UNNotificationSound.default()
//make sure to assign the correct category identifier
content.categoryIdentifier = "cat"
// Deliver the notification in five seconds.
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let request = UNNotificationRequest(identifier: "hello", content: content, trigger: trigger)
let center = UNUserNotificationCenter.current()
center.add(request) { (error : Error?) in
if let theError = error {
print("theError \(theError)")
}
}
}
이러한 기능을 활용하여 각 클래스에 import UserNotifications
해야합니다.
자세한 정보 : User Notifications documentation.
어쩌면 당신이 찾고있는 것 : http://www.appcoda.com/local-notifications-ios8/ – Frankie
+ Frankie 나는 그 웹 사이트를 시험해 보았고 많은 오류가 발생했습니다. – krish
질문이 의미가 없습니다. 로컬 알림을 예약하고 있는데 왜 remoteNotifications를 확인하고 있습니까? – Adeel