Reputation: 411
I'm trying to show an alert when the user opens the app (app became active). The reason for showing the alert is to give information that the user is logged in, on another device, so they will be logged out from the device.
The following code is implemented in AppDelegate
. My expectation is - if it's going to cause failure - it means they will be logged out.
The API works well, but I don't know how to show the alert when the app's in the foreground. How do you implement this correctly?
I don't want to hit the API in every viewDidLoad
in each 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)
}
}
}
}
Upvotes: 0
Views: 240
Reputation: 210
You can add code in AppDelegate below like this
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
}
Upvotes: 1