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-a41-notification


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

Last synced: about 10 hours ago
JSON representation

Awesome Lists containing this project

README

        

# 2024-NC2-A41-Notification

## ๐ŸŽฅ Youtube Link

(to be continued)


## ๐Ÿ’ก About Notification

image

> Push Notification ์„ ์ค„ ์ˆ˜ ์žˆ๋Š” ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•

- local โ†’ foreground ์ผ ๋•Œ ์•Œ๋ฆผ ์•ˆ ๋œธ
- APNS โ†’ local๋ณด๋‹ค ํ›จ์”ฌ ๋‹ค์–‘ํ•œ ์•Œ๋ฆผ ์‚ฌ์šฉ ๊ฐ€๋Šฅ


### ์ œ์•ฝ์‚ฌํ•ญ

- APNS - ๊ฐœ๋ฐœ์ž ๊ณ„์ • ์žˆ์–ด์•ผ ํ•จ ! `ํ•„์ˆ˜ !`
>

### Tip
image




## ๐ŸŽฏ What we focus on?
image




## ๐Ÿ’ผ Use Case
image




## ๐Ÿ–ผ๏ธ Prototype

image

https://github.com/DeveloperAcademy-POSTECH/2024-NC2-A41-Notification/assets/62278377/966a1aba-6fde-4f8c-bd4e-a142937ce14f


## ๐Ÿ› ๏ธ About Code

โœ…ย ๋ฒ„ํŠผ ์•ก์…˜ ์ž‘์„ฑ ๋ฐ ์•Œ๋ฆผ ์„ผํ„ฐ ๋“ฑ๋ก
โœ…ย ์ ์ ˆํ•œ ์ปจํ…์ธ ๋ฅผ ์ฑ„์›Œ๋„ฃ์€ ์•Œ๋ฆผ์„ ์‚ฌ์šฉ์ž์—๊ฒŒ ์ „์†กํ•˜๊ธฐ
โœ…ย ์‚ฌ์šฉ์ž์—๊ฒŒ ํ‘ธ์‹œ ์•Œ๋ฆผ ๊ถŒํ•œ ์š”์ฒญํ•˜๊ธฐ

---

- ํ”„๋กœ์ ํŠธ์— notification content extension ์ถ”๊ฐ€
- `์•Œ๋ฆผ ์•ก์…˜๊ณผ ์ปค์Šคํ…€ ์นดํ…Œ๊ณ ๋ฆฌ๋ฅผ ์„ค์ •`ํ•ด์„œ ์•Œ๋ฆผ ์„ผํ„ฐ์— ๋“ฑ๋ก

---


```swift
import UIKit
import UserNotifications

class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
let center = UNUserNotificationCenter.current()
center.delegate = self

// MARK: - ํ‘ธ์‹œ ์•Œ๋ฆผ ๊ถŒํ•œ ์š”์ฒญ
center.requestAuthorization(options: [.alert, .badge, .sound]) { granted, error in
if let error = error {
print("Notification authorization error: \(error)")
}
print("Notification permission granted: \(granted)")
}
application.registerForRemoteNotifications() // ์›๊ฒฉ ์•Œ๋ฆผ ๋“ฑ๋ก
return true

//MARK: - ์ปค์Šคํ…€ notification์„ ์œ„ํ•œ ๋ถ€๋ถ„
// ์•Œ๋ฆผ ์•ก์…˜ ์„ค์ •
let doneAction = UNNotificationAction(identifier: "doneAction", title: "Done", options: [.destructive])
let openAction = UNNotificationAction(identifier: "openAction", title: "Open CooKoo", options: [.foreground])

// ์•Œ๋ฆผ ์นดํ…Œ๊ณ ๋ฆฌ ์„ค์ •
let customCategory = UNNotificationCategory(identifier: "customNotificationCategory",
actions: [openAction, doneAction],
intentIdentifiers: [],
options: [.customDismissAction])
// ์นดํ…Œ๊ณ ๋ฆฌ ๋“ฑ๋ก
center.setNotificationCategories([customCategory])
}
}
```


