iOS, User Notifications - Notification Requests

iDOยท2022๋…„ 1์›” 7์ผ
0

iOS

๋ชฉ๋ก ๋ณด๊ธฐ
7/8
post-thumbnail

๐Ÿง ํ‘ธ์‰ฌ์•Œ๋ฆผ์„ ์‚ฌ์šฉํ•ด์•ผ ํ• ์ผ์ด ์ƒ๊ฒจ์„œ ์ด๋ฒˆ์— ์ •๋ฆฌํ•˜๊ฒŒ ๋˜์—ˆ๋Š”๋ฐ, ์šฐ์„  ๋กœ์ปฌ์—์„œ ๋ฐœ์ƒํ•˜๋Š” ํ‘ธ์‰ฌ์•Œ๋ฆผ์„ ์„ค์ •ํ•ด๋ดค์Šต๋‹ˆ๋‹ค.

Step 1

์‚ฌ์šฉ์ž์—๊ฒŒ ์•Œ๋ฆผ ์‚ฌ์šฉ์— ๋Œ€ํ•œ ํ—ˆ์šฉ์„ ๋ฐ›์•„์•ผํ•ฉ๋‹ˆ๋‹ค. ๊ถŒํ•œ์„ ๋ฐ›๊ธฐ ์œ„ํ•ด UNUserNotificationCenter ์ธ์Šคํ„ด์Šค๋ฅผ ํ†ตํ•ด requestAuthorization(options:completionHandler:) ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ์‹œ๋‹ค.

โ„๏ธ ViewController.swift

...
import UserNotifications

class ViewController: UIViewController {
	override func viewDidLoad() {
        super.viewDidLoad()
        let center = UNUserNotificationCenter.current()
        center.requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
        
            if let error = error {
            print("\(Error: \(error.localizedDescription))")
            }
            granted ? print("Allow") : print("NotAllow")
        
        }
    }
}
Step 2

์•Œ๋ฆผ ๋‚ด์šฉ์„ ์ƒ์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. UNMutableNotificationContent์„ ์ด์šฉํ•˜์—ฌ ๋‚ด์šฉ์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

โ„๏ธ ViewController.swift

...
class ViewController: UIViewController {

	...
    
    private lazy var notificationContent: UNMutableNotificationContent = {
        let content = UNMutableNotificationContent()
        content.title = "Hello Notification"
        content.body = "Study APNs with Apple developer doc"
        return content
    }()
}
Step 3

์•Œ๋ฆผ์„ ์ œ๊ณตํ•˜๊ธฐ์œ„ํ•œ ์กฐ๊ฑด์„ ์œ„ํ•ด ํŠธ๋ฆฌ๊ฑฐ(๋ฐฉ์•„์‡ )๋ฅผ ์ƒ์„ฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. UNCalendarNotificationTrigger, UNTimeIntervalNotificationTrigger, ๋˜๋Š” UNLocationNotificationTrigger ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŠธ๋ฆฌ๊ฑฐ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์•„๋ž˜์—์„œ๋Š” UNTimeIntervalNotificationTrigger๋ฅผ ์ด์šฉํ•˜์—ฌ ์ƒ์„ฑํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

โ„๏ธ ViewController.swift

...
class ViewController: UIViewController {

	...
    
	private let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 3, repeats: false)
}
Step 4

์œ„์—์„œ ์ƒ์„ฑํ•œ ๋‚ด์šฉ ๋ฐ ํŠธ๋ฆฌ๊ฑฐ๋ฅผ ํฌํ•จํ•œ UNNotificationRequest ์š”์ฒญ์„ ๋งŒ๋“ค๊ณ  ์‹œ์Šคํ…œ์— ์ƒ์„ฑํ•œ ์š”์ฒญ์„ ์˜ˆ์•ฝํ•˜๊ธฐ ์œ„ํ•œ add(_:withCompletionHandler:) ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜์—์„œ๋Š” ๋ฒ„ํŠผ์„ ์ถ”๊ฐ€ํ•ด์„œ ๋ฒ„ํŠผ ์•ก์…˜์— ์š”์ฒญํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋ฅผ ๋งŒ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค.

โ„๏ธ ViewController.swift

...
class ViewController: UIViewController {
	
    ...
    
    @IBAction func pushNoti(_ sender: UIButton) {
        // Create the request
        let uuidString = UUID().uuidString
        let request = UNNotificationRequest(identifier: uuidString,
                                            content: notificationContent, trigger: trigger)
        
        // Schedule the request with the system.
        let notificationCenter = UNUserNotificationCenter.current()
        notificationCenter.add(request) { (error) in
            if error != nil {
                // Handle any errors.
            }
        }
    }
}

๐Ÿง(์•„๋ž˜ ์ฐธ๊ณ ๋‚ด์šฉ์„ ํ†ตํ•ด ๋” ์ž์„ธํ•œ ์ •๋ณด๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!)
Apple Developer - Permission
Apple Developer - ReqNotification Locally

profile
์ด๊ณณ์€ ์ €๋ฅผ ์œ„ํ•œ iOS ์„ค๋ช…์„œ์ž…๋‹ˆ๋‹ค.

0๊ฐœ์˜ ๋Œ“๊ธ€