Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/developeracademy-postech/2024-nc2-m44-notification

NC2 Notification study of Ace and Groo
https://github.com/developeracademy-postech/2024-nc2-m44-notification

Last synced: about 7 hours ago
JSON representation

NC2 Notification study of Ace and Groo

Awesome Lists containing this project

README

        

# ๐Ÿ“ฑ DiaryNotification
NC2 Notification study of Ace and Groo

## ๐Ÿ–ผ๏ธ Prototype

### ๊ธฐ๋ณธ prototype

ํ•™์ƒ์ด ์ผ๊ธฐ๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ์ œ์ถœํ•˜๋ฉด ์„ ์ƒ๋‹˜์ด ์ผ๊ธฐ๋ฅผ ํ™•์ธํ•˜๊ณ  ๋„์žฅ์„ ์ฐ๋Š”๋‹ค.

https://github.com/DeveloperAcademy-POSTECH/2024-NC2-M44-Notification/assets/77305722/7ee52c02-8fb1-4a8d-86d2-dcdad94f0124

### Local notification

ํ•™์ƒ์ด ์ง€์ •ํ•œ ์‹œ๊ฐ„์— ์ผ๊ธฐ ์ž‘์„ฑ ์•Œ๋ฆผ์„ ๋ฐ›๋Š”๋‹ค. ๊พน ๋ˆŒ๋Ÿฌ์„œ ์•ฑ์— ๋“ค์–ด๊ฐ€์ง€ ์•Š๊ณ ๋„ ์ผ๊ธฐ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

https://github.com/DeveloperAcademy-POSTECH/2024-NC2-M44-Notification/assets/77305722/d6b8c9c0-f0ab-48da-90da-2cec1bd7aa2a

### Remote notification

ํ•™์ƒ์ด ์ผ๊ธฐ๋ฅผ ์ œ์ถœํ•˜๋ฉด ์„ ์ƒ๋‹˜์—๊ฒŒ ์ผ๊ธฐ์˜ ๋‚ด์šฉ์ด ๋‹ด๊ธด ์•Œ๋ฆผ์ด ์ „์†ก๋œ๋‹ค.

https://github.com/DeveloperAcademy-POSTECH/2024-NC2-M44-Notification/assets/77305722/c049a275-4fc9-452a-babd-0d2bc425f874

## ๐ŸŽฅ Youtube Link
(์ถ”ํ›„ ๋งŒ๋“ค์–ด์ง„ ์œ ํŠœ๋ธŒ ๋งํฌ ์ถ”๊ฐ€)

## ๐Ÿ’ก About Notification

### Local Notification

์‹œ๊ฐ„, ๊ณต๊ฐ„์ด๋ผ๋Š” trigger๋ฅผ ์ด์šฉํ•˜์—ฌ ๋””๋ฐ”์ด์Šค๋กœ ์•Œ๋ฆผ์„ ๋ณด๋‚ธ๋‹ค. notification์„ ํ†ตํ•ด ์‚ฌ์šฉ์ž์—๊ฒŒ ์ ์ ˆํ•œ ๋•Œ์— ์ ์ ˆํ•œ ํ˜•ํƒœ ์•Œ๋ฆผ์„ ์ œ๊ณตํ•จ์œผ๋กœ์จ ๋‹จ์ˆœํžˆ ๋ฆฌ๋งˆ์ธ๋“œ๊ฐ€ ์•„๋‹Œ ํŠน์ • ํ–‰๋™์„ ๊ถŒ์œ ํ•  ์ˆ˜ ์žˆ๋‹ค.

### Remote Notification

์›๊ฒฉ์—์„œ ํŠน์ • ํŠธ๋ฆฌ๊ฑฐ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์•Œ๋ฆผ์„ ๋ฐœ์ƒ์‹œํ‚ค๋Š” ๊ธฐ์ˆ ์ด๋‹ค.

## ๐ŸŽฏ What we focus on?

