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

[iOS/Swift] Firebase Remote Notification ์ •๋ฆฌ

by ํ‹ด๋”” 2023. 3. 8.
728x90
๋ฐ˜์‘ํ˜•

Apple Develper Site Setting

1. APNs key ์ค€๋น„

Apple Developer ์‚ฌ์ดํŠธ > ์™ผ์ชฝ ๋ฉ”๋‰ด > Keys ์„ ํƒ


Keys +  ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ ์ƒˆ๋กœ์šด ํ‚ค ๋“ฑ๋ก


Apple Push Notification service ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๊ณ , key name์„ ์ง€์ •

Continue ๋ฒ„ํŠผ์ด ํ™œ์„ฑํ™” ๋˜๋ฉด ๋ˆŒ๋Ÿฌ ์คŒ

 

์ •๋ณด๋ฅผ ํ™•์ธํ•œ ๋’ค Register์„ ๋ˆŒ๋Ÿฌ ํ‚ค๋ฅผ ๋“ฑ๋กํ•จ

 

2. Key ID ํ™•์ธ

์ธ์ฆ ํ‚ค์˜ Key ID ํ™•์ธ

์ถ”ํ›„ Firebase ์„ค์ • ์‹œ ํ•„์š”ํ•จ

 

3. Team ID ํ™•์ธ

apple developer ์ธ์ฆ์„œ, ์‹๋ณ„์ž ๋ฐ ํ”„๋กœํŒŒ์ผ ๊ด€๋ฆฌ ํŽ˜์ด์ง€์—์„œ ์˜ค๋ฅธ์ชฝ ์ƒ๋‹จ์— ํŒ€ ์•„์ด๋””๊ฐ€ ๋‚˜์™€ ์žˆ์ง€๋งŒ ํ•ด๋‹น ์‚ฌ์ดํŠธ๋กœ๋„ ํ™•์ธ ๊ฐ€๋Šฅ

 

๋กœ๊ทธ์ธ - Apple

 

idmsa.apple.com

๋กœ๊ทธ์ธ ํ›„ ์Šคํฌ๋กค ํ•˜๊ฒŒ ๋˜๋ฉด ๋ฉค๋ฒ„์‹ญ ์„ธ๋ถ€ ์‚ฌํ•ญ ์„น์…˜์ด ์žˆ๊ณ  ์—ฌ๊ธฐ์„œ ํŒ€ ID๋ฅผ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ์Œ


Firebase Setting

iOS ํ”Œ๋žซํผ ์„ ํƒ ํ›„ ์•ฑ ๋“ฑ๋ก ์ •๋ณด ์ž…๋ ฅ


GoogleService info plist ํŒŒ์ผ์„ ํ”„๋กœ์ ํŠธ์— ํฌํ•จ ์‹œํ‚ด

๊ฐœ๋ฐœ๊ณผ ์šด์˜์˜ ๋ฒˆ๋“ค ์•„์ด๋””๊ฐ€ ๋‹ค๋ฅด๋‹ค๋ฉด ์œ„์™€ ๊ฐ™์ด ๋ถ„๋ฆฌํ•˜์—ฌ ํด๋”์— ๋‹ด์•„์คŒ

 

 

 


ํ”„๋กœ์ ํŠธ ๋ฉ”์ธ ํŽ˜์ด์ง€์—์„œ ๋“ฑ๋กํ•ด ์ค€ ์•ฑ์˜ ํ†ฑ๋‹ˆ ๋ฐ”ํ€ด ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ ์„ค์ • ํŽ˜์ด์ง€๋กœ ์ด๋™

 

 

 

 

 

 


ํด๋ผ์šฐ๋“œ ๋ฉ”์„ธ์ง• ํƒญ ์ง„์ž…


์„ค์ •ํ•  ์•ฑ์„ ์„ ํƒํ•˜๊ณ  APN ์ธ์ฆ ํ‚ค ์„ธ์…˜์—์„œ ์—…๋กœ๋“œ ๋ฒ„ํŠผ ํด๋ฆญ


์ด์ „ ๋‹จ๊ณ„์—์„œ ์ƒ์„ฑ ํ•ด ๋‘” key์™€ ํ‚ค ID, ํŒ€ ID๋ฅผ ์ž‘์„ฑ ํ•œ ํ›„ ์—…๋กœ๋“œ ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ ์„ค์ • ์™„๋ฃŒ

ํŒ€ ID๋Š” ์ผ๋ฐ˜ ํƒญ์—์„œ ๋‚ด ์•ฑ ์„น์…˜ > ํŒ€ ID์—์„œ๋„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ์ด๋•Œ ํ‚ค ๋“ฑ๋ก์‹œ ํŒ€ ID๊ฐ€ ์ˆ˜์ • ๋ถˆ๊ฐ€๋Šฅํ•œ ํ˜•ํƒœ๋กœ ์ด๋ฏธ ์ž‘์„ฑ๋˜์–ด ์žˆ์Œ


Xcode Setting

pod 'Firebase/Analytics'
pod 'Firebase/Messaging'

pod file์— Firebase ๋ฅผ ์ถ”๊ฐ€ํ•ด ์ค€ ๋’ค install ํ˜น์€ update


 

์ด๋ฏธ ํ”„๋กœ๋น„์ €๋‹์ด ๋“ฑ๋ก๋˜์–ด ์žˆ๋‹ค๋ฉด Push Notifications๋ฅผ ๋ˆŒ๋Ÿฌ ์ถ”๊ฐ€ํ•ด ์ฃผ๊ณ  ์ƒ๋‹จ Save ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ ์ €์žฅ

์žฌ ๋‹ค์šด๋กœ๋“œ ๋ฐ›์•„ XCode ํ”„๋กœ์ ํŠธ์— ์ ์šฉ ์‹œํ‚ด


