2
"나에게 나중에 알림"작업을 추가하여 로컬 알림 기능을 확장하고 싶습니다. 즉, 사용자가 "나중에 알림"버튼을 누르면 설정된 시간 후에 알림을 다시 표시하려고합니다."나중에 알림"기능
알림이 활성화되어 있는지 확인하고 알림 카테고리를 설정하고 위임 한 후 나중에 알림 기능을 사용하는 것으로 모든 앱이 올바르게 연결되어 있어도 나중에 알림 버튼을 누른 후 예약 된 알림이 표시되지 않습니다. 조금도.
설정 모든 업 (카테고리, 권한을 확인하는 설정)에 탭 처리
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) {(accepted, error) in
if !accepted {
print("Notification access denied.")
}
}
let action = UNNotificationAction(identifier: "remindLater", title: "Remind me later", options: [])
let category = UNNotificationCategory(identifier: "myCategory", actions: [action], intentIdentifiers: [], options: [])
UNUserNotificationCenter.current().setNotificationCategories([category])
return true
}
는 "나중에 알림"
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) {
if response.actionIdentifier == "remindLater" {
let newDate = Date(timeInterval: 10, since: Date())
scheduleNotification(at: newDate, withCompletionHandler: {
completionHandler()
})
}
}
로컬 알림 설정
실제 코드 :
func scheduleNotification(at date: Date) {
let calendar = Calendar(identifier: .gregorian)
let components = calendar.dateComponents(in: .current, from: date)
let newComponents = DateComponents(calendar: calendar, timeZone: .current, month: components.month, day: components.day, hour: components.hour, minute: components.minute)
let trigger = UNCalendarNotificationTrigger(dateMatching: newComponents, repeats: false)
let content = UNMutableNotificationContent()
content.title = "Tutorial Reminder"
content.body = "Just a reminder to read your tutorial over at appcoda.com!"
content.sound = UNNotificationSound.default()
content.categoryIdentifier = "myCategory"
let request = UNNotificationRequest(identifier: date.description, content: content, trigger: trigger)
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
UNUserNotificationCenter.current().add(request) {(error) in
if let error = error {
print("Uh oh! We had an error: \(error)")
}
}
}
을
내가 뭘 잘못하고 있니? I am using AppCoda iOS 10 user notifications guide. 및 this is the sample code.
예약 할 모든 알림에 고유 한 식별자를 사용해야하는 것이 문제입니다. 트리거 날짜 설명 –
Thanks @LeoDabus를 사용할 수 있습니다. 제안한 것과 같은 트리거 날짜 설명과 같은 고유 식별자를 사용하여 전혀 변경하지 않았습니다. 새로운 알림이 제공되지 않습니다. – Cesare
알림을 받으려면 앱을 닫아야합니다. –