forked from TeamMajorLink/majorLink_server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request TeamMajorLink#87 from kchaeeun/refactor#82
♻️ [REFACTOR] 알림 전송 시 receiver가 아닌 sender에게 알림이 가는 오류 해결
- Loading branch information
Showing
3 changed files
with
76 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<!DOCTYPE html> | ||
<html lang="ko"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Notification Test Page</title> | ||
</head> | ||
<body> | ||
<input type="text" id="authToken" placeholder="Enter X-AUTH-TOKEN"/> | ||
<button type="button" onclick="login()">로그인</button> | ||
</body> | ||
<script type="text/javascript"> | ||
function login() { | ||
const authToken = document.getElementById('authToken').value.trim(); | ||
if (!authToken) { | ||
alert('X-AUTH-TOKEN을 입력해주세요.'); | ||
return; | ||
} | ||
|
||
// Use URL with path variable | ||
const eventSourceUrl = `/notification/subscribe/${encodeURIComponent(authToken)}`; | ||
|
||
const eventSource = new EventSource(eventSourceUrl); | ||
|
||
eventSource.onmessage = function(event) { | ||
console.log('Received message:', event.data); | ||
|
||
const data = JSON.parse(event.data); | ||
|
||
(async () => { | ||
const showNotification = () => { | ||
const notification = new Notification('알림', { | ||
body: data.content, | ||
}); | ||
|
||
setTimeout(() => { | ||
notification.close(); | ||
}, 10 * 1000); | ||
|
||
notification.addEventListener('click', () => { | ||
window.open(data.url, '_blank'); | ||
}); | ||
} | ||
|
||
// 브라우저 알림 허용 권한 | ||
let granted = false; | ||
|
||
if (Notification.permission === 'granted') { | ||
granted = true; | ||
} else if (Notification.permission !== 'denied') { | ||
let permission = await Notification.requestPermission(); | ||
granted = permission === 'granted'; | ||
} | ||
|
||
// 알림 보여주기 | ||
if (granted) { | ||
showNotification(); | ||
} | ||
})(); | ||
}; | ||
|
||
eventSource.onerror = function(event) { | ||
console.error('SSE 오류:', event); | ||
eventSource.close(); | ||
}; | ||
} | ||
</script> | ||
</html> |