Handle notifications on/off in the receive handler instead of just in registration

This ensures we have a registration delegate setup.
This commit is contained in:
Jackson Harper
2022-12-13 14:43:32 +08:00
parent 6b3cf9601e
commit 1b2c3db90c

View File

@ -9,8 +9,6 @@ import Utils
extension AppDelegate {
func configureFirebase() {
guard UserDefaults.standard.bool(forKey: UserDefaultKey.notificationsEnabled.rawValue) else { return }
let keys: FirebaseKeys? = {
let isProd = (PublicValet.storedAppEnvironment ?? .initialAppEnvironment) == .prod
let firebaseKeys = isProd ? AppKeys.sharedInstance?.firebaseProdKeys : AppKeys.sharedInstance?.firebaseDemoKeys
@ -30,9 +28,7 @@ extension AppDelegate {
FirebaseApp.configure(options: firebaseOpts)
FirebaseConfiguration.shared.setLoggerLevel(.min)
if UserDefaults.standard.bool(forKey: UserDefaultKey.notificationsEnabled.rawValue) {
registerForNotifications()
}
registerForNotifications()
}
func registerForNotifications() {
@ -55,6 +51,8 @@ extension AppDelegate: UNUserNotificationCenterDelegate {
withCompletionHandler completionHandler:
@escaping (UNNotificationPresentationOptions) -> Void
) {
guard UserDefaults.standard.bool(forKey: UserDefaultKey.notificationsEnabled.rawValue) else { return }
let userInfo = notification.request.content.userInfo
UIApplication.shared.applicationIconBadgeNumber = 0
print(userInfo) // extract data sent along with PN
@ -66,6 +64,8 @@ extension AppDelegate: UNUserNotificationCenterDelegate {
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void
) {
guard UserDefaults.standard.bool(forKey: UserDefaultKey.notificationsEnabled.rawValue) else { return }
let userInfo = response.notification.request.content.userInfo
if let linkData = userInfo["link"] as? String {
@ -101,6 +101,12 @@ extension AppDelegate: MessagingDelegate {
let savedToken = UserDefaults.standard.string(forKey: UserDefaultKey.firebasePushToken.rawValue)
let deviceTokenID = UserDefaults.standard.string(forKey: UserDefaultKey.deviceTokenID.rawValue)
if !UserDefaults.standard.bool(forKey: UserDefaultKey.notificationsEnabled.rawValue) {
// save the token for use later if needed and return so it is not uploaded
UserDefaults.standard.set(fcmToken, forKey: UserDefaultKey.firebasePushToken.rawValue)
return
}
// If the deviceTokenID is null, that means we haven't set our token yet, and this is just
// a previously saved token.
if savedToken == fcmToken, deviceTokenID != nil {