๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŽ iOS

[Swift] push notification ์ •๋ฆฌ (with Firebase) permission๊ณผ ํ‘ธ์‹œ ๋ฐ›์„ ๋•Œ ํ˜ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜

by ํ‹ด๋”” 2021. 10. 27.
728x90
๋ฐ˜์‘ํ˜•

์นด์นด์˜คํ†ก์„ ์‹คํ–‰์‹œํ‚ค์ง€ ์•Š์€ ์ƒํƒœ์—์„œ ์ƒ๋Œ€๋ฐฉ์œผ๋กœ ๋ถ€ํ„ฐ ๋ฉ”์‹œ์ง€๋ฅผ ๋ฐ›์„ ๋•Œ ์˜ค๋Š” ์•Œ๋žŒ, ํ˜น์€ ์•ฑ์—์„œ ๊ด‘๊ณ ๋ฅผ ๋ณด๋‚ด๊ธฐ ์œ„ํ•ด์„œ ๋“ฑ๋“ฑ

๋งŽ์€ ๊ณณ์—์„œ push notification์ด ์‚ฌ์šฉ๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹น

๊ฑฐ์˜ ํ”„๋กœ์ ํŠธ ํ•  ๋•Œ๋งˆ๋‹ค ํ•„์ˆ˜๋กœ ๊ตฌํ˜„ํ•˜๊ธฐ๋„ ํ•˜๊ณ  ์ฒ˜์Œ์— ํ•˜๋‹ค๋ณด๋ฉด ๊ฝค๋‚˜ ๋ณต์žกํ•ด์„œ ์ด๋ฒˆ ๊ธฐํšŒ์— ์ •๋ฆฌํ•ด ๋ณด๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹น 

๐Ÿฆ‘๐Ÿฆ‘๐Ÿฆ‘

 

push notification์ด๋ž€

Apple Push Notification service = APNs๋ฅผ ์ด์šฉํ•ด์„œ

์•ฑ์œผ๋กœ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋‚ด๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹น

 

์•ฑ์ด ์‹คํ–‰๋˜์ง€ ์•Š์€ ์ƒํƒœ๋‚˜ ๋ฐฑ๊ทธ๋ผ์šด๋“œ์— ์ž ๋“ค์–ด ์žˆ๋Š” ์ƒํƒœ์—์„œ๋„

์‚ฌ์šฉ์ž์—๊ฒŒ ํ‘ธ์‹œ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋‚ด ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹น

 

โญ
๋‹น๊ทผ~ ํ˜น์€ ์นดํ†ก! ํ•˜๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ์›ํ•˜๋Š” ์‚ฌ์šด๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ณ  ์งง์€ ํ…์ŠคํŠธ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋‚ผ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค

๋˜ํ•œ ์•ฑ ์•„์ด์ฝ˜ ์˜†์— ๋œจ๋Š” ๋ฑƒ์ง€์˜ ๊ฐฏ์ˆ˜๋ฅผ ์„ธํŒ…ํ•ด ์ค„ ์ˆ˜ ์žˆ์œผ๋ฉฐ ๋ฏธ๋””์–ด ์ฒจ๋ถ€๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค

 

 

๋กœ์ปฌ๋กœ push notification์„ ๋ณด๋‚ผ ์ˆ˜ ์žˆ์ง€๋งŒ ๋Œ€๋ถ€๋ถ„ ์„œ๋ฒ„์—์„œ ํ‘ธ์‹œ๋ฅผ ์‚ฌ์šฉ์ž์—๊ฒŒ

๋ณด๋‚ด๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋Š”๋ฐ ์ด๋•Œ firebase๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹น

firebase์™€ ์ด์™€ ๊ด€๋ จ๋œ ๊ธฐ๋Šฅ์€ ๋‹ค๋ฅธ ํฌ์ŠคํŒ…์—์„œ ํ•˜๋„๋ก ํ•˜๊ณ  ์ด๋ฒˆ์—๋Š”

์ด push notification ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ฝ”๋“œ ์ƒ์—์„œ ํ•ด์ค˜์•ผ ํ•  ๊ฒƒ์— ๋Œ€ํ•ด์„œ ์ •๋ฆฌํ•ด ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹น


๐ŸŽ ์‚ฌ์šฉ์ž๋กœ ๋ถ€ํ„ฐ permission ์–ป์–ด์˜ค๊ธฐ

์‚ฌ์šฉ์ž์—๊ฒŒ ํ‘ธ์‹œ ๋…ธํ‹ฐํ”ผ์ผ€์ด์…˜์„ ๋ณด๋‚ด๊ธฐ ์œ„ํ•ด์„œ ์šฐ์„ ์ ์œผ๋กœ permission์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค!

์‚ฌ์šฉ์ž๊ฐ€ allow ํ•˜๋ฉด ๊ทธ๋•Œ ํ‘ธ์‹œ๋ฅผ ๋ณด๋‚ด๊ณ  ์‚ฌ์šด๋“œ๊ฐ€ ์žˆ๋Š” ํ‘ธ์‹œ๋ฅผ ์„ค์ •ํ•˜๊ณ  ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹น