## โœ…ย ๋ฒ„ํŠผ ์•ก์…˜ ์ž‘์„ฑ ๋ฐ ์•Œ๋ฆผ ์„ผํ„ฐ ๋“ฑ๋ก

---

```swift
// ์•ฑ์ด foreground์— ์žˆ์„ ๋•Œ ํ‘ธ์‹œ ์•Œ๋ฆผ ํ˜ธ์ถœ
extension AppDelegate: UNUserNotificationCenterDelegate {

// ์•Œ๋ฆผ ์ˆ˜์‹  ์‹œ ํ˜ธ์ถœ๋˜๋Š” ๋ถ€๋ถ„
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.banner, .sound, .badge, .list])
}

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse,
withCompletionHandler completionHandler: @escaping () -> Void) {
if response.actionIdentifier == "doneAction" {
// Done ์•ก์…˜ ์ฒ˜๋ฆฌ
print("Done action triggered")
}
else if response.actionIdentifier == "openAction" {
// ์•ฑ ์˜คํ”ˆ ์•ก์…˜ ์ฒ˜๋ฆฌ
print("Open action triggered")
}
completionHandler()
}
}

```


## โœ…ย ์ ์ ˆํ•œ ์ปจํ…์ธ ๋ฅผ ์ฑ„์›Œ๋„ฃ์€ ์•Œ๋ฆผ์„ ์‚ฌ์šฉ์ž์—๊ฒŒ ์ „์†กํ•˜๊ธฐ

---

- **NotificationViewController**์™€ **MainInterface**์—์„œ ํ™•์žฅ๋œ `์•Œ๋ฆผ์˜ UI`๋ฅผ ๊พธ๋ฐ€ ์ˆ˜ ์žˆ์Œ
- ์•Œ๋ฆผ์„ ๋ณด๋‚ผ ๋•Œ `์ œ๋ชฉ, ์นดํ…Œ๊ณ ๋ฆฌ, ๋‚ด์šฉ` ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ํ•จ๊ป˜ ์šธ๋ฆด `์‚ฌ์šด๋“œ`๊นŒ์ง€ ์ถ”๊ฐ€ํ•ด์„œ ์š”์ฒญ์„ ์ƒ์„ฑํ•˜๋ฉด ์•Œ๋ฆผ ์„ผํ„ฐ์— ์ปค์Šคํ…€๋œ ์•Œ๋ฆผ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Œ

```swift
// MARK: - ์ปค์Šคํ…€ ํ‘ธ์‹œ ์•Œ๋ฆผ์„ ๋ณด๋‚ด๋Š” ํ•จ์ˆ˜
func sendNotification() {
let content = UNMutableNotificationContent()

// MARK: ์•Œ๋ฆผ ๋‚ด์šฉ์„ ์„ค์ •ํ•˜๋Š” ๋ถ€๋ถ„
content.title = "CooKoo"
content.categoryIdentifier = "customNotificationCategory"

// MARK: selectedKeyword์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ๋ฌธ๊ตฌ ์„ค์ •
switch selectedKeyword {
case .cook:
content.body = "Hey! Cooking's up!"
case .study:
content.body = "Hey! Studying's up!"
case .exercise:
content.body = "Hey! Workout's up!"
case .laundry:
content.body = "Hey! Laundry's up!"
}

// MARK: ์•Œ๋ฆผ๊ณผ ํ•จ๊ป˜ ์šธ๋ฆด ์‚ฌ์šด๋“œ ์ „์†ก
content.sound = UNNotificationSound.defaultRingtone

// ์•Œ๋ฆผ์˜ ํŠธ๋ฆฌ๊ฑฐ ๋ฐœ์†ก
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 0.1, repeats: false)
// ์š”์ฒญ ์ƒ์„ฑ
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)

// MARK: ์š”์ฒญ์„ ์•Œ๋ฆผ ์„ผํ„ฐ์— ์ถ”๊ฐ€
UNUserNotificationCenter.current().add(request) { error in
if let error = error {
print("Error adding notification: \(error)")
}
}

// MARK: ์ž‘๋™ ์ค‘์ด๋˜ Live activity ์ข…๋ฃŒ
LiveActivityManager().endActivity()
}
```