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

APNS, Apple Push Notification Service

by yongmin.Lee 2021. 12. 6.
  1. APNS๋ž€?
  2. ์ž‘๋™์›๋ฆฌ
  3. Push notification ์„ค์ •

APNS๋ž€?

  • third party ๊ฐœ๋ฐœ์ž๊ฐ€ ์šฐ๋ฆฌ ์•ฑ์— ํ‘ธ์‹œ ์•Œ๋žŒ์„ ๋ณด๋‚ผ ์ˆ˜ ์žˆ๋„๋ก Apple์—์„œ ๋งŒ๋“  ์•Œ๋ฆผ ์„œ๋น„์Šค ํ”Œ๋žซํผ
  • ์ฆ‰, ๋ชจ๋“  ํ‘ธ์‰ฌ์•Œ๋ฆผ์„ ๋ณด๋‚ด๊ธฐ ์œ„ํ•ด ์ค‘์žฌ๋ฅผ ํ•˜๊ณ  ์žˆ๋Š” ์„ผํ„ฐ
  • Apple์€ Server๊ฐ€ ์•ฑ์— ์ง์ ‘ ์•Œ๋žŒ์„ ๋ณด๋‚ด๋Š” ๊ฒƒ์„ ๋ถˆํ—ˆํ•˜๊ณ  ํ•ญ์ƒ APNs๋ผ๋Š” ํ”Œ๋žซํผ์„ ํ†ตํ•ด์„œ๋งŒ ๋ณด๋‚ผ ์ˆ˜ ์žˆ๊ฒŒ ํ•จ

์ž‘๋™๋ฐฉ์‹

 

1. App์ด APNs์—๊ฒŒ remote notification ๋“ฑ๋ก ์š”์ฒญ

 

application.registerForRemoteNotifications()


2. APNs๊ฐ€ App์—๊ฒŒ ์„ฑ๊ณต์ ์œผ๋กœ ๋˜์—ˆ์Œ์„ ์•Œ๋ ค์คŒ๊ณผ ๋™์‹œ์— Device Token ์ „๋‹ฌ

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)

 

3. App์ด Push Server์—๊ฒŒ Device Token์„ ๋ณด๋‚ด๊ณ , Server๋Š” ํ•ด๋‹น ํ† ํฐ์„ ์ €์žฅํ•˜๊ณ  ์žˆ๋Š”๋‹ค. Server๋Š” ๋‚ด๊ฐ€ ๋งŒ๋“  ์„œ๋ฒ„์ผ ์ˆ˜๋„ ์žˆ๊ณ , ๋‹ค๋ฅธ ๊ธฐ์—…์—์„œ ์ œ๊ณตํ•˜๋Š” ์„œ๋ฒ„๋ฅผ ์ด์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค (ex Firebase Cloud Messaging)

 

4. Server๋Š” ํ‘ธ์‹œ๋ฅผ ๋ณด๋‚ด๊ณ  ์‹ถ์„ ๋•Œ, APNs์—๊ฒŒ Device Token๊ณผ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋‚ธ๋‹ค. ์ด๋•Œ APNs์™€ Server๋Š” TLS ํ†ต์‹ ์„ ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, Server์—๋Š” ์ธ์ฆ์„œ๊ฐ€ ์ค€๋น„๋˜์–ด ์žˆ์–ด์•ผ ํ•œ๋‹ค.


5. APNs๋Š” ํ•ด๋‹น Device Token์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋‚ธ๋‹ค.
๋‚ด Device๊ฐ€ Background ์ƒํƒœ๋ฉด OS๊ฐ€ ์ฒ˜๋ฆฌํ•ด์„œ ํ‘ธ์‹œ์•Œ๋ฆผ์„ ๋„์šฐ๊ณ , Foreground ์ƒํƒœ๋ฉด AppDelegate์— ์ •์˜ํ•ด๋†“์€ ๋ฐฉ์‹ ๋Œ€๋กœ APP ์œ„์— ๋„์›Œ์ง„๋‹ค.

 

