APNsTutorial-iOS
-
๋จ์ํ ์์๋ฅผ ๋ฐ๋ผ์ ๊ฐ๋ฉด ๋ ์ค ์์๋๋ฐ ์์์ผํ ๊ฒ๋ ์์๊ณ ๊ฒฝ์ฐ์ ๋ฐ๋ผ์ ์๊ตฌํ๋ ํ์ผ๋ ๋ฌ๋๋ค. ๊ทธ๋ฌ๋ ์ฒ์ฒํ ์ฝ์ด์ฃผ์๊ธฐ ๋ฐ๋๋๋ค.
-
๋จผ์ ์ด๋ค ์๋ฒ ํ๊ฒฝ์์ ํธ์ฌ์๋ฆผ์ ๋ณด๋ด์ค ๊ฑด์ง ๊ทธ๋ ๋ค๋ฉด server provider ๊ฐ ์๊ตฌํ๋ ํ์ผ์ ๋ฌด์์ธ์ง ์๊ณ ์งํํ๊ธฐ๋ฅผ ๋ฐ๋๋๋ค.
p8(์ธ์ฆํค) ,p12(์ธ์ฆ์) ํ์ผ ๋ชจ๋ ๋ง๋๋ ๋ฐฉ๋ฒ์ ์ ๋ฆฌํ๋ค. APNs ํค๋(p8) ๊ณ์ ๋น 2๊ฐ๊น์ง๋ง ๋ง๋ค ์ ์์ด์ ๋๋ ๋ชป๋ง๋ค์ด๋ดค๊ณ ์ถ์ฒ๋ฅผ ์ฐธ๊ณ ํ๋ค.
(์ฐธ๊ณ ๋ก p8(์ธ์ฆํค)์ ์ธ์ฆ๊ฐฑ์ ์ ํ์ง ์์๋ ๋๊ธฐ ๋๋ฌธ์ p12(์ธ์ฆ์)๋ณด๋ค ์ ํธํ๋ค. ํ์ด์ด๋ฒ ์ด์ค์์๋ ๋ ๋ค ์ง์ํ๋ค.)
์ดํด๋ฅผ ์๋ฒฝํ๊ฒ ํ์ง ์๋๋ผ๋ ์๋์ ๊ธ์ ๋จผ์ ์ฝ์ด๋ณด๋ ๊ฑธ ์ถ์ฒํ๋ค.
[iOS] provider server์ APNs์ ์์ ํ ์ฐ๊ฒฐ์ ์ํ ๋๊ฐ์ง ๋ฐฉ๋ฒ
Apple Push Notification service (APNs) ์ค์ ํ๊ธฐ
APNs ๋? iOS ์์ ์ง์ํ๋ ํธ์ฌ์๋ฆผ์ด๋ค.
1. CSR ๋ฐ๊ธ
CSR (Certificate Signing Request : ์ธ์ฆ์ ์๋ช ์์ฒญ)
- ํค์ฒด์ธ์ ์ฐ๋ค. ํค์ฒด์ธ ์ ๊ทผ - ์ธ์ฆ์ ์ง์ - ์ธ์ฆ ๊ธฐ๊ด์์ ์ธ์ฆ์ ์์ฒญ
- ์ด๋ฉ์ผ ์ฃผ์์ ์ผ๋ฐ ์ด๋ฆ์ ์ ๋ ฅํด์ฃผ๊ณ ์์ฒญํญ๋ชฉ์ ๋์คํฌ์ ์ ์ฅ๋จ ์ด๋ผ๊ณ ์ฒดํฌํด์ค๋ค.
- ๊ณ์ ์งํํ๋ฉด ๋๋ค.
CertificateSigningRequest.certSigningRequest ๋ช ์ผ๋ก ํ์ผ์ด ์์ฑ๋๋ค.
2. App ID ๋ฑ๋ก ๋ฐ Push Notification ํ์ฑํ
apple developer ํํ์ด์ง์ ์ ์.
(Account
- Identifiers ๋ฑ๋ก
- App IDs ์ ํ
- Register an App ID
Bundle ID ๋ ํ๋ก์ ํธ์์ ํ์ธ ํ ์ ์์ต๋๋ค!
- ํ๋จ์ Push Notifications ์ ํ
๊ทธ๋ฆฌ๊ณ ๋ง๋ค์ด์ง App ID ๋ฅผ ํ์ธํ๋ฉด ๋ฉ๋๋ค.
A. p12 ์ธ์ฆ์ ๋ง๋ค๊ธฐ
p8 ์ธ์ฆํค๋ฅผ ๋ง๋ค๋ ค๋ฉด B ๋ก ๊ฐ๋ฉด ๋๋ค.
A-1. Certificates ์์ฑ
- ์๊น ๋ง๋ Identifier ๋ฅผ ์ ํํด์ ํ๋จ์ Push Notification ์์ configure ๋ฅผ ํด๋ฆญํ๋ค.
- ์ฉ๋์ ๋ง๊ฒ ์ธ์ฆ์๋ฅผ ์์ฑํ๋ฉด ๋๋ค. (ํ์ด์ด๋ฒ ์ด์ค์์๋ Development ์ Production ๋ชจ๋ ์๊ตฌํ๋ค.)
- ์๊น ๋ง๋ค์ด๋ CSR ํ์ผ์ ์ ๋ก๋ํ๋ค.
- ์์ฑํ ์ธ์ฆ์๋ฅผ ๋ค์ด๋ก๋ํด์ค๋ค.
ํ์ธ)
๊ทธ๋ฌ๋ฉด Account
A-2. Certificate ๋ฅผ ํค์ฒด์ธ์ ๋ฑ๋กํ๊ธฐ
- ๋ค์ด๋ก๋๋ฐ์ aps_development.cer ํ์ผ์ ๋๋ธํด๋ฆญํด์ ํค์ฒด์ธ์ ๋ฑ๋กํด์ค๋ค.
(์ด๋ ํค๋ ๊ฐ์ด ๋ฑ๋ก๋๋๋ฐ ํ๋ฒ๋ง ๋ฑ๋ก ํ ์ ์๋ ๋ฏ ํ๋ค. ๊ถ๊ธํด์ ํค์ฒด์ธ์์ ์ญ์ ํด๋ดค๋๋ฐ ํค๋ ๋ฑ๋ก์ด ์๋๋ค. identifier ๋ฅผ ์๋ก ๋ฑ๋กํด์ฃผ๊ณ ์ธ์ฆ์๋ ๋ค์ ๋ง๋ค์ ๋ฑ๋ก ํ ์ ์์๋ค.)
(์ผ๋ถ ๊ธ์์ p8 ํ์ผ์ ๋ง๋ค๊ธฐ ์ํด์ apple developer ์์ key ๋ฅผ ๋ฑ๋กํด์ฃผ๋๋ฐ ๊ทธ๋๋ง ๋ค์ด๋ก๋๊ฐ ๊ฐ๋ฅํ๋ค. p12 ํ์ผ์ ๋ํด์ ํค๋ ์ด๋๋ง ๋ฑ๋ก๊ฐ๋ฅํ ๊ฒ ๊ฐ๋ค.)
A-3. p12 ํ์ผ ์์ฑ
์ฐ๋ฆฌ๊ฐ Push Notification ์ ๋ฐ๊ธฐ ์ํด์ pem ํํ์ ์ธ์ฆ์๊ฐ ํ์ํ๋ค.(๊ฒฝ์ฐ์ ๋ฐ๋ผ์๋ p12 ํํ์ ํ์ผ๋ง ์๊ตฌ๋๊ธฐ๋ ํ๋ค.) ๊ทธ๋ ๊ธฐ ์ํด์ ๋จผ์ p12 ํํ์ ํ์ผ์ ๋ง๋ค์ด์ผ ํ๋๋ฐ, ์ด p12 ํ์ผ์ ์์ฑํ๊ธฐ ์ํด์ ์๊น ํค์ฒด์ธ์ ๋ฑ๋กํ๋ ์ธ์ฆ์์ ํค๊ฐ ํ์ํ๋ค.
- ํค์ฒด์ธ์ ๋ฑ๋กํ ๋ ๋ก์ปฌํญ๋ชฉ์ ์ง์ ํด๋์๋ค๋ฉด ๊ธฐ๋ณธํค์ฒด์ธ - ๋ก๊ทธ์ธ - ์ธ์ฆ์ ์์ ์ธ์ฆ์์ ๊ฐ์ธํค๋ฅผ ํ์ธ ํ ์ ์๋ค.
- ์ธ์ฆ์์ ๊ฐ์ธํค ๋๊ฐ๋ชจ๋ ์ ํํ๊ณ ๋ด๋ณด๋ด๊ธฐํ๋ค.
- .p12 ํ์ผ๋ก ์ค์ ํ๊ณ ์ ์ฅํด์ค๋ค.
- ์ํธ๋ฅผ ์ ๋ ฅํด์ฃผ๋๋ฐ ๋์ค์๋ ํ์ํ๋ค ๊ธฐ์ตํด๋์.
*provider server ์ ์ธ์ฆ์์ ๊ฐ์ธ ํค๋ฅผ ๋ชจ๋ ์ค์นํด์ผํ๋๋ฐ, ์๋ฒ ๊ฐ๋ฐํ๊ฒฝ์ ๋ฐ๋ผ ์ ๋ฌํ ํ์ผ์ด ๋ฌ๋ผ์ง๋ค๊ณ ํ๋ค. php ์๋ฒ์๋ .pem ์ ์ ๋ฌํด์ผํ๊ณ , ๋๋จธ์ง๋ .p12 ๋ง ์ ๋ฌํด๋ ๋๋ ๊ฒฝ์ฐ๊ฐ ์๋ ๋ฏํ๋ค.
firebase ๊ฐ์๊ฒฝ์ฐ๋ ๋๊ฐ์ง ์ง์ํ๋ค.
์ฐธ๊ณ ) pem ํ์ผ์ด ์๊ตฌ๋๋ค๋ฉด ์๋ ์ฃผ์๋ฅผ ์ฐธ์กฐํด์ ํตํด์ ๋ง๋ค์ด์ฃผ๋ฉด ๋๋ค.
https://babbab2.tistory.com/57
B. p8 ์ธ์ฆํค ๋ง๋ค๊ธฐ
์์ App ID ๋ฑ๋ก ๋ฐฉ๋ฒ๊ณผ ๋๊ฐ์ด ํด์ฃผ๊ณ ๋ค๋ฅธ์ ์ Development ์ Production certificates ๋ง ๋ง๋ค์ง ์์ผ๋ฉด ๋๋ค. ์๋๋ฉด ์ฐ๋ฆฐ ์ธ์ฆ์๊ฐ ์๋ ์ธ์ฆํค๋ฅผ ์ฌ์ฉํ ๊ฑฐ๋๊น.
๋ ธ๋์ ๊ฒฝ๊ณ ์ฒ๋ผ ํ๋ฒ๋ง ๋ค์ด๋ก๋ ๊ฐ๋ฅํ๋ค.
๊ทธ ๋ค์์ ํ์ด์ด๋ฒ ์ด์ค์์ p8 ํ์ผ์ ์ธ์ฆํค๋ก ๋ฑ๋กํด์ฃผ๋ฉด ๋๋ค.
์ถ์ฒ
3. ํ๋ก์ ํธ์์ iOS Push Notification, Backgrounds Mode ํ์ฑํ
- ์์ ์ iPhone ์์ ๋๋ฆด ๊ฒฝ์ฐ์ ํด๋น ๊ฐ๋ฐ์๊ณ์ ๊ณผ device ๋ฅผ ์ค์ ํ๋ค.
- Signing & Capabilities ํญ์์ +Capability ๋ฅผ ์ ํํด์ Push Notifications ๋ฅผ ์ถ๊ฐํ๋ค.
- ๊ฐ์ ๋ฐฉ๋ฒ์ผ๋ก Background Modes ๋ ์ถ๊ฐํ๊ณ Background fetch ์ Remote notifications ๋ฅผ ์ค์ ํ๋ค.
4. firebase ์์ ํธ์ ์๋ฆผ์ ๊ตฌํํ๋ค๋ฉด?
- p8 ํค๋ฅผ ์ฐ๋ ์์. ์ธ์ฆํค๋ 1๋ ๋ง๋ค ๊ฐฑ์ ํ์ง ์์๋ ๋๊ธฐ ๋๋ฌธ์ ๋ณดํต p8 ๋ฅผ ์ฐ๋ ์์๊ฐ ๋ง์๋ค.
- p12 ์ธ์ฆ์๋ฅผ ์ฐ๋ ์์. ์ธ์ฆ์๋ 1๋ ๋ง๋ค ๊ฐฑ์ ํด์ผ ํ๋ค.
https://faith-developer.tistory.com/156
์ ์ค์ ๋ก ํ๋ฒ ํธ์์๋ฆผ์ ๋ณด๋ด๋ณด๊ฒ ์ต๋๋ค.
5. iOS ์ฑ์ Firebase ์ถ๊ฐํ๊ธฐ
iOS ์ฑ์ ๋ด Firebase ํ๋ก์ ํธ์ ์์ ํ๊ฒ ์ฐ๊ฒฐํ๊ธฐ ์ํด์ , ์ธ์ฆ์ ํ์ผ์ ๋ค์ด๋ฐ์์ผ ํ๊ณ , ๊ทธ๊ฑธ ๋ด iOS ์ฑ ์์ ์ธํ ํด์ผ ํฉ๋๋ค.
- ํ๋ก์ ํธ๋ฅผ ์์ฑํด์ฃผ๊ณ
- ios ์ฑ์ ์ถ๊ฐํด์ค๋๋ค.
- ์ฑ์ ๋ฑ๋กํด์ค๋๋ค. (iOS ๋ฒ๋ค ID ๋ ํ๋ก์ ํธํ์ผ์์ ํ์ธ๊ฐ๋ฅํ๋ค.)
- GoogleService-info.plist ๋ค์ด๋ก๋ ํด์ ํ๋ก์ ํธ์ ์ถ๊ฐํด์ค๋ค.
- Friebase SDK ๋ฅผ ์ถ๊ฐํ๋ค.
- ์ถ๊ฐ์ ์ผ๋ก ์ถํ์ ๋ฉ์์ง ์์ ์ ์ํด์
Firebase/Messaging
ํ์ํ๋ ์ง๊ธ ๊ฐ์ด ์ถ๊ฐํด์ install ํด์ฃผ์.
- AppDelegate.swift ํ์ผ์
import Firebase
์FirebaseApp.configure()
๋ฅผ ์ถ๊ฐํด์ค๋ค.
6. ์๋ฆผ ๊ถํ ์์ฒญ
- iOS ์์๋ ์ฌ์ฉ์์ ๋์ ์๋ notification ์ ์์ ํ์ง ๋ชปํ๋๋ก ํ๊ธฐ๋๋ฌธ์ ๊ถํ์ ์์ฒญํด์ผํ๋ค.
๋ณดํต ์ฑ์ด ์ฒ์ ์คํ๋ ๋ ๋ฌผ์ด๋ณด๊ฑฐ๋ ํธ์์๋ฆผ์ ์ค์ ํ๋ ๋จ๊ณ์์ ๋ฌผ์ด๋ณด๋ฉด ๋๋ค. ์๋ ์ฝ๋๋ ์ฑ์ ๋ฐ์นํ ๋ ๊ถํ์ ์์ฒญํ๊ธฐ ์ํด์ application(_:didFinishLaunchingWithOptions:)
๋ฉ์๋์ ์ถ๊ฐํด ์คฌ๋ค.
- AppDelegate.swift
UNAuthorizationOptions
์ผ๋ก ํธ์ ๊ถํ์ ์ค์ ํด์ค๋ค. (alert : ์๋ ๊ถํ, badge : badge ์
๋ฐ์ดํธ ๊ถํ, sound : ์๋ฆฌ ๊ถํ)
// Tells the delegate that the launch process is almost done and the app is almost ready to run.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
// ๊ธฐ์กด์ ์ ์๋ ํ์ด์ด๋ฒ ์ด์ค ๊ถํ ์ค์
FirebaseApp.configure()
// ํธ์ ๊ถํ ์ค์
UNUserNotificationCenter.current().delegate = self
let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter
.current()
.requestAuthorization(
options: authOptions,completionHandler: { (_, _) in }
)
application.registerForRemoteNotifications()
return true
}
- ํ์ด์ด๋ฒ ์ด์ค์ ๋ ธํฐ๊ฐ ์์ ๋ ์ ์๋๋ก ํ๋ค.
extension AppDelegate : UNUserNotificationCenterDelegate {
// 1. Asks the delegate how to handle a notification that arrived while the app was running in the foreground.
func userNotificationCenter(_ center: UNUserNotificationCenter,willPresent notification: UNNotification,withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .sound])
}
// 2. Asks the delegate to process the user's response to a delivered notification.
func userNotificationCenter(_ center: UNUserNotificationCenter,didReceive response: UNNotificationResponse,withCompletionHandler completionHandler: @escaping () -> Void) {
completionHandler()
}
}
์ฃผ์)
1 : userNotificationCenter(_:willPresent:withCompletionHandler:)
๋ฉ์๋๋ foreground ์์ ๋ฌ๋์ค์ ์ฑ์ ๋์ฐฉํ๋ notification ์ ๋ค๋ฃฌ๋ค.
completionHandler()
์ notification ์ ๋ํด์ presentation option ์ ๊ฐ์ง๊ณ ์คํ๋ ๋ธ๋ก์ด๋ค. UNNotificationPresentationOptions(.sound, .badge ๋ฑ์ ์ต์
) -> Void
์ ํตํด ์ด ๋ธ๋ก์๋ ๋ฆฌํด๊ฐ์ด ์๊ณ UNNotificationPresentationOptions
ํ๋ผ๋ฏธํฐ๋ฅผ ์ฌ์ฉํจ์ ์ ์ ์๋ค.
2 : userNotificationCenter(_:didReceive:withCompletionHandler:)
๋ฉ์๋๋ ๋์ฐฉํ notification ์ ๋ํ user ์ ๋ฐ์์ ๋ค๋ฃฌ๋ค.
completionHandler ์ ๋ฐํ๊ฐ์ด () -> Void completionHandler()
์ด๊ธฐ ๋๋ฌธ์ completionHandler ๋ง์ง๋ง์ ํญ์ completionHandler()
๋ฅผ ํธ์ถํด์ฃผ์ด์ผ ํ๋ค.
์ด ๋ฉ์๋๋ ์๋์ ๊ฐ์ด notification ์์ action ์ ๋ํ ์ค์ ์ ํ ์ ์๋ค.
๋ ์์ธํ ๋ด์ฉ์ ์๋ ์ฃผ์๋ฅผ ์ฐธ์กฐ.
Apple Developer - Declaring Your Actionable Notification Types
warning
iOS 14.0 ์์ alert ์ต์ ์ด ์ถ์ฒ๋์ง ์์๋ค. ๊ทธ๋์ UNNotificationPresentationOptions ์ ์ด๋ค ์์ฑ๋ค์ด ์กด์ฌํ๋์ง ์ดํด๋ณด์๋ค.
badge : ์ฑ ์์ด์ฝ ์ฐ์ธก์๋จ์ ํ์๋๋ ์๋ฆผ ์ซ์๊ฐ badge ์ ํด๋นํ๋ค. foreground ์ผ ๊ฒฝ์ฐ๋ ์๋ฆผ์ ๋ฐ๋ก ํ์ธํ๋๊น ํน๋ณํ ๊ฒฝ์ฑ ์๋๋ฉด ๋ถํ์ํ๋ค.
sound : notification ๊ณผ ๊ด๋ จ๋ ์๋ฆฌ๋ฅผ ์คํํ๋ค.
alert : notification ์ผ๋ก๋ถํฐ content ๋ฅผ ์ ๊ณต๋ฐ์ alert ์๋์ ๋ณด์ฌ์ค๋ค.
iOS 14 ๋ถํฐ ์ถ๊ฐ๋ ์ต์ ๋ค์ด๋ค. alert ๋ ๋์ด์ ์ถ์ฒ๋์ง ์๋๋ค.
banner : ๋ฐฐ๋์ฒ๋ฌ notification ์ ๋ณด์ฌ์ค๋ค.
list : Notification Center ์์ ๋ณด์ฌ์ค๋ค. (๋ง ๊ทธ๋๋ก ์๋ฆผ์ผํฐ๋ค. ์ ๊ธ ํ๋ฉด์์ ํ๋ฉด์ ์ค๊ฐ์ ์ค์์ดํ ์ ํ๊ฑฐ๋ ๋ค๋ฅธ ํ๋ฉด์์ ์คํฌ๋ฆฐ ์์ ์ค์์ ์ค์์ดํ๋ค์ดํ๋ฉด ๋์ค๋ '์๋ฆผ์ผํฐ'๋ค.)
- ์ฑ์ ์คํํ๊ฒ ๋๋ฉด notifications ์ ๋ํ ๊ถํ์ ์์ฒญํ๋ค. ๊ทธ๋ฆฌ๊ณ ์ฑ์ค์ ์์ ํ์ธํด๋ณด๋ฉด Allow Notificiations ๊ฐ ์ค์ ๋์ด์๋ ๊ฒ์ ํ์ธ ํ ์ ์๋ค.
7. Firebase iOS ์ฑ ๋ด์ ์ธ์ฆ์ ๋ฑ๋ก
- ํ์ด์ด๋ฒ ์ด์ค์ ๋ฑ๋กํ๋ ํ๋ก์ ํธ์ ์ฑ์ ์ ํํ๋ค.
- ํด๋ผ์ฐ๋ ๋ฉ์์ง์์ ์ธ์ฆ์๋ฅผ ๋ฑ๋กํ๋ค.
- ์ธ์ฆํค ์ฆ p8 ํ์ผ์ ์ฌ์ฉํ๋ฉด ์ข๋ค๊ณ ํ์ง๋ง ๋๋ p12 ํ์ผ๋ง ์๊ธฐ๋๋ฌธ์ APN ์ธ์ฆ์์ ์ธ์ฆ์๋ฅผ ์ ๋ก๋ ํ ๊ฒ์ด๋ค.
A-1. Certificates ์์ฑ ๋จ๊ณ์์ ๊ฐ๋ฐ APN ๊ณผ ํ๋ก๋์ APN ์ ๋ง๋ค์ด์ฃผ๋ฉด ๋๋ค. ๊ทธ๋ฆฌ๊ณ ํค์ฒด์ธ์์ ๋ด๋ณด๋ด๊ธฐํ p12 ํ์ผ์ ์ ๋ก๋ํด์ค๋ค.
8. Firebase ์ฝ์์์ ํ ์คํธ ๋ฉ์ธ์ง ๋ณด๋ด๊ธฐ
- Cloud Messaging ๋ค์ด๊ฐ๋ค.
- ๋ฉ์์ง ๋ณด๋ด๊ธฐ
- ์๋ฆผ ์ ๋ชฉ๊ณผ ํ ์คํธ๋ฅผ ์ค์ ํด์ค๋ค.
- ํ์ด์ด๋ฒ ์ด์ค์ ๋ฑ๋กํ App ์ ์ ํํ๋ค.
- ์์ฝํ ์ต์ ์ ์ ํํ๊ณ ๋ค์์ ๋๋ฌ ์งํ์ ํด๋ ๋๊ณ ๊ฒํ ๋ฅผ ํด๋ ๋๋ค.
- ๊ฒํ ํ๊ฒ๋๋ฉด ๋ค์๊ณผ ๊ฐ์ ํ์ ์ฐฝ์ด ๋ฌ๋ค. ๊ฒ์ํด๋ณด์.
์๋์ ์ธ๊ฐ์ง ๊ฒฝ์ฐ ๋ชจ๋ ์ ์์ ์ผ๋ก ๋์ฐฉํ๋ค๋ฉด ํ ์คํธ ๋ฉ์์ง ๋ณด๋ด๊ธฐ ์ฑ๊ณต์ด๋ค.
- ์ฑ์ด ์ผ์ ธ์์ ๋ (Foreground)
- ์ฑ์ด ์ผ์ ธ์์ง๋ง background ์ ์์ ๋ (Background)
- ์ฑ์ด ๊บผ์ ธ์์ ๋ (Quit)