Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] 푸시알림에 데이터 페이로드 추가 #98

Merged
merged 2 commits into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 73 additions & 25 deletions src/controller/alarmController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,45 +54,47 @@ const setFinishedAlarm = async(req: Request, res: Response, next: NextFunction)
const setOnDeadlineAlarm = async() => {
try{
const today = moment().format('YYYY-MM-DD');
const deviceTokens = await alarmService.getUserListByDeadline(new Date(today));
if(!deviceTokens){
console.log("no device tokens");
const user = await alarmService.getUserListByDeadline(new Date(today));
if(!user){
return;
}

const data = {
"title": alarm.DEADLINE_ALARM_TITLE,
"contents": alarm.ON_DEADLINE_ALARM,
"deviceTokens": deviceTokens
for(var i =0;i<user.length;i++){
const data = {
"payload": user[i].worryId,
"title": alarm.DEADLINE_ALARM_TITLE,
"contents": alarm.ON_DEADLINE_ALARM,
"deviceToken": user[i].deviceToken
}
pushAlarmWithPayload(data);
}

await pushAlarmToMany(data);

}catch (error) {

console.log(error)
}
}


const setBeforeDeadlineAlarm = async() => {
try{
const deadline = moment().add(3,"days").format('YYYY-MM-DD');
const deviceTokens = await alarmService.getUserListByDeadline(new Date(deadline));
if(!deviceTokens){
console.log("no device tokens");
const user = await alarmService.getUserListByDeadline(new Date(deadline));
if(!user){
return;
}

const data = {
"title": alarm.DEADLINE_ALARM_TITLE,
"contents": alarm.BEFORE_DEADLINE_ALARM,
"deviceTokens": deviceTokens
for(var i =0;i<user.length;i++){
const data = {
"payload": user[i].worryId,
"title": alarm.DEADLINE_ALARM_TITLE,
"contents": alarm.BEFORE_DEADLINE_ALARM,
"deviceToken": user[i].deviceToken
}
pushAlarmWithPayload(data);
}

await pushAlarmToMany(data);

}catch (error) {

console.log(error)
}
}

Expand All @@ -114,21 +116,56 @@ const setNoDeadlineAlarm = async() => {
await pushAlarmToMany(data);

}catch (error) {

console.log(error)
}
}


const pushAlarm = (data: any, next: NextFunction) => {
try{
const { deviceToken, title, contents } = data;

let message = {
token: deviceToken,
notification: {
title: title,
body: contents,
},
title: title,
body: contents,
}
}

admin
.messaging()
.send(message)
.then(function (response:Response) {
console.log('Successfully sent message: : ', response)
// return res.status(200).json({success : true})
})
.catch(function (err:Error) {
console.log('Error Sending message!!! : ', err)
// return res.status(400).json({success : false})
});
} catch (error) {
next(error)
}

}

const pushAlarmWithPayload = (data: any) => {
try{
const { payload, deviceToken, title, contents } = data;

// data must only contain string values
let message = {
token: deviceToken,
notification: {
title: title,
body: contents,
},
data: {
worryId: String(payload)
}
}
console.log(message)

admin
.messaging()
Expand All @@ -142,7 +179,7 @@ const pushAlarm = (data: any, next: NextFunction) => {
// return res.status(400).json({success : false})
});
} catch (error) {
next(error);
console.log(error)
}

}
Expand Down Expand Up @@ -182,6 +219,16 @@ const settingAlarm = async (req: Request, res: Response, next: NextFunction) =>
const { isTrue } = req.params
const { userId, deviceToken } = req.body

const data = await tokenService.getDeviceToken(userId)
// 알람 비활성화 상태에서 알람 비활성화하려 할 때
if(data == "" && +isTrue == 0){
return res.status(sc.OK).send(success(statusCode.OK, rm.ALARM_DISABLE_SUCCESS))
}
// 알람 활성화 상태에서 알람 활성화하려 할 때
if(data != "" && +isTrue == 1){
return res.status(sc.OK).send(success(statusCode.OK, rm.ALARM_ENABLE_SUCCESS));
}

if(+isTrue == 1){
await tokenService.setDeviceToken(userId, deviceToken)
return res.status(sc.OK).send(success(statusCode.OK, rm.ALARM_ENABLE_SUCCESS));
Expand All @@ -206,5 +253,6 @@ export default{
setBeforeDeadlineAlarm,
setNoDeadlineAlarm,
pushAlarm,
pushAlarmWithPayload,
settingAlarm
}
8 changes: 6 additions & 2 deletions src/repository/worryRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,12 @@ const findWorryListByTemplate = async(templateId: number,userId: number) => {
const findUserListByDeadline = async(date: Date) => {

return await prisma.worry.findMany({
select:{
user_id:true,
include:{
user:{
include:{
token:true
}
}
},
where: {
deadline: date,
Expand Down
14 changes: 6 additions & 8 deletions src/service/alarmService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,16 @@ const getUserListByDeadline =async (date: Date) => {
return null;
}

const user_ids :Array<number> = [];
const result :Array<any> = [];
for (var i =0;i<data.length;i++){
user_ids.push(data[i].user_id);
result.push({
worryId: data[i].id,
deviceToken: data[i].user.token?.device_token
})
}

const token = await tokenRepository.findDeviceTokenListByIds(user_ids);
const user_deviceTokens :Array<string|null> = [];
for (var i =0;i<token.length;i++){
user_deviceTokens.push(token[i].device_token);
}
return result;

return user_deviceTokens;
}

const getUserListWithNoDeadline =async (date: string) => {
Expand Down