iOS
[iOS] ํธ์์๋ฆผ ๋ฐ์์ ์ฒ๋ฆฌํ๊ธฐ
yangsubinn
2022. 10. 12. 16:21
ํธ์์๋ฆผ์ ํตํด ์ฑ์ ๋ค์ด์ค๋ ๊ฒฝ์ฐ๋ ํฌ๊ฒ ๋๊ฐ์ง๋ก ๋๋ ์ ์์ด๋๋ค
- ์ฑ์ด ์์ ํ ์ข ๋ฃ๋ ์ํ
- inactive/active/background ์ํ
1๏ธโฃ ์ฑ์ด ์์ ํ ์ข ๋ฃ๋ ์ํ์์ ํธ์์๋ฆผ์ ํตํด ์ฑ์ ๋ค์ด์ค๋ ๊ฒฝ์ฐ
SceneDelegate์ willConnectTo์์ ์ฒ๋ฆฌ
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
...
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
...
// ์ฑ ์ข
๋ฃ ์ํ์์ ํธ์์๋ฆผ์ ํตํด ์ฑ์ ์ ์ํ๋ ๊ฒฝ์ฐ
if let notification = connectionOptions.notificationResponse {
// notification์ ํฌํจ๋์ด ์ ๋ฌ๋๋ content๋ ์๋์ ๊ฐ์ด ์ ๊ทผํ ์ ์์ต๋๋ค
let content = notification.notification.request.content
// dictionary ํํ๋ก, userInfo["key๊ฐ"] ์ผ๋ก ์ ๊ทผ ๊ฐ๋ฅ
let userInfo = content.userInfo
guard let recordId: String = userInfo["recordId"] as? String else { return }
// ์ํ๋ ๋ทฐ๋ก ์ ํ
}
}
...
}
2๏ธโฃ ์ฑ์ด ์์ ์ข ๋ฃ๋์ง ์์ ์ํ์์ ํธ์์๋ฆผ์ ํตํด ์ฑ์ ๋ค์ด์ค๋ ๊ฒฝ์ฐ (background / inactive / active)
์ด๋ฐ ์ํ์์ ์ฑ์ ์ ์ํ๋ ๊ฒฝ์ฐ, ์คํ๋์๋ฅผ ๊ฑฐ์น์ง ์๊ณ ๋ง์ง๋ง์ผ๋ก ์ฑ์ ์ฌ์ฉํ์๋ ๋ณธ ๋ทฐ๊ฐ ์ ์ง๋์ด ์์ต๋๋ค.
AppDelegate์ didReceive ์์ ์ฒ๋ฆฌ
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo
...
// active / inactive / background
let applicationState = UIApplication.shared.applicationState
// ์ํ์ ๋ฐ๋ผ ์ํ๋ ๋ทฐ๋ก ์ ํ
if applicationState == .active || applicationState == .inactive {
// active ๋๋ inactive ์ํ์์ ํธ์์๋ฆผ์ ๋๋ฅธ ๊ฒฝ์ฐ ์ฒ๋ฆฌ
} else if applicationState == .background {
// background ์ํ์์ ํธ์์๋ฆผ์ ๋๋ฅธ ๊ฒฝ์ฐ ์ฒ๋ฆฌ
}
}