maybeReRequestPermission static method

Future<void> maybeReRequestPermission()

Call on every app open. If the user is already opted in, does nothing. Otherwise, checks in at most once every _reaskIntervalDays days: if the OS will still show its native permission popup (mainly an Android possibility - iOS essentially never re-shows it after an explicit answer), triggers that; otherwise shows adhub's own custom dialog, which deep-links to the device's app-settings screen since that's the only way back in once permanently denied.

Implementation

static Future<void> maybeReRequestPermission() async {
  final PermissionStatus status = await Permission.notification.status;
  if (status.isGranted || status.isLimited || status.isProvisional) return;

  final prefs = await SharedPreferences.getInstance();
  final String? lastCheckStr = prefs.getString(_reaskLastCheckPrefsKey);

  if (lastCheckStr == null) {
    // First time this check has ever run - just start the clock, don't
    // immediately re-prompt right after the still-fresh initial ask.
    await prefs.setString(_reaskLastCheckPrefsKey, DateTime.now().toIso8601String());
    return;
  }

  final DateTime lastCheck = DateTime.parse(lastCheckStr);
  if (DateTime.now().difference(lastCheck).inDays < _reaskIntervalDays) return;

  await prefs.setString(_reaskLastCheckPrefsKey, DateTime.now().toIso8601String());

  if (status.isPermanentlyDenied || status.isRestricted) {
    await AdhubDialogs.showNotificationPermissionDialog();
  } else {
    // Still possibly re-askable (mainly Android's first-denial window) -
    // harmless no-op if the OS won't actually show anything.
    await Permission.notification.request();
  }
}