我試圖在用戶打開應用程式(應用程式變為活動狀態)時顯示警報。顯示警報的原因是提供用戶在另一臺設備上登錄的資訊,因此他將從該設備中注銷。
下面的代碼是在AppDelegate. 我的期望是 - 如果它會導致失敗 - 這意味著他將被注銷。
該 API 運行良好,但我不知道如何在前臺應用程式時顯示警報。如何正確實施?
我不想viewDidLoad在 each in each中訪問 API viewController。
extension AppDelegate: MessagingDelegate {
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
guard let fcmToken = fcmToken else { return }
print("Firebase registration token: \(fcmToken)")
let dataDict:[String: String] = ["token": fcmToken]
NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)
is generated.
guard !AuthManager.shared.userLoginSession.isEmpty, //true user belom login
let tokenlogin = SettingsManager.tokenlogin,
!tokenlogin.isEmpty //true token login harus kosong
else { return }
NetworkManager.instance.requestObject(ServiceAPI.start(regID: fcmToken, id: AuthManager.shared.userLoginSession, token: tokenlogin), c: SingleResponse<StartResponse>.self) { (result) in
switch result {
case .success(let response):
AuthManager.shared.userLoginSession = response.data.apikey ?? ""
// MoEngage.sharedInstance().setAlias("1234")
case .failure(let error):
// If it's going failure. it means they will be logged out
print("FCM with error", error.description)
}
}
}
}
uj5u.com熱心網友回復:
您可以像這樣在下面的 AppDelegate 中添加代碼
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
DispatchQueue.main.asyncAfter(deadline: .now() 1.0) {
let window = UIApplication.shared
.connectedScenes
.flatMap { ($0 as? UIWindowScene)?.windows ?? [] }
.first { $0.isKeyWindow }
let alertController = UIAlertController(title: "Test Data", message:"Message", preferredStyle: UIAlertController.Style.alert)
// add an action (button)
alertController.addAction(UIAlertAction(title: "OK", style: UIAlertAction.Style.default, handler: nil))
window?.rootViewController?.present(alertController, animated: true)
}
return true
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/522864.html
標籤:IOS迅速
