첫째, 통지가 필요로하는 파일의 상단에 배치 :
import UserNotifications
그런 다음 다음과 같이 이제 알림을 요청 AppDelegate
let notifCenter = UNUserNotificationCenter.current()
let options: UNAuthorizationOptions = [.alert, .badge, .sound]
notifCenter.requestAuthorization(options: options, completionHandler: nil)
의 application(_:didFinishLaunchingWithOptions:)
이 장소 :
// timeInterval is in seconds, so 60*60*12*3 = 3 days, set repeats to true if you want to repeat the trigger
let requestTrigger = UNTimeIntervalNotificationTrigger(timeInterval: (60*60*12*3), repeats: false)
let requestContent = UNMutableNotificationContent()
requestContent.title = "Title" // insert your title
requestContent.subtitle = "Subtitle" // insert your subtitle
requestContent.body = "A body in notification." // insert your body
requestContent.badge = 1 // the number that appears next to your app
requestContent.sound = UNNotificationSound.default()
// Request the notification
let request = UNNotificationRequest(identifier "PutANameForTheNotificationHere", content: requestContent, trigger: requestTrigger
// Post the notification!
UNUserNotificationCenter.current().add(request) { error in
if let error = error {
// do something to the error
} else {
// posted successfully, do something like tell the user that notification was posted
}
이 코드 부분은 다음 세 가지를 수행합니다.
- 알림 컨텍스트
- 포스트 컨텍스트를 생성 트리거를 정의
당신이 통지와 함께 작업을 계속하려는 경우, 반응하는 방법에 대한 어떤 또한 회담, this tutorial 볼 때 사용자가 알림을 탭합니다.
참고 : UNMutableNotificationContext
가 변경 가능한 이름에도 불구하고, UN
그냥 네임 스페이스입니다. 혼동하지 마십시오! 당신은 자막을 제공하는 경우

, 그것은 제목과 본문 사이에있을 것입니다. 먼저 자습서 구글에서 검색을 시도해야

:
이것은
badge
속성입니다. 다음은 그 예입니다. https://code.tutsplus.com/tutorials/an-introduction-to-the-usernotifications-framework--cms-27250 – paper1111시도했지만 실제로 알아낼 수 없었습니다. 그것, 고마워! :) –
실제로 "경고 메시지 보내기"라고 말하면 [this] (http://i.imgur.com/h4ph9mk.jpg)와 같은 메시지를 보내시겠습니까? – paper1111