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