         var castContainerVC: GCKUICastContainerViewController?
         castContainerVC = (window?.rootViewController as? GCKUICastContainerViewController)
         castContainerVC?.miniMediaControlsItemEnabled = notificationsEnabled
-      }
-      else {
+      } else {
         var rootContainerVC: RootContainerViewController?
         rootContainerVC = (window?.rootViewController as? RootContainerViewController)
         rootContainerVC?.miniMediaControlsViewEnabled = notificationsEnabled

        if (prefItemType == "PSChildPaneSpecifier") {
          let prefItemFile = prefItem["File"]  as? String
          loadDefaults(&appDefaults, fromSettingsPage: prefItemFile!, inSettingsBundleAt: settingsBundleURL)
        }
        else if (prefItemKey != nil) && (prefItemDefaultValue != nil) {
          appDefaults[prefItemKey!] = prefItemDefaultValue
        }
      }

    if (prefApplicationID == kPrefCustomReceiverSelectedValue) {
      prefApplicationID = userDefaults.string(forKey: kPrefCustomReceiverAppID)
    }
    if prefApplicationID == nil {
      let message: String = "You don't seem to have an application ID.\n" +
      "Please fix the app settings."
      showAlert(withTitle: "Invalid Receiver Application ID", message: message)
      return nil
    } else {
      let appIdRegex = try? NSRegularExpression(pattern: "\\b[0-9A-F]{8}\\b", options: [])
      let rangeToCheck = NSRange(location: 0, length: (prefApplicationID?.characters.count ?? 0))
      let numberOfMatches = appIdRegex?.numberOfMatches(in: prefApplicationID!,
                                                        options: [],
                                                        range: rangeToCheck)
      if numberOfMatches == 0 {
        let message: String = "\"\(prefApplicationID)\" is not a valid application ID\n" +
        "Please fix the app settings (should be 8 hex digits, in CAPS)"
        showAlert(withTitle: "Invalid Receiver Application ID", message: message)
        return nil
      }
    }
    return prefApplicationID
  }

  func syncWithUserDefaults() {
    let userDefaults = UserDefaults.standard
    // Forcing no logging from the SDK
    enableSDKLogging = false
    let mediaNotificationsEnabled = userDefaults.bool(forKey: kPrefEnableMediaNotifications)
    GCKLogger.sharedInstance().delegate?.logMessage?("Notifications on? \(mediaNotificationsEnabled)", fromFunction: #function)
    if firstUserDefaultsSync || (self.mediaNotificationsEnabled != mediaNotificationsEnabled) {
      self.mediaNotificationsEnabled = mediaNotificationsEnabled
      if useCastContainerViewController {
        let castContainerVC = (window?.rootViewController as? GCKUICastContainerViewController)
        castContainerVC?.miniMediaControlsItemEnabled = mediaNotificationsEnabled
      }
      else {
        let rootContainerVC = (window?.rootViewController as? RootContainerViewController)
        rootContainerVC?.miniMediaControlsViewEnabled = mediaNotificationsEnabled
      }
    }
    firstUserDefaultsSync = false
  }
}

// MARK: - GCKLoggerDelegate
extension AppDelegate: GCKLoggerDelegate {
  func logMessage(_ message: String, fromFunction function: String) {
    if enableSDKLogging {
      // Send SDK's log messages directly to the console.
      print("\(function)  \(message)")
    }
  }

}

// MARK: - GCKSessionManagerListener
extension AppDelegate: GCKSessionManagerListener {

  func sessionManager(_ sessionManager: GCKSessionManager, didEnd session: GCKSession, withError error: Error?) {
    if error == nil {
      Toast.displayMessage("Session ended", for: 3, in: (window?.rootViewController?.view)!)
    } else {
      let message = "Session ended unexpectedly:\n\(error?.localizedDescription)"
      showAlert(withTitle: "Session error", message: message)
    }