Signing&Capabilities์—์„œ +Capability๋ฅผ ๋ˆŒ๋Ÿฌ ๋‚˜ํƒ€๋‚˜๋Š” ํ™”๋ฉด์—์„œ  Push Notifications๋ฅผ ๋ˆŒ๋Ÿฌ ๋“ฑ๋ก

 

Code Side

AppDelegate

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
    application.registerForRemoteNotifications()
    Messaging.messaging().delegate = self
    Messaging.messaging().isAutoInitEnabled = true
    return true
}

/// ํ† ํฐ ์„ค์ •
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    Messaging.messaging().apnsToken = deviceToken
}

/// ํ† ํฐ ํš๋“
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
    Messaging.messaging().token { token, error in
        if let error = error {
            print("๐Ÿ”‘๐Ÿ”‘๐Ÿ”‘๐Ÿ”‘๐Ÿ”‘ Error fetching FCM registration token: \(error)")
        } else if let token = token {
            print("๐Ÿ”‘๐Ÿ”‘๐Ÿ”‘๐Ÿ”‘๐Ÿ”‘ FCM registration token: \(token)")
        }
    }
}

extension HomeViewController: UNUserNotificationCenterDelegate { }

Remote Notification์ด ์˜ค๋ฉด ์ฒ˜๋ฆฌํ•  ํด๋ž˜์Šค์—์„œ  UNUserNotifiationCenterDelegate๋ฅผ ์ฑ„ํƒ

 

// Foreground
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        completionHandler([.alert, .sound, .badge])
    }
// Background
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        
    }

์•ฑ์ด Foreground, Background์˜ ๊ฒฝ์šฐ ์œ„ ๋ฉ”์†Œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋จ

 

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // remote notification
        if let response = connectionOptions.notificationResponse {
            let userInfo = response.notification
                .request.content.userInfo
        }
    }

์•ฑ์ด ์™„์ „ํžˆ ์ข…๋ฃŒ๋œ ์ƒํƒœ์—์„œ ํ‘ธ์‹œ๋ฅผ ๋ˆŒ๋Ÿฌ ์•ฑ์„ ์‹คํ–‰ํ•˜๋Š” ๊ฒฝ์šฐ SceneDeleate์—์„œ๋Š” ์œ„์™€ ๊ฐ™์ด ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ์Œ

 


GoogleService Info Plist ํŒŒ์ผ์˜ ์ด๋ฆ„์„ ๋ณ€๊ฒฝํ•œ ๊ฒฝ์šฐ AppDelegate์—์„œ ์„ค์ • ํ•„์š”

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
        var filePath: String!
        #if DEBUG
        filePath = Bundle.main.path(forResource: "GoogleService-Info-dev", ofType: "plist")
        #else
        filePath = Bundle.main.path(forResource: "GoogleService-Info-release", ofType: "plist")
        #endif
        
        let options = FirebaseOptions.init(contentsOfFile: filePath)!
        FirebaseApp.configure(options: options)
}

์ถ”ํ›„ ๋ฐ์ดํ„ฐ ํ•ธ๋“ค๋ง ํฌ์ŠคํŒ…


Firebase Cloud message

(๊ธ€ ์ž‘์„ฑ ์‹œ์  2023.03.08 / ํŒŒ์ด์–ด๋ฒ ์ด์Šค ํ”„๋กœ์ ํŠธ ํŽ˜์ด์ง€๋„ ์ˆ˜์‹œ๋กœ ๋ณ€๊ฒฝ๋˜๋ฏ€๋กœ ๋‚ ์งœ ํ™•์ธ ํ•„์š”)

์™ผ์ชฝ ๋ฉ”๋‰ด์—์„œ ๋ชจ๋“  ์ œํ’ˆ์„ ๋ˆ„๋ฅธ ํ›„ ๊ฐฑ์‹ ๋˜๋Š” ํ™”๋ฉด์—์„œ Cloud Messaging์„ ๋ˆŒ๋Ÿฌ์คŒ 

๊ทธ ํ›„ ์™ผ์ชฝ ๋ฉ”๋‰ด์— Messaging ๋…ธ์ถœ ๋˜๋ฉฐ ์ดํ›„์—๋Š” ์ด ๋ฉ”๋‰ด๋ฅผ ๋ˆŒ๋Ÿฌ ์ ‘๊ทผํ•˜๋ฉด ๋จ

์ฒซ ๋ฒˆ์งธ ์บ ํŽ˜์ธ ๋งŒ๋“ค๊ธฐ ์„ ํƒ

Firebase ์•Œ๋ฆผ ๋ฉ”์‹œ์ง€๋ฅผ ์„ ํƒ

 

์ •๋ณด๋ฅผ ์ž…๋ ฅ ํ•ด ์ค€ ๋’ค ๋‹ค์Œ ๋ฒ„ํŠผ ๋ˆ„๋ฆ„

ํ‘ธ์‹œ๋ฅผ ๋ณด๋‚ผ ํƒ€๊ฒŸ ์•ฑ์„ ์„ ํƒ ํ•œ ๋’ค ๋‹ค์Œ

์‹œ๊ฐ„ ์„ค์ • ํ›„ ๋‹ค์Œ ๋ˆ„๋ฅธ ๋’ค ๊ฒŒ์‹œํ•ด ์ฃผ๋ฉด ์บ ํŽ˜์ธ์ด ์ €์žฅ๋˜๊ณ  ํ‘ธ์‹œ๊ฐ€ ๊ฐ

 

์ด๋ฏธ์ง€ ์ถœ์ฒ˜ : https://www.pexels.com/ko-kr/photo/15007786/

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€