Swift

FCM, APNS 연동 방법

반다이크 2022. 2. 25. 16:36
반응형

1. APNS

1-1 Apple Developer 접속

 

2-1 클릭

 

 

2-2 Key 추가하기 클릭(아이디당 2개 생성 가능)

 

 

2-3 체크 후 Continue

 

2-4 등록 완료

 

 

2-5 등록 완료

  • (p8 파일 다운로드 필수!)
  • (한번 다운로드 이후 재시도 불가)
  • Key ID 별도 메모

2-6 Identifier 클릭

 

2-7 App IDs 클릭

 

 

2-8 이름 및 Bundle ID 입력

 

- push notification 체크

 

2-9 Identifier 확인

 

 

2.FireBase 연동

1-1 프로젝트 생성

.

1-2 프로젝트 이름 입력(XCode 프로젝트 이름과 무관)

 

2-1 iOS 앱 등록

2-2 IOS 번들 ID를 입력(필수), App Store ID 없을때 임의 번호 입력(선택)

 

2-3 GoogleService Info 다운로드 후 Xcode 프로젝트로 드래그

 

 

2-4 Firebase pod 설치

 

 

2-5 AppDelegate에서 파이어베이스 연동

 

 

2-6 1차 연동 완료

3.Cloud Messaging

1-1 프로젝트 설정 진입

 

1-2 클라우드 메시징 진입

 

 

1-3 p8 파일 등록

 

  • 키ID 입력(APNS 2-4에서 등록했던 키)
  • 팀ID 입력(APNS 2-8에서 등록했던 인증서)

4.XCode 설정

1-1 push notification 설정 추가

 



1-2 Firebase Messaging pod 설치

1-3 Appdelegate에서 import Firebase 추가

 

 

1-3(2) 권한 관련 코드 추가

 public func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?){
        
        if #available(iOS 12.0, *) {
            UNUserNotificationCenter.current().requestAuthorization(
                options: [.alert, .sound, .badge, .providesAppNotificationSettings], completionHandler: { didAllow,Error in
            })
        } else {
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge], completionHandler: {didAllow,Error in
                print(didAllow)
            })
        }
        UNUserNotificationCenter.current().delegate = self
        
        FirebaseApp.configure()
        Messaging.messaging().delegate = self
        
        application.registerForRemoteNotifications()
    }

1-3(3) 메시징 및 토큰 관련 코드 추가

 

extension AppDelegate: UNUserNotificationCenterDelegate, MessagingDelegate {
    
    public func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
        let firebaseToken = fcmToken ?? ""
        print("firebase token: \(firebaseToken)")
    }
    
    public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        completionHandler([.alert, .badge, .sound])
    }
    
    public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        completionHandler()
    }
}

##콘솔창에 FCM Token 출력

 

5.노티 테스트

1-1 Firebase Cloud Messaging 진입

 

 

1-2 메시지 알림 타이틀 및 내용 작성

 

1-3 Xcode에서 출력된 토큰값 입력

 

 

1-4 푸시 메시지 확인

반응형

'Swift' 카테고리의 다른 글

iOS 페이스북 로그인  (0) 2022.02.26
iOS 네이버 로그인  (0) 2022.02.26
iOS 카카오 로그인  (0) 2022.02.26
Firebase Dynamic Link  (0) 2022.02.25
URL Scheme 딥링크  (0) 2022.02.25