Fixed group message notifications always triggering

This commit is contained in:
Keannu Bernasol 2023-10-01 16:39:43 +08:00
parent 2461f2c404
commit fa07743a90

View file

@ -17,16 +17,26 @@ const FETCH_GROUP_MESSAGES = "GROUP_MESSAGES_TASK";
TaskManager.defineTask(FETCH_GROUP_MESSAGES, async () => {
const data = await GetStudyGroupMessages();
if (data[0] && data[1]) {
let messages_prev = JSON.parse(
(await AsyncStorage.getItem("messages")) || "{}"
let messages_prev = await JSON.parse(
(await AsyncStorage.getItem("messages")) || "[]"
);
if (!messages_prev) {
await AsyncStorage.setItem("messages", JSON.stringify(data[1]));
} else {
let message_curr = data[1];
let difference: Array<any> = messages_prev
.filter((x: any) => !message_curr.includes(x))
.concat(message_curr.filter((x: any) => !messages_prev.includes(x)));
.filter(
(x: any) =>
!message_curr.some(
(y: any) => JSON.stringify(y) === JSON.stringify(x)
)
)
.concat(
message_curr.filter(
(x: any) =>
!messages_prev.some(
(y: any) => JSON.stringify(y) === JSON.stringify(x)
)
)
);
if (difference.length > 0) {
console.log(`${difference.length} unread messages`);
@ -40,7 +50,6 @@ TaskManager.defineTask(FETCH_GROUP_MESSAGES, async () => {
},
});
}
}
} else {
console.log(data[1].response.data);
}
@ -52,7 +61,7 @@ TaskManager.defineTask(FETCH_STUDENT_STATUS, async () => {
const data = await GetStudyGroupListFiltered();
const student_status_data = await GetStudentStatus();
if (data[0] && data[1]) {
console.log("Background Fetch", data[1]);
console.log("Fetching nearby study groups...");
const entryWithLeastDistance = data[1].reduce(
(prev: StudyGroupType, curr: StudyGroupType) => {
return prev.distance < curr.distance ? prev : curr;
@ -63,6 +72,9 @@ TaskManager.defineTask(FETCH_STUDENT_STATUS, async () => {
student_status_data[1].study_group == null ||
student_status_data[1].study_group == ""
) {
console.log(
"User has no study group yet. Found nearby groups, pushing notification"
);
Notifications.scheduleNotificationAsync({
content: {
title: "Students are studying nearby",