Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Firebase Cloud Messaging 入門編

miup
February 20, 2018

Firebase Cloud Messaging 入門編

Firebase.Yebisu #2 発表資料

miup

February 20, 2018
Tweet

More Decks by miup

Other Decks in Programming

Transcript

  1. Who am I ࡾӜ࿨໵(miup) • Cookpad, Komerco ࣄۀ෦ • iOS

    Engineer • ΍͍ͬͯΔ͜ͱ http://techlife.cookpad.com/entry/2018/02/09/102554
  2. ۩ମతʹʢػೳ໘ʣ • OS ͝ͱɺΞϓϦ͝ͱͷૹ৴ʢίϯιʔϧʣ • Topic ͷ֓೦ ( ΧςΰϦΈ͍ͨͳ΋ͷ )

    • Topic ୯ҐͰͷ௨஌ͷडऔ͕Մೳ • ϢʔβʔͷߦಈΛτϦΨʔʹϑΥϩϫʔʹ ௨஌
  3. ࣮૷ (ೝূ) // ೝূ (iOS 10 Ҏ্Λ૝ఆ) Messaging.messaging().delegate = self

    UNUserNotificationCenter.current().delegate = self let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound] UNUserNotificationCenter.current().requestAuthorization( options: authOptions, completionHandler: {_, _ in }) UIApplication.shared.registerForRemoteNotifications()
  4. ࣮૷ (Token औಘ) // implement MessagingDelegate extension AppDelegate: MessagingDelegate {

    public func messaging( _ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) { print(fcmToken) } }
  5. ࣮૷ (subscribe) guard Messaging.messaging().fcmToken != nil else { return }

    // subscribe Messaging.messaging().shouldEstablishDirectChannel = true // unsubscribe Messaging.messaging().shouldEstablishDirectChannel = false
  6. ࣮૷ (Message Handling) // ϑΥΞάϥ΢ϯυͰͷ௨஌ͷड৴ extension AppDelegate: UNUserNotificationCenterDelegate { public

    func userNotificationCenter( _ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { print(response.notification.request.content.userInfo) // call completion handler completionHandler([.alert, .badge, .sound]) } }
  7. ࣮૷ (Message Handling) // ௨஌։෧࣌ͷॲཧ (ϑΥΞάϥ΢ϯυɺόοΫάϥ΢ϯυڞ௨) extension AppDelegate: UNUserNotificationCenterDelegate {

    public func userNotificationCenter( _ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { print(response.notification.request.content.userInfo) // call completion handler completionHandler() } }
  8. ࢖͍ํʢinitializeʣ class NotificationHandler { static let shared = NotificationHandler() private

    init() { // initialize your Tsushi settings (ىಈதͰ΋όφʔग़͔͢Ͳ͏͔) Tsuchi.shared.showsNotificationBannerOnPresenting = true Tsuchi.shared.didRefreshRegistrationTokenActionBlock = { token in // save token to your Database } Tsuchi.shared.subscribe(PushNotification.self) { result in switch result { case let .success((payload, mode)): print("reiceived: \(payload), mode: \(mode)") case let .failure(error): print("error: \(error)") } } } }
  9. ࢖͍ํʢRegistrationʣ extension NotificationHandler { func register() { Tsuchi.shared.register { granted

    in if granted { print("success registration") } else { print("failure registration") } } } func unregister() { Tsuchi.shared.unregister { print("unregister") } } }
  10. ࢖͍ํʢTopicʣ enum Topic: TopicType { case userAction(userID: String) var rawValue:

    String { switch self { case .userAction(let userID): return "user-action-\(userID)" } } } extension NotificationHandler { func subscribe(topic: Topic) { Tsuchi.shared.subscribe(toTopic: topic) } func unsubscribe(topic: Topic) { Tsuchi.shared.unsubscribe(fromTopic: topic) } }
  11. ۩ମతʹ • ΞϓϦىಈ࣌ʹ NotificationHandler Λ init • Ϣʔβʔ࡞੒ͨ͠Β Registration •

    ϑΥϩʔ࣌ʹ subscribe to topic • ΞϯϑΥϩʔ࣌ʹ unsubscribe from topic
  12. ࣮ࡍͷίʔυʢRegistrationʣ let user = Firebase.User() user.save { (ref, error) in

    guard let _ = ref else { return } NotificationHandler.shared.register() }
  13. ࣮ࡍͷίʔυʢTopicʣ extension Firebase { class User: RootObject { ... func

    follow(_ user: Firebase.User) { self.followee.insert(user) user.follower.insert(self) NotificationHandler.shared .subscribe(topic: .userAction(userID: user.id)) } func unfollow(_ user: Firebase.User) { self.followee.remove(user) user.follower.remove(self) NotificationHandler.shared .unsubscribe(topic: .userAction(userID: user.id)) } } }
  14. ࣮ࡍͷίʔυʢCloud Functionsʣ functions.database.ref(root/v1/post/{postID}).onCreate(async event => { const firPost: firebaseModel.Post =

    event.data.val() let user = await admin.database() .ref(`root/v1/user/${firPost.userID}`) .once(‘value’) .then(snap => snap.val()) const payload = { notification: { title: '৽͍͠౤ߘ͕͋Γ·ͨ͠', body: `${user.name}͕৽͍͠౤ߘΛ͠·ͨ͠` } } admin.messaging() .sendToTopic(`user-action-${firPost.userID}`, payload) }