簡體   English   中英

iOS Swift 應用終止時處理通知點擊

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

當我單擊通知時應用程序終止時如何處理通知單擊操作

當應用程序正在運行或處於后台模式時,我通過此方法處理我的通知點擊操作:

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()
    }

此通知將發送給場景委托 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)
    }
}

如上面的代碼所示,我在connectionOptions.notificationResponse中收到的通知

當應用程序處於終止/終止 state 時,不會調用 didReceiveRemoteNotification 方法。 然后點擊通知應用程序(_:didFinishLaunchingWithOptions)方法將被調用。 如果通過點擊通知啟動應用程序,則 launchOption 包含有效負載。 為此,在此方法中編寫給定的代碼:

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

您的所有有效負載數據都將在 launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] 中可用並從那里執行您的應用程序邏輯(導航..)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM