简体   繁体   English

iOS Swift 应用终止时处理通知点击

[英]iOS Swift Handle notification click when app is terminated

How to handle notification click action when app is terminated when i click the notification my app just lunch first screen as normal lunch not the expected screen i don't now or how can i debug this problem to find out what happening exactly当我单击通知时应用程序终止时如何处理通知单击操作

when app is running or in background mode, i handle my notification click actions through this method:当应用程序正在运行或处于后台模式时,我通过此方法处理我的通知点击操作:

func userNotificationCenter(_ center: UNUserNotificationCenter,
                                       didReceive response: UNNotificationResponse,
                                       withCompletionHandler completionHandler: @escaping () -> Void) {
        let userInfo = response.notification.request.content.userInfo
        // Print message ID.
        if let messageID = userInfo[gcmMessageIDKey] {
            print("Message ID: \(messageID) tap")
        }
        NotificationCenter.default.post(name: .remoteNotificationActionName, object: nil, userInfo: userInfo)
        print("willPresent userInfo", userInfo)
        completionHandler()
    }

this notification will send to scene delegate class and from my scene delegate i will open the appropriate screen:此通知将发送给场景委托 class 并从我的场景委托中打开相应的屏幕:

@objc
private func remoteNotificationClickAction(notification: Notification){
    performRemoteNotificationClickAction(notification: notification)
}

private func performRemoteNotificationClickAction(notification: Notification){
    guard let userInfo = notification.userInfo else { return }
    guard let clickAction = NotificationsActions(rawValue: userInfo["click_action"] as? String ?? "") else { return }
    switch clickAction {
    case .dashboardAds:
        routeToNotifications()
    case .dashboardAdsContent:
        routeToNotifications()
    case .providerAcceptOrder, .providerOnWay, .providerArrive, .tripStarted, .addReceiverToChat:
        guard let orderString = userInfo["order"] as? String else { return }
        routeToActiveOrder(orderString: orderString)
    case .orderComplete:
        guard let orderString = userInfo["order"] as? String else { return }
        routeToServiceReport(orderString: orderString)
    case .orderCanceled, .receiverTracking, .receiverCancelOrder, .transportationDocumentIssued, .adminCancelOrder, .userCancelOrder:
        guard let orderString = userInfo["order"] as? String else { return }
        self.routeToOrderDetails(orderString: orderString)
    case .providerCancelOrderAccepted, .offerAdded, .receiverTrackingAccept, .orderExpired, .receiverTrackingReject, .offerWithdrawn:
        guard let orderString = userInfo["order"] as? String else { return }
        routeToSearchForServiceProvider(orderString: orderString)
    case .messageReceive:
        guard let orderString = userInfo["order"] as? String else { return }
        routeToChat(orderString: orderString)
    case .amountSent, .amountReceive, .amountAdded, .amountSubtracted, .withdrawAccepted, .withdrawRejected, .bankTransferAccepted, .bankTransferRejected:
        routeToWallet()
    case .pointsAdded:
        routeToTopUpPoints()
    case .reportClosed:
        routeToComplaints()
    default:  break
    }
    
}
 func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    guard let windowScene = scene as? UIWindowScene else { return }
    window = UIWindow(windowScene: windowScene)
    whereToGo(window!)
    if let notificationResponse = connectionOptions.notificationResponse{
        NotificationCenter.default.post(name: .remoteNotificationActionName, object: nil, userInfo: notificationResponse.notification.request.content.userInfo)
    }
}

as shown in above code my notification received in connectionOptions.notificationResponse如上面的代码所示,我在connectionOptions.notificationResponse中收到的通知

When app is in killed/terminated state, didReceiveRemoteNotification method will not be called.当应用程序处于终止/终止 state 时,不会调用 didReceiveRemoteNotification 方法。 Then on the tap of notification application(_:didFinishLaunchingWithOptions) method will be called.然后点击通知应用程序(_:didFinishLaunchingWithOptions)方法将被调用。 launchOption contains the payload if app is launched by tap on notification.如果通过点击通知启动应用程序,则 launchOption 包含有效负载。 For that write the given code in this method:为此,在此方法中编写给定的代码:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
       if launchOptions != nil{
         let userInfo = launchOptions? 
         [UIApplicationLaunchOptionsKey.remoteNotification]
          if userInfo != nil {
        // Perform action here
         }
    }

All your payload data will be available in launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] & perform your app logic(navigation..) from there.您的所有有效负载数据都将在 launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] 中可用并从那里执行您的应用程序逻辑(导航..)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 应用程序终止时的 Swift IOS 启动通知 - Swift IOS launch notification when app terminated 应用程序终止时处理通知(iOS) - Handle Notifications when app is terminated(iOS) 如何处理 iOS Swift 中的推送通知点击? - How to handle push notification click in iOS Swift? Flutter:当应用程序处于后台但未在 iOS 中终止时,无法在通知单击时重定向到特定屏幕 - Flutter : Unable to redirect to specific screen on Notification click when app is in background but not terminated in iOS 应用未运行/应用终止时如何处理推送通知 - How to handle push notification when app is not running/app is terminated 应用终止后无法处理本地通知 - Unable to Handle Local Notification when app has been terminated 当应用程序在后台或终止时,FCM通知iOS无法正常工作 - FCM Notification iOS not working when app is in background or terminated 当应用程序在 ios 13 中的静默推送通知中终止时运行代码 - Run code when app is terminated on silent push notification in ios 13 当应用程序在 iOS 14 中终止或终止时,未收到 Voip 通知 - Voip notification did not receive when app kill or terminated in iOS 14 FCM iOS 推送通知在应用程序终止时捕获自定义数据? - FCM iOS push notification capture custom data when app is terminated?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM