์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
- Flutter
- Kuring
- rxcocoa
- tableView
- node.js
- combine
- SnapKit
- swift
- ํ๋ก๊ทธ๋๋จธ์ค
- ๋ฐฑ์ค
- realm
- CollectionView
- RxSwift
- XCTest
- TCA
- reactorkit
- ios
- designpattern
- raywenderlich
- BOJ
- SwiftUI
- Lv2
- ํจ์คํธ์บ ํผ์ค
- BFS
- Swfit
- UIKit
- Xcode
- MVVM
- visionOS
- arkit
- Today
- Total
lgvv98
[iOS14] ๋ฐฑ๊ทธ๋ผ์ด๋์์ ํน์ ์๊ฐ ๋ก์ปฌ ์๋ฆผ๋ณด๋ด๊ธฐโฐ ๋ณธ๋ฌธ
[iOS14] ๋ฐฑ๊ทธ๋ผ์ด๋์์ ํน์ ์๊ฐ ๋ก์ปฌ ์๋ฆผ๋ณด๋ด๊ธฐโฐ
๐ฅ ์บ๋ฟ๋งจ 2021. 7. 29. 16:31โ ์ด๋ฒ์๊ฐ์๋ ํน์ ์๊ฐ์ ๋ก์ปฌ ์๋ฆผ์ ๋ณด๋ด๋ ๋ฐฉ๋ฒ์ ๋ํด์ ์์๋ณผ ์์ ์ด์ผ.
๋ก์ปฌ ์๋ฆผ์ด๋ ์ด๋ฐ ์์ผ๋ก ์๋ฆผ์ด ์จ ๊ฒ์ ๋งํ๋ค..!
๊ทธ๋ผ ์ด๋ป๊ฒ ์ฐ๋์ง ์์๋ณด๋๋ก ํด์ผ๊ฒ ์ง?
(๋ชฉ์ฐจ)
1. ๋ก์ปฌ ์๋ฆผ์ ์ด๋ค ์ฒ๋ฆฌ๊ณผ์ ์ ๊ฑฐ์น ๊น?
2. SceneDelegate์์ ๋ก์ปฌ ์๋ฆผ ๋ณด๋ด๊ธฐ
3. AppDelegate์์ ๊ถํ ์ป๊ธฐ
4. ํน์ ์๊ฐ ๋ง๊ณ ๋ค๋ฅธ trigger๋?
โ 1. ๋ก์ปฌ ์๋ฆผ์ ์ด๋ค ์ฒ๋ฆฌ๊ณผ์ ์ ๊ฑฐ์น ๊น?
- ๋ก์ปฌ ์๋ฆผ ๋ฐ์ก์ 4๋จ๊ณ -
1๏ธโฃ ๋ฐ์กํ ๋ด์ฉ์ ์ ์ UNMuatableNotificationContent
2๏ธโฃ ๋ฐ์ก ์กฐ๊ฑด์ ์ ์ UNTimeIntervalNotificationTrigger
3๏ธโฃ ์๋ฆผ ์์ฒญ์ ๋ง๋ฆ UNNotificationRequest
4๏ธโฃ ๋
ธํฐํผ์ผ์ด์
์ผํฐ์ ํด๋น ์์ฒญ์ ๋ฑ๋ก UNUserNotificationCenter
์์ ๋ค ๊ฐ์ง ๋จ๊ณ๋ก ๊ตฌ์ฑ ๋ผ.
์ฐ์ ๋ฉ์๋๋ค์ ์ด๋ฆ๋ง ์ด์ง ๋ณด๊ณ ๋ค์ ์ฝ๋๋ก ๋์ด๊ฐ ๋ณด์.
โ 2. SceneDelegate์์ ๋ก์ปฌ ์๋ฆผ ๋ณด๋ด๊ธฐ
func sceneWillResignActive(_ scene: UIScene) {
// Called when the scene will move from an active state to an inactive state.
// This may occur due to temporary interruptions (ex. an incoming phone call).
if #available(iOS 10.0, *) { // iOS ๋ฒ์ 10 ์ด์์์ ์๋
UNUserNotificationCenter.current().getNotificationSettings { settings in
if settings.authorizationStatus == UNAuthorizationStatus.authorized {
/*
๋ก์ปฌ ์๋ฆผ์ ๋ฐ์กํ ์ ์๋ ์ํ์ด๋ฉด
- ์ ์ ์ ๋์๋ฅผ ๊ตฌํ๋ค.
*/
let nContent = UNMutableNotificationContent() // ๋ก์ปฌ์๋ฆผ์ ๋ํ ์์ฑ ์ค์ ๊ฐ๋ฅ
nContent.title = "๐ฆ ์ค๋์ ์ฝ๋ก๋ ํํฉ ์๋ฆผโฐ"
nContent.subtitle = "์ด ํ์ง์ : \n ์ฐ๋ฆฌ์ง์ญ ํ์ง์ : "
nContent.body = "์ด ํ์ง์ : \n ์ฐ๋ฆฌ์ง์ญ ํ์ง์ : "
nContent.sound = UNNotificationSound.default
nContent.userInfo = ["name":"lgvv"]
var date = DateComponents()
date.hour = 15
date.minute = 52
// ์๋ฆผ ๋ฐ์ก ์กฐ๊ฑด ๊ฐ์ฒด
let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: false)
// ์๋ฆผ ์์ฒญ ๊ฐ์ฒด
let request = UNNotificationRequest(identifier: "wakeup", content: nContent, trigger: trigger)
// NotificationCenter์ ์ถ๊ฐ
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
} else {
NSLog("User not agree")
}
}
} else {
NSLog("User iOS Version lower than 13.0. please update your iOS version")
// iOS 9.0 ์ดํ์์๋ UILocalNotification ๊ฐ์ฒด๋ฅผ ํ์ฉํ๋ค.
}
}
์ฐ์ ์ฐ๋ฆฌ๊ฐ ๋ฐฑ๊ทธ๋ผ์ด๋๋ก ๋ค์ด๊ฐ์ ๋, ์ฝ๋๊ฐ ์๋ํ๊ฒ ๋ง๋ค ์์ ์ด๋ฏ๋ก, SceneDelegate ๋ถ๋ถ์์ ๋ฐฑ๊ทธ๋ผ์ด๋๋ก ๋์ด๊ฐ์ ๋์ ๋ฉ์๋ ๋ถ๋ถ์ ์์ฑํด ์ค ํ์๊ฐ ์์ด.
๋งจ ์์ if ์กฐ๊ฑด์ ์ฐ๋ฆฌ๊ฐ ๋ฐฑ๊ทธ๋ผ์ด๋์์ ์๋ฆผ ๋ณด๋ด๋ ๊ถํ์ ํ๋ฝํ๋์ง ํ์ง ์์๋์ง์ ๋ํด์ ๊ฒ์ฌํ๋ ๋ถ๋ถ์ด์ผ.
๊ถํ์ ์ฃผ์์ ๋๋ง ๋์๊ฐ๊ฒ๋!! -> ๊ถํ ์ค์ ์ AppDelegate ๋ถ๋ถ์ ์์ฑํ๋๋ก ํ๋ ๊ฒ์ด ์ฑ์ ์ฒ์ ์ค์นํ ๋, ์๋ํ๋ฏ๋ก ํจ์จ์ ์ด๋ค.
nContent ๋ถ๋ถ์ ์ฐ๋ฆฌ์ ์๋ฆผ์ ํ์๋ ๋ถ๋ถ๋ค
date ๋ณ์๋ ํน์ ์๊ฐ์ ์ง์ ํด์ฃผ๊ณ
trigger ๋ณ์๋ฅผ ํตํด ์ด๋ค ์กฐ๊ฑด์์ ๋ฐ์์ํฌ๊ฑด์ง ์ ์ํด์ค ๋ค์.
request ๋ณ์๋ฅผ ํตํด ์ฐ๋ฆฌ๊ฐ ๋ง๋ notification์ id์ content ๋ด์ฉ ๊ทธ๋ฆฌ๊ณ ๋ฐ์์กฐ๊ฑด์ ์ฃผ์ด์ ๊ฐ์ฒด๋ฅผ ๋ง๋ค์ด
๊ทธ๋ฆฌ๊ณ ๋ง์ง๋ง์ผ๋ก NotificationCenter์ ๋ฑ๋กํ๋ฉด ๋!
โ๏ธUNUserNotificationCenter ๋ ์ฑ๊ธํค ํจํด์ผ๋ก ์์ฑ๋์ด ์์ด์ ์ฐ๋ฆฌ๊ฐ ๋ง์๋๋ก ๊ฐ์ฒด๋ฅผ ์ ์ธํ๋ฉด ์๋๋ค.
๊ทธ๋ผ if๋ฌธ์ ํต๊ณผํ๊ธฐ ์ํด์ ๊ถํ์ ์ด๋ป๊ฒ ์ป๋์ง ํ๋ฒ ๋ณผ๊น?
โ 3. AppDelegate์์ ๊ถํ ์ป๊ธฐ
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
if #available(iOS 11.0, *) {
// ๊ฒฝ๊ณ ์ฐฝ ๋ฐฐ์ง ์ฌ์ด๋๋ฅผ ์ฌ์ฉํ๋ ์๋ฆผ ํ๊ฒฝ ์ ๋ณด๋ฅผ ์์ฑํ๊ณ , ์ฌ์ฉ์ ๋์์ฌ๋ถ ์ฐฝ์ ์คํ
let notiCenter = UNUserNotificationCenter.current()
notiCenter.requestAuthorization(options: [.alert, .badge, .sound]) { (didAllow, e) in }
notiCenter.delegate = self // ์ด ์ฝ๋๋ ์ฌ์ฉ์๊ฐ ์๋ฆผ์ ํด๋ฆญํ์ฌ ๋ค์ด์จ ์ด๋ฒคํธ๋ฅผ ์ ๋ฌ๋ฐ๊ธฐ ์ํ ๋ธ๋ฆฌ๊ฒ์ดํธ ํจํด๊ตฌ์กฐ
// ์ฆ, ์๋ฆผ ์ผํฐ์ ๊ด๋ จํ์ฌ ๋ญ๊ฐ ์ฌ๊ฑด์ด ๋ฐ์ํ๋ฉด ๋(์ฑ ๋ธ๋ฆฌ๊ฒ์ดํธ) ํํ
์๋ ค์ค ์ด๋ฐ ์๋ฏธ์
/* ํด๋ก์ ๋งค๊ฐ๋ณ์ ์ค๋ช
์ฌ์ฉ์๊ฐ ๋ฉ์์ง ์ฐฝ์ ์๋ฆผ ๋์ ์ฌ๋ถ๋ฅผ true / false
์ค๋ฅ ๋ฐ์์ ์ฌ์ฉํ๋ ์ค๋ฅ ๋ฐ์ ํ์
์ ๋งค๊ฐ๋ณ์ e
*/
} else {
// ๊ฒฝ๊ณ ์ฐฝ, ๋ฐฐ์ง, ์ฌ์ด๋๋ฅผ ์ฌ์ฉํ๋ ์๋ฆผ ํ๊ฒฝ ์ ๋ณด๋ฅผ ์์ฑํ๊ณ , ์ด๋ฅผ ์ ํ๋ฆฌ์ผ์ด์
์ ์ ์ฅ.
let setting = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
application.registerUserNotificationSettings(setting) // ์์ฑ๋ ์ ๋ณด ์ ํ๋ฆฌ์ผ์ด์
์ ๋ฑ๋ก
}
return true
}
/*
์ ์ ๋
ธํฐํผ์ผ์ด์
์ผํฐ๋ ํ๋กํ ์ฝ์ด๋ค. ๊ทธ๋์ ์์ ๋
ธํฐ์ผํฐ.๋๋ฆฌ๊ฒ์ดํธ ์
ํ ์ด ์ฝ๋๊ฐ ์์ด์ผ ์์ฑ๊ฐ๋ฅํ๋ค.
์ด ๋ฉ์๋๋ ์๋ฆผ ๋ฐฐ๋์ ํ์ ์ฌ๋ถ์ ๊ด๊ณ์์ด ์๋ฆผ์ด ๋์ฐฉํ๋ฉด, ํธ์ถ๋๋ค.
*/
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
if notification.request.identifier == "wakeup" { // ๋ก์ปฌ ์๋ฆผ ๋ฑ๋ก์ ์
๋ ฅํ ์๋ณ ์์ด๋๋ฅผ ์ฝ์ด์ค๋ ์์ฑ
let userInfo = notification.request.content.userInfo // ์ ์ ๊ฐ ์ปค์คํ
์ผ๋ก ์ ์ํ ์ ๋ณด๋ฅผ ์ฝ์ด์ค๋ ์ญํ
print(userInfo["name"]!) // ๋์
๋๋ฆฌ ๊ฐ์ ์ถ๋ ฅ
}
// ์๋ฆผ ๋ฐฐ๋ ๋์์ฃผ๊ธฐ
completionHandler([.alert, .badge, .sound])
}
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
/*
์์์์ ๋น์ทํ์ง๋ง, ์ด ๋ฉ์๋๋ ์ค์ ๋ก ์ฌ์ฉ์๊ฐ ์๋ฆผ ๋ฉ์์ง๋ฅผ ํด๋ฆญ ํ์ ๊ฒฝ์ฐ์ ์คํ๋๋ค๋ ์ฐจ์ด๋ฅผ ๊ฐ์ง.
์ด๋ ์๋ฆผ ๋ฉ์์ง์ ๋ํ ์ ๋ณด๋ ๋ชจ๋ ์ ๋ฉ์๋์ ๋๋ฒ์จฐ ์ธ์๊ฐ์ธ response ๋งค๊ฐ๋ณ์์ ๋ด๊ฒจ ์ ๋ฌ๋ฉ๋๋ค.
*/
if response.notification.request.identifier == "wakeup" {
let userInfo = response.notification.request.content.userInfo
print(userInfo["name"]!)
}
completionHandler()
}
์ฝ๋๋ฅผ ์ญ ์ฝ์ด๋ณด๋ฉด ์ฝ๊ฒ ์ดํดํ ์ ์์ ๊ฒ์ด๋ค.
์ฐ์ appDelegate์ชฝ์ ์ฝ๋๋ฅผ ์์ฑํ๊ณ , iOS11 ์ด์๊ณผ ๋ค๋ฅธ ๋ฒ์ ์์์ ์๋ํ๋ ๋ฐฉ์์ด ๋ฌ๋ผ์ ๋ถ๊ธฐํด์ ๊ถํ์ ์ป์ด์จ๋ค.
๊ทธ๋ฆฌ๊ณ ์๋์ willPresent์ didReceive ๋ ๊ฐ์ง๊ฐ ์๋๋ฐ ์ ์๋ ์๋ฆผ ๋ฐฐ๋๋ฅผ ๋์ฐ๋ ์ญํ , ํ์๋ ์๋ฆผ ๋ฐฐ๋๋ฅผ ํด๋ฆญํ์ ๋ ์ฒ๋ฆฌํ ์ด๋ฒคํธ๋ฅผ ๋ฃ์ด์ค๋ค. (์๋ฅผ ๋ค๋ฉด ๋ฐฐ๋ ํด๋ฆญ ์, ์ฑ์ ์์์ด ์๋ ๊ทธ ๊ณณ์ผ๋ก ๋ฐ๋ก ์ด๋ํ๊ฒ ์ค์ ํด์ค ์๋ ์๋ค.)
โ 4. ํน์ ์๊ฐ ๋ง๊ณ ๋ค๋ฅธ trigger๋?
๋ฌผ๋ก ํน์ ์๊ฐ์๋ง ๋ณด๋ด๋ ๊ฒ ๋ง๊ณ ๋ ๋ค์ํ ํธ๋ฆฌ๊ฑฐ๋ค์ด ์กด์ฌํ๋ค.
https://developer.apple.com/documentation/usernotifications
์ ํ ๊ณต์ ๋ฌธ์์ ๋ฐ๋ฅด๋ฉด
ํน์ ์๊ฐ์ด ์๋๋ผ ์ง์ญ, ํธ์ฌ, ์ผ์ ์๊ฐ ๋ฐ๋ณต ๋ฑ ๋ค์ํ ๊ฒ๋ค์ด ์กด์ฌํ๋๊น ์ ํ์ฉํด ๋ณด๋๋ก ํ์.
๊ณต์ ๋ฌธ์์ ๊ฐ๋ฉด ๋ ๋ค์ํ ์ ๋ณด๊ฐ ์์ด์ ์ฐธ๊ณ ํ๋ฉด ์ข๊ฒ ๋ค...!
'โ ๏ธ deprecated โ ๏ธ > ๐ท ์ฝ๋ก๋19์๋ฆผ์ฑ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[iOS14] Swift5 alertDialog ํจ์๋ก ๋ง๋ค์ด ์ฌ์ฉํ๊ธฐ (0) | 2021.08.04 |
---|---|
[iOS14] โ๏ธ SplashView๋ฅผ ํ์ฉํด Alert ๋ฐ์ํ๊ธฐ (0) | 2021.08.02 |
String HTMLTag ์ง์ฐ๊ธฐ (์ ๊ทํํ์) (1) | 2021.07.29 |
Swift5 ๋ค์ด๋ฒ ๊ฒ์ API ์ฌ์ฉํ๊ธฐ(๋ด์ค) (0) | 2021.07.26 |
Swift5 Open API - xml parsing (0) | 2021.07.21 |