- **Trigger**: Local Notification์˜ ๊ฒฝ์šฐ ์‹œ๊ฐ„, ๊ณต๊ฐ„์„ Trigger ๋กœ ์‚ฌ์šฉํ•˜์—ฌ ์•Œ๋ฆผ์„ ์ค„ ์ˆ˜ ์žˆ๋‹ค. ๊ทธ ์ค‘ ์‹œ๊ฐ„์ด ๊ณ„ํš๊ณผ ๋ฐ€์ ‘ํ•˜๊ธฐ๋„ ํ•˜๊ณ , ์‹œ๊ฐ„์ด๋”๋ผ๋„ ํƒ€์ด๋จธ, ํŠน์ • ๋‚ ์งœ, ํŠน์ • ์‹œ๊ฐ„ ๋“ฑ์— ๋”ฐ๋ผ ๋‹ฌ๋ฆฌ ์•Œ๋žŒ์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์–ด์„œ ์•ฑ์˜ use case์˜ ์ ์ ˆํ•œ ์•Œ๋ฆผ์„ ๊ตฌํ˜„ํ•˜๊ณ ์ž ํ–ˆ๋‹ค.
- **Notification Action**: ์•Œ๋ฆผ์„ ๋ฐ›๊ณ  ๋‹จ์ˆœํžˆ ์•ฑ์œผ๋กœ ์ ‘๊ทผํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ์•ฑ์— ๋“ค์–ด์˜ค์ง€ ์•Š์•„๋„ ๋ฏธ๋ฆฌ ์„ค์ •ํ•œ ๊ณผ์ •์„ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜์—ฌ ์‚ฌ์šฉ์ž์˜ ํŽธ๋ฆฌ์„ฑ์„ ๋†’์ด๊ณ  ๋ชฉ์ ์— ๋ถ€ํ•ฉํ•˜๋Š” ์•Œ๋ฆผ์„ ๋ณด๋‚ด๊ณ ์ž ํ–ˆ๋‹ค.
- **Text Input Action:** Notification action ์ค‘์— ๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋Š” ๊ฒƒ์—์„œ๋งŒ ๋๋‚˜๋Š” ๊ฒŒ ์•„๋‹ˆ๋ผ ๋ฐ”๋กœ ํ•„์š”ํ•œ ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์œผ๋กœ, use case์— ์ ์ ˆํ•œ ์•Œ๋ฆผ์„ ๊ตฌํ˜„ํ•˜๊ณ ์ž ํ–ˆ๋‹ค.
- **Remote Notification** ์˜ ๊ฒฝ์šฐ ์„œ๋ฒ„์—์„œ ์›ํ•˜๋Š” ๋Œ€๋กœ trigger๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. ์‚ฌ์šฉ์ž ์ƒํ˜ธ์ž‘์šฉ์— ๋งž์ถฐ ์•Œ๋ฆผ์„ ์ „์†กํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ•„์ˆ˜์ด๋‹ค. ํ‘ธ์‹œ ์•Œ๋ฆผ์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” APNs ๋ฅผ ํฌํ•จํ•œ ์—ฌ๋Ÿฌ ๋‹จ๊ณ„๋ฅผ ๊ฑฐ์ณ์•ผ ํ•˜๋Š”๋ฐ, ํ•ด๋‹น ์ธํ”„๋ผ๋ฅผ ๊ตฌ์ถ•ํ•˜๋Š” ๋ฒ•์— ๋Œ€ํ•ด ์ง‘์ค‘ํ–ˆ๋‹ค.

## ๐Ÿ’ผ Use Case

> ์ ์ ˆํ•œ ์•Œ๋ฆผ์„ ์ฃผ์–ด ํ•™์ƒ์€ ์ผ์ •ํ•œ ์‹œ๊ฐ„์— ์ผ๊ธฐ๋ฅผ ์ž‘์„ฑํ•˜๊ณ , ์„ ์ƒ๋‹˜์€ ํ•™์ƒ์ด ์ œ์ถœํ•œ ์ผ๊ธฐ๋ฅผ ํ™•์ธํ•˜๊ฒŒ ํ•˜์ž.