import UserNotifications

func requestPermission() {
	UNUserNotificationCenter.current().delegate = self
    let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
    UNUserNotificationCenter.current().requestAuthorization(options: authOptions) { didAllow, error in
          print(didAllow)     
	}
        
        UIApplication.shared.registerForRemoteNotifications()
}

options ์—๋Š” ๋‹ค์–‘ํ•œ ์„ค์ •์ด ๋“ค์–ด ๊ฐˆ ์ˆ˜ ์žˆ์œผ๋‚˜ ์ฃผ๋กœ 

์•ฑ ์•„์ด์ฝ˜์˜ ์˜ค๋ฅธ์ชฝ ๋ชจ์„œ๋ฆฌ์— ์ˆซ์ž ๋ฑƒ์ง€ ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•œ .badge

 ๊ทธ๋ฆฌ๊ณ  ํ‘ธ์‹œ ๋ฐ›์„ ๋•Œ ์‚ฌ์šด๋“œ ๋ฐœ์ƒ์„ ์œ„ํ•œ .sound

๋…ธํ‹ฐํ”ผ์ผ€์ด์…˜์„ ๋ณด์—ฌ์ค„ .alert

์ด๋ ‡๊ฒŒ ์ง€์ •ํ•ด ์ค๋‹ˆ๋‹น ์ด ๋ฐ–์—๋„ .carPlay, .provisional, .providesAppNotificationSettings ๋“ฑ์ด ์žˆ์Šต๋‹ˆ๋‹น

 

AppDelegate์—์„œ ํ˜ธ์ถœ ํ•  ์ˆ˜๋„ ์žˆ์œผ๋‚˜ 

ํ•„์š”์— ๋”ฐ๋ผ ํ‘ธ์‹œ ๋…ธํ‹ฐํ”ผ์ผ€์ด์…˜์ด ํ•„์š”ํ•œ ํ™”๋ฉด์—์„œ ๊ตฌํ˜„ํ•ด๋„ ๋ฌด๋ฐฉํ•ฉ๋‹ˆ๋‹ค

์ €๋Š” ์ฃผ๋กœ ์Šคํ”Œ๋ž˜์‰ฌ๊ฐ€ ๋๋‚œ ํ›„ ๋ฉ”์ธ ์˜์—ญ์—์„œ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹น


์‚ฌ์šฉ์ž๊ฐ€ Allow๋ฅผ ํ•ด์„œ ํ—ˆ์šฉํ•œ ํผ๋ฏธ์…˜์€ ์–ผ๋งˆ๋“ ์ง€ ์„ค์ •์—์„œ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—

์•ฑ ๋‚ด๋ถ€์— ์„ธํŒ…ํ™”๋ฉด์ด๋‚˜ ํ‘ธ์‹œ๋ฅผ ํ•„์š”๋กœ ํ•˜๋Š” ์˜์—ญ์— 

์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ถ”์ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹น

let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
	UNUserNotificationCenter.current().requestAuthorization(options: authOptions, completionHandler: { didAllow, error in

	DispatchQueue.main.async {
    }
})

ํด๋กœ์ € ๋‚ด๋ถ€์—์„œ guard didAllow else {return} ๋“ฑ์œผ๋กœ ์‚ฌ์šฉ์ž์˜ ํผ๋ฏธ์…˜ ์„ค์ •์„ ํ™•์ธํ•˜๊ณ 

ํ—ˆ์šฉ ๋˜์—ˆ์„ ๋•Œ ์ˆ˜ํ–‰ํ•  ์„ค์ •์ด๋‚˜ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด ์ค„ ์ˆ˜ ์žˆ์–ด์š”!

 


๐ŸŽ Callback method

์•ฑ์ด ํ‘ธ์‹œ ๋…ธํ‹ฐํ”ผ์ผ€์ด์…˜์„ ๋ฐ›์œผ๋ฉด iOS์—์„œ๋Š”  UIApplicationDelegate ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœ ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค

์ด๋•Œ ํ˜ธ์ถœ๋˜๋Š” ๋ฉ”์†Œ๋“œ๋Š” ์•ฑ์˜ ์ƒํƒœ์— ๋”ฐ๋ผ ๋‹ค๋ฅด๊ฒŒ ์ฒ˜๋ฆฌ ๋ฉ๋‹ˆ๋‹ค

 

์šฐ์„  ๋Ÿฐ์น˜ ์‹œ์  ์ž…๋‹ˆ๋‹ค

์•ฑ์ด ์‹คํ–‰ ๋˜์ง€ ์•Š์€ ์ƒํƒœ์—์„œ ์‚ฌ์šฉ์ž๊ฐ€ ํ‘ธ์‹œ๋ฅผ ๋ˆŒ๋Ÿฌ์„œ ์•ฑ์„ ์‹คํ–‰์‹œํ‚จ ๊ฒฝ์šฐ

AppDelegate์— didFinishLaunchingWithOptions  ์ด ์‹คํ–‰๋ ๋•Œ

