Skip to content

Commit

Permalink
Merge pull request #270 from TeamRecorDream/fix/266-push-alarm
Browse files Browse the repository at this point in the history
[Fix] 같은 소셜, 다른 기기로 로그인 시 새로운 기기로 푸시알림 리스케줄링
  • Loading branch information
Seokyeong237 authored Sep 28, 2023
2 parents 299313b + c760d67 commit 5fbe0d7
Showing 1 changed file with 90 additions and 0 deletions.
90 changes: 90 additions & 0 deletions src/services/AuthService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { AuthResponseDto } from "../interfaces/auth/AuthResponseDto";
import { AuthLogoutDto } from "../interfaces/auth/AuthLogoutDto";
import exceptionMessage from "../modules/exceptionMessage";
import agenda from "../loaders/agenda";
import pushMessage from "../modules/pushMessage";
import * as admin from "firebase-admin";

const kakaoLogin = async (kakaoToken: string, fcmToken: string): Promise<AuthResponseDto | null | undefined> => {
try {
Expand Down Expand Up @@ -77,6 +79,50 @@ const kakaoLogin = async (kakaoToken: string, fcmToken: string): Promise<AuthRes

await User.findByIdAndUpdate(existUser._id, existUser);

if (existUser.time != null && existUser.isActive == true) {
const timeSplit = existUser.time.split(/ /);
const ampm = timeSplit[0];
const pushTime = timeSplit[1];

// 새로 로그인 된 기기로 알림 리스케줄링
const alarms = {
android: {
data: {
title: pushMessage.title,
body: pushMessage.body,
},
},
apns: {
payload: {
aps: {
contentAvailable: true,
alert: {
title: pushMessage.title,
body: pushMessage.body,
},
},
},
},
tokens: existUser.fcmTokens,
};

agenda.define("push_" + `${existUser._id}`, async (job: any, done: any) => {
admin
.messaging()
.sendMulticast(alarms)
.then(function (res: any) {
console.log("Sent message result: ", res);
});
job.repeatEvery("24 hours").save();
done();
});
agenda.start();

await agenda.cancel({ "data.userId": existUser._id });

agenda.schedule("today at " + pushTime + ampm + "", "push_" + `${existUser._id}`, { userId: existUser._id });
}

return data;
} catch (err) {
console.log(err);
Expand Down Expand Up @@ -154,6 +200,50 @@ const appleLogin = async (appleToken: string, fcmToken: string): Promise<AuthRes

await User.findByIdAndUpdate(existUser._id, existUser);

if (existUser.time != null && existUser.isActive == true) {
const timeSplit = existUser.time.split(/ /);
const ampm = timeSplit[0];
const pushTime = timeSplit[1];

// 새로 로그인 된 기기로 알림 리스케줄링
const alarms = {
android: {
data: {
title: pushMessage.title,
body: pushMessage.body,
},
},
apns: {
payload: {
aps: {
contentAvailable: true,
alert: {
title: pushMessage.title,
body: pushMessage.body,
},
},
},
},
tokens: existUser.fcmTokens,
};

agenda.define("push_" + `${existUser._id}`, async (job: any, done: any) => {
admin
.messaging()
.sendMulticast(alarms)
.then(function (res: any) {
console.log("Sent message result: ", res);
});
job.repeatEvery("24 hours").save();
done();
});
agenda.start();

await agenda.cancel({ "data.userId": existUser._id });

agenda.schedule("today at " + pushTime + ampm + "", "push_" + `${existUser._id}`, { userId: existUser._id });
}

return data;
} catch (err) {
console.log(err);
Expand Down

0 comments on commit 5fbe0d7

Please sign in to comment.