## ๐Ÿ› ๏ธ About Code

### Local Notification

---

**์‹œ๊ฐ„์„ trigger๋กœ ์•Œ๋ฆผ ์„ค์ •ํ•˜๊ธฐ**

```swift
let dateComponents = Calendar.current.dateComponents([.hour, .minute], from: date)
trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
```

**๋ณด๋‚ด๋Š” ์•Œ๋ฆผ์˜ category ์„ค์ •ํ•˜๊ธฐ**

```swift
let textAction = UNTextInputNotificationAction(
identifier: "TEXT_ACTION",
title: "๊ฐ„๋‹จํ•˜๊ฒŒ ์ผ๊ธฐ ์“ฐ๊ธฐ",
options: [],
textInputButtonTitle: "์ œ์ถœ",
textInputPlaceholder: "์ผ๊ธฐ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”"
)

let basicNotificationCategory = UNNotificationCategory(
identifier: "STUDENT_NOTIFICATION",
actions: [textAction, acceptAction],
intentIdentifiers: [],
hiddenPreviewsBodyPlaceholder: "์ผ๊ธฐ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”",
options: .customDismissAction
)
```

**์ˆ˜์‹ ํ•œ ์•Œ๋ฆผ์˜ category์— ๋”ฐ๋ผ action ์„ค์ •ํ•˜๊ธฐ**

```swift
func userNotificationCenter(
_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void
) {
if response.notification.request.content.categoryIdentifier == "STUDENT_NOTIFICATION" {
switch response.actionIdentifier {
case "TEXT_ACTION":
print("didReceive: text inputโ€)
if let userInput = (response as? UNTextInputNotificationResponse)?.userText {
print(userInput)
UserDefaults.standard.set(userInput, forKey: "diary")
UserDefaults.standard.set(true, forKey: "isDiarySubmitted")
// โ€ฆ
```

### Remote Notification

---

**APNs ๋“ฑ๋ก ํ›„ ๋””๋ฐ”์ด์Šค ํ† ํฐ ์š”์ฒญ**

```swift
application.registerForRemoteNotifications()
```

**๋””๋ฐ”์ด์Šค ํ† ํฐ DB ์„œ๋ฒ„์— ์ €์žฅ**

```swift
private func saveUserTokenToFirestore(userId: String, token: String) {
db.collection("users").document(userId).
setData(["fcmToken": token], merge: true) { error in
// ..
}
}
```

**FCM ํ† ํฐ ์ƒ์„ฑ**

```swift
func messaging(_ messaging: Messaging,
didReceiveRegistrationToken fcmToken: String?) {
let dataDict: [String: String] = ["token": fcmToken ?? ""]
NotificationCenter.default.post(
name: Notification.Name("FCMToken"),
object: nil,
userInfo: dataDict
)
}
```

**Firebase Functions ํ˜ธ์ถœ**

```swift
func sendNotificationToUser
(userId: String,title: String, body: String) {
let data: [String: Any] = [
"userId": userId,
"title": title,
"body": body
]
functions.httpsCallable("sendNotification").call(data)
{ result, error in
//..
}
}
```

**Firebase Function ์—์„œ ์•Œ๋ฆผ ์ „์†ก (JavaScript)**

```jsx
exports.sendNotification = onCall(async (data, context) => {
const { userId, title, body } = data;
//..
const userDoc = await getFirestore().
collection('users').doc(userId).get();
const fcmToken = userDoc.data().fcmToken;
const message = {
notification: {
title: title,
body: body
},
token: fcmToken
};
await admin.messaging().send(message);
//..
}
}
```

## :people_hugging: Authors

|||
|:-:|:-:|
|Groo
[@treesofgroo](https://github.com/treesofgroo)|์ด์ƒํ˜„ Ace
[@dgh06175](https://github.com/dgh06175)|