ClintJang / sample-swift-fcm-settting

iOS FCM(Firebase Cloud Messaging), Push 메시지 최소한의 설정해본 샘플입니다. 미디엄에 글을 쓰고 예제 소스 링크를 위해 만들었습니다.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

iOS Swift FCM Push 메시지를 설정 해본 Sample입니다. ☺️

iOS FCM(Firebase Cloud Messaging), Push 메시지 최소한의 설정해본 샘플입니다. 미디엄에 글을 쓰고 예제 소스 링크를 위해 만들었습니다.

참고 사항

  • CocoaPods 로 구성되어있습니다.

  • GoogleService-Info.plist 파일은 제외하고 소스를 올렸습니다.

    • 미디엄 링크 내용 중 아래 이미지 처럼 GoogleService-Info.plist을 내려받을 수 있는 진행이 있습니다. 해당 파일을 넣어주시면 됩니다.

상세

target 'JWSFCMSample' do
  # Comment the next line if you're not using Swift and don't want to use dynamic frameworks
  use_frameworks!

  # Pods for JWSFCMSample

  pod 'Firebase/Core'
  pod 'Firebase/Messaging'

end
import UIKit
import Firebase
import FirebaseMessaging
import UserNotifications

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        FirebaseApp.configure()
        Messaging.messaging().delegate = self

        UNUserNotificationCenter.current().delegate = self
        
        let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization(
            options: authOptions,
            completionHandler: {_, _ in })
        application.registerForRemoteNotifications()
        return true
    }

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        Messaging.messaging().apnsToken = deviceToken
    }
}

extension AppDelegate: UNUserNotificationCenterDelegate {
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        print("\(#function)")
    }
}

extension AppDelegate: MessagingDelegate {
    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
        print("Firebase registration token: \(fcmToken)")
        let dataDict:[String: String] = ["token": fcmToken]
        NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)
    }
}
  • Capabilitles 설정
Push Notifications Background Modes


감사합니다.


즐거운 하루 되세요 🙇‍

About

iOS FCM(Firebase Cloud Messaging), Push 메시지 최소한의 설정해본 샘플입니다. 미디엄에 글을 쓰고 예제 소스 링크를 위해 만들었습니다.

License:MIT License


Languages

Language:Swift 88.1%Language:Ruby 11.9%