iOS Swift Xcode

Firebase Cloud Messagingを使ってiOSアプリにPush通知を送る

対応手順

  1. Apple Developerアカウントにログインして、APNsのキーを作成してp8ファイルをダウンロードします
  2. Apple Developerアカウントにて、開発用及び本番環境用のAPNs証明書を作成します。
  3. 上記をFirebaseコンソール上で プロジェクトを設定 > Cloud Messaging のメニューから先程ダウンロードしたAPNs 認証キー及びAPNs証明書をアップロードします。
  4. firebase-ios-sdkをインストールしてMessagingを使えるようにします。
  5. XCode上で、TARGETS > YOURAPP > Signing & Capabilitiesで Push NotificationsのCapabilityを追加します。
  6. AppDelegateに以下の記述を追加します。
import SwiftUI
import Firebase

@main
struct Application: App {
    
    @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
    
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

class AppDelegate: NSObject, UIApplicationDelegate, MessagingDelegate {
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {        
        FirebaseApp.configure()
        Messaging.messaging().delegate = self
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().delegate = self
            let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
            UNUserNotificationCenter.current().requestAuthorization(
                options: authOptions,
                completionHandler: { granted, _ in
                    if granted {
                      // TODO subscribe to fcm topic, etc...
                    }
                }
            )
        } else {
            let settings: UIUserNotificationSettings
                = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
            application.registerUserNotificationSettings(settings)
        }
        application.registerForRemoteNotifications()
        
        return true
    }
    
    func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
        print("Oh no! Failed to register for remote notifications with error \(error)")
    }
    
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        var readableToken: String = ""
        for i in 0..<deviceToken.count {
            readableToken += String(format: "%02.2hhx", deviceToken[i] as CVarArg)
        }
        print("Received an APNs device token: \(readableToken)")
    }
}

extension AppDelegate: MessagingDelegate {
    @objc func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
      print("Firebase token: \(String(describing: fcmToken))")
      // let dataDict:[String: String] = ["token": fcmToken ?? ""]
      // NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)
    }
}

extension AppDelegate : UNUserNotificationCenterDelegate {
    func userNotificationCenter(
        _ center: UNUserNotificationCenter,
        willPresent notification: UNNotification,
        withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void
    ) {
        if #available(iOS 14, *) {
            completionHandler([[.banner, .list, .sound]])
        } else {
            completionHandler([[.alert, .sound]])
        }
    }
    
    func userNotificationCenter(
        _ center: UNUserNotificationCenter,
        didReceive response: UNNotificationResponse,
        withCompletionHandler completionHandler: @escaping () -> Void
    ) {
        let userInfo = response.notification.request.content.userInfo
        // 以下の用にしてnotification centerを使って通知し、
        // view側では以下のメソッドを使って、処理を続行します。
        // onReceive(NotificationCenter.default.publisher(for: Notification.Name("didReceiveRemoteNotification")))
        NotificationCenter.default.post(
            name: Notification.Name("didReceiveRemoteNotification"),
            object: nil,
            userInfo: userInfo
        )
        completionHandler()
    }
}

 

-iOS, Swift, Xcode

© 2024 かずのアプリときどきキャンプ飯 Powered by AFFINGER5