나 함수를 만들었다. 원하는 때에 특정 시간에 기능을 호출 할 수 있습니다. 시계 응용 프로그램을 만들어서 사용자가 알람을 만들었을 때 로컬 알림을 트리거해야합니다. 알림 센터 위임 방법에서는 응답을 처리하고 원하는 모든 방법을 호출 할 수 있습니다.
class LocalNotificationMethod : NSObject {
static let notificationInstance = LocalNotificationMethod()
let requestIdentifier = "SampleRequest" //identifier is to cancel the notification request
internal func scheduleLocalNotification(titleOfNotification:String, subtitleOfNotification:String, messageOfNotification:String, soundOfNotification:String, dateOfNotification:String) {
if #available(iOS 10.0, *) {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd hh:mm a"
let date3 = formatter.date(from: dateOfNotification)
let content = UNMutableNotificationContent()
content.body = NSString.localizedUserNotificationString(forKey: titleOfNotification, arguments: nil)
content.sound = soundOfNotification.characters.count > 0 ? UNNotificationSound.init(named: soundOfNotification + ".mp3") : UNNotificationSound.default()
let trigger = UNCalendarNotificationTrigger.init(dateMatching: NSCalendar.current.dateComponents([.day, .month, .year, .hour, .minute], from: date3!), repeats: false)
let request = UNNotificationRequest(identifier:requestIdentifier, content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request){(error) in
if (error != nil){
print(error?.localizedDescription)
} else {
print("Successfully Done")
}
}
} else {
// Fallback on earlier versions
}
}
}
그리고 AppDelegate에 방법에
은 : - 당신은 당신의 통지 또는 때마다 알림이 작업을 완료하려면 무엇을 할 present.Is됩니다에 때마다 사용자가 클릭을 처리 할 수 있습니다.
// MARK : - 알림 대표
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) {
print("Tapped in notification")
}
//This is key callback to present notification while the app is in foreground
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print("Notification being triggered")
//You can either present alert ,sound or increase badge while the app is in foreground too with ios 10
//to distinguish between notifications
if notification.request.identifier == "SampleRequest" {
completionHandler([.alert,.sound,.badge])
}
}
당신은 귀하의 콘텐츠와 지역 알림을 예약 할 수 있습니다. – KKRocks
@KKRocks 아마도 내 질문에 대해 충분히 명확하지 않았습니다. 내가보기에 문제는 알림을 표시 할 때 현지 알림에 표시 할 콘텐츠를 가져오고 싶습니다.따라서 필자의 경우 사용자가 오전 7시에 알림을 예약 할 수 있으며 그 때 알림 내용을 가져오고 싶습니다. –
그렇다면 날씨 예보를 표시하려면 푸시 알림이 가장 좋습니다. – KKRocks