Skip to content

Commit 7a2b1ce

Browse files
committed
feat(ios_localnotif): adding localStorage usage
1 parent 83d7ac7 commit 7a2b1ce

File tree

1 file changed

+45
-26
lines changed

1 file changed

+45
-26
lines changed

xpeapp_ios/XpeApp/XpeApp/Presentation/Notifications/BackgroundTaskManager.swift

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class BackgroundTaskManager {
1414
static let instance = BackgroundTaskManager()
1515

1616
let taskId = "xpeapp.notifications_check" // Define task id
17-
private let startDate = Date(timeIntervalSinceNow: 120) // Schedule the next task in 2 minutes
17+
private let startDate = Date(timeIntervalSinceNow: 30) // Schedule the next task in 2 minutes
1818
private let localNotificationsManager = LocalNotificationsManager.instance
1919

2020
private init() {}
@@ -48,38 +48,57 @@ class BackgroundTaskManager {
4848
}
4949

5050
private func handleBackgroundTask(task: BGProcessingTask) {
51-
52-
// let obtainedLastNewsletter = await NewsletterRepositoryImpl.instance.getLastNewsletter()
53-
54-
// Send test notification
51+
debugPrint("Background Received")
52+
// Étape 5: Envoyer une notification
5553
localNotificationsManager.scheduleNotification(
56-
title: "title",
57-
message: "message de tests"
54+
title: "Nouvelle newsletter !",
55+
message: "Restez informé avec notre nouvelle newsletter !"
5856
)
57+
58+
Task {
59+
60+
61+
62+
//await checkNewNewsletter()
5963

60-
// Mark the task as completed
61-
task.setTaskCompleted(success: true)
64+
// Mark the task as completed
65+
task.setTaskCompleted(success: true)
6266

63-
// Reschedule the task
64-
self.submitBackgroundTask()
67+
// Reschedule the task
68+
self.submitBackgroundTask()
69+
}
6570
}
6671

67-
private checkNewNewsletter() {
68-
// TODO: Call to the API to check if there is a new newsletter
69-
// let obtainedLastNewsletter = await NewsletterRepositoryImpl.instance.getLastNewsletter()
72+
private func checkNewNewsletter() async {
73+
// Étape 1: Récupérer la dernière newsletter depuis l'API
74+
let obtainedLastNewsletter = await NewsletterRepositoryImpl.instance.getLastNewsletter()
7075

71-
// TODO: Check if there is a newsletter saved in local storage
72-
// We only save the last newsletter pdfURL to identify the newsletter
73-
74-
// TODO: If there is not newsletter in the local storage, save the fetched one
75-
76-
// TODO: If there is a newsletter in the local storage, compare the last newsletter fetched with the local one
77-
// If they are different, save the new one and send a notification
76+
// Étape 2: Vérifier si une newsletter est enregistrée localement via le UserDefaults
77+
let lastSavedPDFURL = UserDefaults.standard.string(forKey: "lastNewsletterPDFURL")
78+
79+
// Étape 3: Comparer la newsletter locale avec la nouvelle
80+
if let lastSavedPDFURL = lastSavedPDFURL {
81+
if lastSavedPDFURL == obtainedLastNewsletter?.pdfUrl {
82+
debugPrint("Aucune nouvelle newsletter détectée.")
83+
84+
} else {
85+
// Étape 4: Nouvelle newsletter détectée -> Mettre à jour le stockage local
86+
UserDefaults.standard.set(obtainedLastNewsletter?.pdfUrl, forKey: "lastNewsletterPDFURL")
87+
88+
// Étape 5: Envoyer une notification
89+
localNotificationsManager.scheduleNotification(
90+
title: "Nouvelle newsletter !",
91+
message: "Restez informé avec notre nouvelle newsletter !"
92+
)
93+
debugPrint("Nouvelle newsletter détectée et notification envoyée.")
94+
}
7895

79-
// Send test notification
80-
localNotificationsManager.scheduleNotification(
81-
title: "Nouvelle newsletter !",
82-
message: "Restez informé avec notre nouvelle newsletter !"
83-
)
96+
97+
} else {
98+
debugPrint("Sauvegarde d'une newsletter pour référence !")
99+
// Étape 4: Nouvelle newsletter détectée -> Mettre à jour le stockage local
100+
UserDefaults.standard.set(obtainedLastNewsletter?.pdfUrl, forKey: "lastNewsletterPDFURL")
101+
}
84102
}
103+
85104
}

0 commit comments

Comments
 (0)