How to show alert in every ViewController when app is active (foreground)?

lauwis

I'm trying to show an alert when user opens the app (app become active). The reason of showing alert is to give information that user is logged in, in another device, so he will be logged out from the device.

The code below is implemented in AppDelegate. My expectation is - if it's going to cause failure - it means he will be logged out.

The API works well, but I don't know how to show the alert when apps in the foreground. How to 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)
            }
        }
    }
}
Dhruvin Thumar

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
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to show local notification when app is foreground

How to present Lock_ViewController when app is entering foreground in SWIFT?

How to show an Alert dialog for push notifications when not using a ViewController

Twilio Notifications alert received when app in foreground

Flutter Firebase how to control notification not to show when the app is in foreground

Show notification in iOS app While app in foreground / Active in swift 3

How do I prevent Alert when App is on Foreground with Incoming OneSignal Push Notification?

How to show a alert dialog when app is launched first time

Don't show Alert when the ViewController is not in Window hierarchy

Which View Controller is active when app comes to foreground?

Show an alert even when the app is not running

How to detect current foreground app when foreground service is running

How to load the current viewcontroller when application comes to foreground

Alert box not show in the right direction when active the condition

How to handle the Firebase notification when app is in foreground

How to update UI when app is in foreground?

How can I bring Electron app to foreground every "N" minute?

How can I show a custom Local notification View or an Custom Alert view when app is in Background?

How to show alert messages popup from background service when app is in background

How to show an alert when no results found

How to show alert when enter key pressed

How to show an alert when a variable is changed?

avoid showing alert for notification while app is in foreground

UIPageViewController not reloading current viewController after foreground app

Show an alert when deferring a Promoted IAP request from App Store

show the second ViewController when app launch the second time

Show Alert on ViewController from external static function

Swift - push alert action to show new ViewController

Flutter, Firebase ios, cloud message doesn't show when app in background, only if app in foreground