mirror of
https://github.com/lemeow125/StudE-Frontend.git
synced 2024-11-17 06:19:25 +08:00
Fixed group message notifications always triggering
This commit is contained in:
parent
2461f2c404
commit
fa07743a90
1 changed files with 34 additions and 22 deletions
|
@ -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",
|
||||||
|
|
Loading…
Reference in a new issue