Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/developeracademy-postech/2024-nc2-a41-notification
- Owner: DeveloperAcademy-POSTECH
- Created: 2024-06-16T04:46:34.000Z (5 months ago)
- Default Branch: main
- Last Pushed: 2024-06-20T19:02:28.000Z (5 months ago)
- Last Synced: 2024-06-22T09:15:46.123Z (5 months ago)
- Language: Swift
- Size: 3.95 MB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 2024-NC2-A41-Notification
## ๐ฅ Youtube Link
(to be continued)
## ๐ก About Notification
> Push Notification ์ ์ค ์ ์๋ ๋ ๊ฐ์ง ๋ฐฉ๋ฒ
- local โ foreground ์ผ ๋ ์๋ฆผ ์ ๋ธ
- APNS โ local๋ณด๋ค ํจ์ฌ ๋ค์ํ ์๋ฆผ ์ฌ์ฉ ๊ฐ๋ฅ
### ์ ์ฝ์ฌํญ
- APNS - ๊ฐ๋ฐ์ ๊ณ์ ์์ด์ผ ํจ ! `ํ์ !`
>### Tip
## ๐ฏ What we focus on?
## ๐ผ Use Case
## ๐ผ๏ธ Prototype
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 UserNotificationsclass 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()
}
```