lunchOption์—์„œ ํ‘ธ์‹œ ๋ฐ›์€ ๋‚ด์šฉ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹น

 

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
	if let options = launchOptions {
    	if let remoteNotification = options[UIApplication.LaunchOptionsKey.remoteNotification] as? [AnyHashable: Any] {
    	// remoteNotification์— ํ‘ธ์‹œ์—์„œ ๋ฐ›์€ ๋‚ด์šฉ์ด ๋“ค์–ด ์žˆ์Œ
    	}
    }
    
    FirebaseApp.configure()
    Messaging.messaging().delegate = self
    
    return true
    }

launchOptions์—

UIApplication.LaunchOptionsKey.remoteNotification ์—์„œ

ํ‘ธ์‹œ ๋ฐ์ดํ„ฐ๋ฅผ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค

 

๊ทธ ๋‹ค์Œ์œผ๋กœ๋Š” Background ์ž…๋‹ˆ๋‹น

์•ฑ์ด ํ™”๋ฉด์— ๋ณด์ด๋Š” ๊ฒƒ์€ ์•„๋‹ˆ์ง€๋งŒ ์•„์ง ๋ฐฑ๊ทธ๋ผ์šด๋“œ์—์„œ ์‹คํ–‰ ์ค‘์ธ ๊ฒฝ์šฐ

์›ํ•˜๋Š” ํด๋ž˜์Šค์—์„œ 

UNUserNotificationCenterDelegate

๋ฅผ ์ฑ„ํƒํ•˜์—ฌ ์•„๋ž˜ ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•ด ์ค๋‹ˆ๋‹น

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
	let userInfo = response.notification.request.content.userInfo
}

๋ฐฑ๊ทธ๋ผ์šด๋“œ ์ƒํƒœ์—์„œ ํ‘ธ์‹œ๋ฅผ ๋ˆ„๋ฅธ ๊ฒฝ์šฐ ํ•ด๋‹น ๋ฉ”์†Œ๋“œ๋ฅผ ํƒ€๊ฒŒ ๋ฉ๋‹ˆ๋‹น

์ด๋•Œ response์— notification.request.content.userInfo ์— ํ‘ธ์‹œ ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด ์žˆ์Šต๋‹ˆ๋‹น!

 

 

Foreground๋Š” ์•ฑ์ด ์‹คํ–‰ ์ค‘์ผ ๋•Œ

ํ˜„์žฌ ํ™”๋ฉด์—์„œ ์•ฑ์„ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ์„ ๋•Œ ํ‘ธ์‹œ๋ฅผ ์„ค์ •ํ•ด ์ค๋‹ˆ๋‹ค!

์ด ๋ถ€๋ถ„์„ ๊ตฌํ˜„ํ•˜์ง€ ์•Š์œผ๋ฉด ์•ฑ์„ ์‚ฌ์šฉ ์ค‘์ผ ๋•Œ ํ‘ธ์‹œ๊ฐ€ ๋ณด์ด์ง€ ์•Š๊ฑฐ๋‚˜

๋ฑƒ์ง€๊ฐ€ ์นด์šดํŒ… ๋˜์ง€ ์•Š๊ฑฐ๋‚˜ ์†Œ๋ฆฌ๊ฐ€ ์•ˆ๋‚˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        let userInfo = notification.request.content.userInfo
        completionHandler([.alert, .badge, .sound])
}

์ด๋•Œ ํ‘ธ์‹œ ๋ฐ์ดํ„ฐ๋Š” notification.request.content.userInfo์— ๋“ค์–ด ์žˆ์–ด์š”!

 

๋งŒ์•ฝ ์•ฑ์„ ์‹คํ–‰ํ•˜๊ณ  ์‚ฌ์šฉ ์ค‘์ผ  ๋•Œ (Foreground) ์ƒํƒœ์ผ ๋•Œ ํ‘ธ์‹œ๊ฐ€ ์˜ค๊ณ 

์‚ฌ์šฉ์ž๊ฐ€ ํ‘ธ์‹œ๋ฅผ ๋ˆ„๋ฅด๊ฒŒ ๋˜๋ฉด

willPresent๋ฅผ ํƒ€๋ฉด์„œ ์„ค์ •ํ•œ ์˜ต์…˜๋Œ€๋กœ ํ‘ธ์‹œ๊ฐ€ ์‚ฌ์šฉ์ž์—๊ฒŒ ์ œ๊ณต๋˜๊ณ 

๊ทธ ํ›„์— didReceive๋ฅผ ํƒ€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค!

 


ํ‹€๋ฆฌ๊ฑฐ๋‚˜ ์ž˜ ๋ชป ๋œ ์ ์ด ์žˆ๋‹ค๋ฉด ๊ผญ ๋Œ“๊ธ€ ๋ถ€ํƒ๋“œ๋ ค์š”!!!

๐Ÿฆ‘๐Ÿ’จ๐Ÿ’จ๐Ÿ’จ๐Ÿ’จ

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€