macOS, iOS
[iOS] userNotification 함수 update for iOS13
SweetDev
2021. 6. 5. 12:23
1. AppDelegate의 didFinishLaunchingWithOptions에서
// Check if launched from notification
let notificationOption = launchOptions?[.remoteNotification]
if let notification = notificationOption as? [String: AnyObject], let aps = notification["aps"] as? [String: AnyObject] {
print(aps)
let userInfo = aps["data"]!
print(userInfo["actionParam"])
let a = userInfo["actionParam"]
let decoder = JSONDecoder()
이런식으로 분기를 타는 법은 이제는 안쓰는 방식. SceneDelegate의 willConnectTo 함수에서 해야한다!!!!
2. 나는 보통
extension AppDelegate: UNUserNotificationCenterDelegate {
// 앱 foreground(실행중인) 상태일때 앱에 보여지게 하기 위한 코드.
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{}
/// 만약에 백그라운드 상태에서 Push를 받으면, 그 Push를 탭해서 들어갔을 때 아래 내용이 실행됨
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
}
}
이런식으로 써왔는데 이것보다는 함수를 모두 userNotification함수를 쓰는것이 좋다.
// 백그라운드, 포그라운드 모두 푸시를 받아서 클릭했을 때 (아예 앱 종료에서 진입하는건 SceneDelegate에서 처리함)
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void)
{
let notification = response.notification
let userInfo = notification.request.content.userInfo
if response.actionIdentifier == UNNotificationDismissActionIdentifier {
print ("Message Closed")
}
else if response.actionIdentifier == UNNotificationDefaultActionIdentifier {
print ("푸시 메시지 클릭 했을 때")
}
// 앱 foreground(실행중인) 상태일때 앱에 푸시가 보여지게 하기 위한 코드.
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
let userInfo = notification.request.content.userInfo
print(userInfo)
// Change this to your preferred presentation option
completionHandler([[.alert, .sound]])
}
// (아예 앱 종료에서 진입하는건 SceneDelegate에서 처리함)
함수를 써서 분기를 타는게 좋아보인다.
3. 아예 종료된 상태에서 켜려면..iOS13부터는 SceneDelegate에서 할당한다고 한다...
왜 안알려줬니..?
willConnectTo에서 쓰면 됨...
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
if connectionOptions.notificationResponse == nil {
//Not opened from push notification
} else {
//Opened from push notification
}
}