There are few methods you may obtain synchronization of your app and its extension however I believe utilizing UserDefaults
through app group is the simplest:
In applicationDidBecomeActive
:
let userDefaults = UserDefaults(suiteName: "com.yourapp.shared")
userDefaults?.set(true, forKey: "isAppActive")
Within the NotificationService extension:
let userDefaults = UserDefaults(suiteName: "com.yourapp.shared")
if let isAppActive = userDefaults?.bool(forKey: "isAppActive"), !isAppActive {
// increment badge rely
}
In applicationWillTerminate
:
let userDefaults = UserDefaults(suiteName: "com.yourapp.shared")
userDefaults?.set(false, forKey: "isAppActive")
Nonetheless, notice that this answer will not work as anticipated in case your app is crashing as applicationWillTerminate
will not be referred to as on this case. So do not let it crash 🙂