Is it possible to schedule a time to update a field (periodically) in the Firebase database using Workmanager instead of using Cloud Function?
There are two Timestamps in the database. One is static, chosen by the user, and the second is updated periodically by Workmanager. I have been able to create a function to periodically update Timestampe in a field of the database but I also want that when the dynamic Timestamp is greater or equal to the static Timestamp, a field should be updated. Can anyone help with it, please?
With the main.dart
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
await Workmanager().initialize(callbackDispatcher, isInDebugMode: false);
runApp(const MyApp());
}
With the callbackDispatcher:
@pragma("vm:entry-point")
void callbackDispatcher() async {
WidgetsFlutterBinding.ensureInitialized();
Workmanager().executeTask((taskName, inputData) async {
if (Platform.isAndroid) {
SharedPreferencesAndroid.registerWith();
}
// else if(Platform.isIOS){
// SharedPreferencesIOS.registerWith();
// }
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
final uid = FirebaseAuth.instance.currentUser!.uid;
readsingleFirebaseData() async {
final docData =
FirebaseFirestore.instance.collection("ADS GHANA").doc(uid);
final snapshot = await docData.get();
if (snapshot.exists) {
return snapshot.data()!;
}
return null;
}
try {
final prefs = await SharedPreferences.getInstance();
await prefs.setInt("value", 100);
//This part is the dynamic Timestamp and works well
final dataFromFirebase = await readsingleFirebaseData();
if (dataFromFirebase != null) {
await FirebaseFirestore.instance
.collection("ADS GHANA")
.doc(uid)
.update({"Timestamp": FieldValue.serverTimestamp()});
//This is where I want the comparison to take place. To check whether the dynamic Timestamp is equal to the static Timestamp and then execute the function but can't seem to do it. Is there a better alternative?
try {
await FirebaseFirestore.instance
.collection("ADS GHANA")
.doc(uid)
.get()
.then((value) {
final timestamp = value.data()?["Timestamp"];//This dynamic
final departTime = value.data()?["TimestampDeparture"];//This is static
if (timestamp >= departTime) {
FirebaseFirestore.instance
.collection("ADS GHANA")
.doc(uid)
.update({"PostStatus": "Started"});
FirebaseFirestore.instance
.collection("ADS GHANA")
.doc(uid)
.collection("GHANA REQUEST")
.where("AdStatus",
whereNotIn: ["Passenger cancelled", "Rejected"])
.get()
.then((value) => value.docs.forEach((element) {
FirebaseFirestore.instance
.collection("ADS GHANA")
.doc(uid)
.collection("GHANA REQUEST")
.doc(element.id)
.update({"AdStatus": "Started"});
}));
}
});
} catch (e) {}
}
} catch (err) {
// Logger().e(err.toString());
throw Exception(err);
}
return true;
});
}
my button widget.
TextButton(
onTap:
await Workmanager()
.registerPeriodicTask(
"updateAds",
"PeriodicTaks",
tag: "2",
frequency:const Duration(minutes: 15),
existingWorkPolicy:ExistingWorkPolicy.replace,
initialDelay:const Duration(seconds: 15),
constraints: Constraints( networkType:
NetworkType.connected),
backoffPolicy:BackoffPolicy.linear,
backoffPolicyDelay: const Duration(seconds: 10),
);
)