새싹 iOS 4주차 - 5

영 yyyng·2022년 7월 31일
0

Sesac

목록 보기
23/32


notificationCenter

//ViewController
class ViewController: UIViewController{
let notificationCenter = UNUserNotificationCenter.current()

	override func viewDidLoad() {
    	super.viewDidLoad()
        //호출.
		requestAuthorization()
    }
    //권한 허가 받을 옵션 채택
    func requestAuthorization(){
        let authorizationOptions = UNAuthorizationOptions(arrayLiteral: .badge, .sound, .alert)
        
        notificationCenter.requestAuthorization(options: authorizationOptions) { success, error in
            if success {
                self.sendNotification()
            }
        }
    }
    //노티 설정
    func sendNotification(){
        let notificationContent = UNMutableNotificationContent()
        notificationContent.sound = .default
        notificationContent.title = "noti title"
        notificationContent.subtitle = "noti subtitle"
        notificationContent.body = "noti body"
        notificationContent.badge = 1
        
        let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 60, repeats: true)

        let request = UNNotificationRequest(identifier: "1", content: notificationContent, trigger: trigger)
        
        notificationCenter.add(request)
        
    }

}
//AppDelegate
//UNUserNotificationCenterDelegate 델리게이트 채택.
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
//Launch스크린 이후 도착한 노티 삭제.
	func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    	UNUserNotificationCenter.current().removeAllDeliveredNotifications()
        UNUserNotificationCenter.current().delegate = self
        return true
    }
//포그라운드 상태에서도 노티 받기.
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        completionHandler([.badge, .sound, .banner, .list])
    }
}
//SceneDelegate
func sceneDidBecomeActive(_ scene: UIScene) {
//액티브됐을때 쌓인 뱃지 제거.
        UIApplication.shared.applicationIconBadgeNumber = 0
        
    }

sms 인증번호 받기

textfield.textContentType = .oneTimeCode

폰트변경

profile
yyyng2.github.io

0개의 댓글