Push notification ์„ค์ •

  1. ๊ฐœ์ธ์ธ์ฆ์„œ ์ƒ์„ฑ
    1. ํ‚ค์ฒด์ธ → ์ธ์ฆ์„œ ์ง€์› → ์ธ์ฆ ๊ธฐ๊ด€์—์„œ ์ธ์ฆ์„œ ์š”์ฒญ
  2. ํ‘ธ์‹œ์ธ์ฆ์„œ ์ƒ์„ฑ
    1. ๊ฐœ๋ฐœ์ž ์‚ฌ์ดํŠธ → Identifiers → Create Certificate (๊ฐœ๋ฐœ์šฉ ๋ฐฐํฌ์šฉ ๋”ฐ๋กœ ์กด์žฌ) → ๊ฐœ์ธ ์ธ์ฆ์„œ(certSigningRequest) ์ฒจ๋ถ€ → ํ‘ธ์‹œ์ธ์ฆ์„œ ์ƒ์„ฑ ๋ฐ ๋‹ค์šด๋กœ๋“œ
  3. ํ‘ธ์‹œ์ธ์ฆ์„œ ํ‚ค์ฒด์ธ์— ๋“ฑ๋ก
    1. ๋‹ค์šด๋ฐ›์€ ํ‘ธ์‹œ์ธ์ฆ์„œ ๋”๋ธ”ํด๋ฆญ → ํ‚ค์ฒด์ธ๋“ฑ๋ก → ํ‘ธ์‹œ์ธ์ฆ์„œ์˜ ๊ฐœ์ธํ‚ค๋Š” ๊ฐœ์ธ์ธ์ฆ์„œ์˜ ์ด๋ฆ„์œผ๋กœ ๋“ฑ๋ก
  4. ์„œ๋ฒ„์šฉ ์ธ์ฆ์„œ ์ƒ์„ฑ
    1. ํ‘ธ์‹œ์ธ์ฆ์„œ์™€ ๊ฐœ์ธํ‚ค๋ฅผ p12 ํŒŒ์ผ๋กœ ์ €์žฅ → ๋‘๊ฐœ์˜ p12ํŒŒ์ผ๋กœ ๊ฐ๊ฐ pemํŒŒ์ผ ์ƒ์„ฑ → ๋‘๊ฐœ์˜ pem ํŒŒ์ผ์„ ํ•ฉ์ณ ํ•˜๋‚˜์˜ pem ํŒŒ์ผ (์„œ๋ฒ„์šฉ ์ธ์ฆ์„œ) ์ƒ์„ฑ → ์„œ๋ฒ„์— ํ•ด๋‹น ์ธ์ฆ์„œ๋ฅผ ์ €์žฅ
  5. ํ”„๋กœ์ ํŠธ ์„ค์ •
    1. iOS ํ”„๋กœ์ ํŠธ์—์„œ Push Notification, Background Modes ํ™œ์„ฑํ™”
  6. AppDelegate ์ˆ˜์ •
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
	func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
		// APNs์—๊ฒŒ remote notificaiton ๋“ฑ๋ก์š”์ฒญ
		application.registerForRemoteNotifications()
	}
        
	// APNS ์„œ๋ฒ„์— ๋””๋ฐ”์ด์Šค ๋“ฑ๋ก ์„ฑ๊ณต
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
		// ํ† ํฐ์„ ์„œ๋ฒ„์— ์•Œ๋ ค์ค€๋‹ค
	}

	// APNS ์„œ๋ฒ„์— ๋“ฑ๋ก ์‹คํŒจ
    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
		// do something
    }

	// foreground ์ƒํƒœ์—์„œ push notification ์ˆ˜์‹ ์‹œ
	func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
		// do something
	}
}

 

 

์ฐธ๊ณ 

'iOS ๐ŸŽ > iOS' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

App Extension  (0) 2021.12.06
Xcode Target, Project, Workspace  (0) 2021.12.06
Responder chain (1)  (0) 2021.12.06
[Objective-C] atomic vs nonatomic  (0) 2021.12.06
iOS Code Signing  (0) 2021.06.25