Share
Embed
Transcript
26 slides
-
Who am I ࡾӜ(miup) • Cookpad, Komerco ࣄۀ෦ • iOS
Engineer • ͍ͬͯΔ͜ͱ http://techlife.cookpad.com/entry/2018/02/09/102554 -
۩ମతʹʢػೳ໘ʣ • OS ͝ͱɺΞϓϦ͝ͱͷૹ৴ʢίϯιʔϧʣ • Topic ͷ֓೦ ( ΧςΰϦΈ͍ͨͳͷ )
• Topic ୯ҐͰͷ௨ͷडऔ͕Մೳ • ϢʔβʔͷߦಈΛτϦΨʔʹϑΥϩϫʔʹ ௨ -
࣮ (ೝূ) // ೝূ (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() -
࣮ (Token औಘ) // implement MessagingDelegate extension AppDelegate: MessagingDelegate {
public func messaging( _ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) { print(fcmToken) } } -
࣮ (subscribe) guard Messaging.messaging().fcmToken != nil else { return }
// subscribe Messaging.messaging().shouldEstablishDirectChannel = true // unsubscribe Messaging.messaging().shouldEstablishDirectChannel = false -
࣮ (subscribe topic) // subscribe topic Messaging.messaging().subscribe(toTopic: “TopicName”) // unsubscribe
topic Messaging.messaging().unsubscribe(toTopic: “TopicName") -
࣮ (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]) } } -
࣮ (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() } } -
͍ํʢPayload) struct PushNotification: PushNotificationPayload { let hoge: String let hige:
String let aps: APS? } -
͍ํʢ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)") } } } } -
͍ํʢ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") } } } -
͍ํʢ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) } } -
۩ମతʹ • ΞϓϦىಈ࣌ʹ NotificationHandler Λ init • Ϣʔβʔ࡞ͨ͠Β Registration •
ϑΥϩʔ࣌ʹ subscribe to topic • ΞϯϑΥϩʔ࣌ʹ unsubscribe from topic -
࣮ࡍͷίʔυʢRegistrationʣ let user = Firebase.User() user.save { (ref, error) in
guard let _ = ref else { return } NotificationHandler.shared.register() } -
࣮ࡍͷίʔυʢ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)) } } } -
࣮ࡍͷίʔυʢ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) }