bannaviiOS) foreground ver - fcm push 클릭시 서버에서 보내준 url대로 이동하는 코드 순서도 흐름
2022. 11. 11. 11:42ㆍiOS/iOS
728x90
반응형
앱이 켜져있는 foreground 상태에서 push를 받았을때, push를 터치하면 진행되는 흐름을 정리했습니다.
제가 헷갈리지 않으려고...껄껄
웹뷰에 url 로딩 완료시 호출되는 함수(한번이라도 로드 된적이 있는지 판별하는 isFirstLoad값을 false로 설정)
//class생략
private var isFirstLoad: Bool = true
//초기에 웹뷰 url로딩 완료시 호출되는 함수
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
if (self.isFirstLoad) {
self.isFirstLoad = false
if let url = URL(string: SmartStoreSharedData.instance.url ?? "url is nil") {
let request: URLRequest = URLRequest(url: url)
webView.load(request)
}
}
}
step1) 푸시를 수신하기 직전에 호출
Appdelegate.swift - usernotificationCenter willPresent 함수 호출
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification) async -> UNNotificationPresentationOptions {
}
step2) 수신받은 푸시를 터치했을때 호출
Appdelegate.swift - usernotificationCenter didReceive 함수 호출
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
let userInfo = response.notification.request.content.userInfo //푸시로 오는 데이터
let title = response.notification.request.content.title //알림제목
let body = response.notification.request.content.body //알림내용
if let link = userInfo["link"] {
SmartStoreSharedData.instance.url = link as! String //해당 링크로 이동할때 UrlInfo.shared.url를 이용한다.
}
//foreground 상태에서 푸시를 클릭했을때는 viewwillappear함수가 호출되지 않을테니 푸시를 터치했을때 앱 state가 active상태라면 웹뷰에 전달받은 url로 로딩을 시킴
if UIApplication.shared.applicationState == .active {
NotificationCenter.default.post(name: NSNotification.Name("uIApplicationDidBecomeActive"), object: nil)
}
completionHandler()
}
viewwillappear함수 내부에서 추가해줬었던 addObserver(applicationDidBecome함수 호출)
viewwilldisappear에서 removeObserver는 따로 추가
NotificationCenter.default.addObserver(self, selector: #selector(self.applicationDidBecome), name: NSNotification.Name("uIApplicationDidBecomeActive"), object: nil)
notificationcenter가 실행될때 추가되는 함수 applicationDidBecome
@objc public func applicationDidBecome() {
//푸시 클릭 시 SmartStoreSharedData.instance.url에 저장된 링크로 webView를 이동 시킵니다.
//앱이 한번이라도 구동 된 적이 있다면
if(!self.isFirstLoad) {
if let url = URL(string: SmartStoreSharedData.instance.url ?? "url is nil") {
let request: URLRequest = URLRequest(url: url)
webView.load(request)//전달받은 알림 페이지 웹뷰에 로딩
SmartStoreSharedData.instance.url = ""
}
}
}
도움이 되셨다면 하트를 부탁드립니다 🌷
댓글도 언제나 환영이에요^ㅅ^
728x90
반응형
'iOS > iOS' 카테고리의 다른 글
bannaviiOS) Unsupported OS version에러 해결하기 (0) | 2022.11.15 |
---|---|
bannaviiOS) background ver - fcm push 클릭시 서버에서 보내준 url대로 이동하는 코드 순서도 흐름 (0) | 2022.11.11 |
bannaviiOS) appdelegate와 scenedelegate(UILifeCycle에 관하여) (0) | 2022.11.09 |
bannaviiOS) bundleid를 바꿨는데 push가 안올때 체크사항(feat. enterprise배포) (0) | 2022.11.07 |
bannaviiOS) push 사용중, bundleid를 바꿨는데 build fail일때 (0) | 2022.11.04 |