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

Propagate Remote Config updates in real time

d_date
November 14, 2018

Propagate Remote Config updates in real time

d_date

November 14, 2018
Tweet

More Decks by d_date

Other Decks in Programming

Transcript

  1. exports.showConfigDiff = functions.remoteConfig.onUpdate(versionMetadata => { return admin.credential.applicationDefault().getAccessToken() .then(accessTokenObj => {

    return accessTokenObj.access_token; }) .then(accessToken => { const currentVersion = versionMetadata.versionNumber; const templatePromises = []; templatePromises.push(getTemplate(currentVersion, accessToken)); templatePromises.push(getTemplate(currentVersion - 1, accessToken)); return Promise.all(templatePromises); }) .then(results => { const currentTemplate = results[0]; const previousTemplate = results[1]; const diff = jsonDiff.diffString(previousTemplate, currentTemplate); console.log(diff); return null; }).catch(error => { console.error(error); return null; }); });
  2. extension AppDelegate : MessagingDelegate { func messaging(_ messaging: Messaging, didReceiveRegistrationToken

    fcmToken: String) { Messaging.messaging().subscribe(toTopic: "PUSH_RC") { error in print("Subscribed to PUSH_RC topic") } } }
  3. import * as functions from 'firebase-functions'; import * as admin

    from ‘firebase-admin'; admin.initializeApp(); exports.pushConfig = functions.remoteConfig.onUpdate(versionMetadata => { // Create FCM payload to send data message to PUSH_RC topic. const payload: admin.messaging.MessagingPayload = { data: { 'CONFIG_STATE': ‘STALE' } }; const options: admin.messaging.MessagingOptions = { contentAvailable: true } // Use the Admin SDK to send the ping via FCM. return admin.messaging() .sendToTopic('PUSH_RC', payload, options) .then(resp => { console.log(resp); return null; }); });
  4. // Silent push notification func application(_ application: UIApplication, didReceiveRemoteNotification userInfo:

    [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { print("Entire message \(userInfo)") if let _ = userInfo["CONFIG_STATE"] as? String { UserDefaults.standard.set(true, forKey: "CONFIG_STALE") } completionHandler(.newData) }
  5. func fetchConfig() { welcomeLabel.text = remoteConfig[loadingPhraseConfigKey].stringValue var expirationDuration = 43200

    if remoteConfig.configSettings.isDeveloperModeEnabled || UserDefaults.standard.bool(forKey: "CONFIG_STALE") { expirationDuration = 0 UserDefaults.standard.set(false, forKey: "CONFIG_STALE") } remoteConfig.fetch(withExpirationDuration: TimeInterval(expirationDuration)) { (status, error) -> Void in let alertAction: UIAlertAction = .init(title: "OK", style: .default, handler: nil) if status == .success { print("Config fetched!") self.remoteConfig.activateFetched() } else { print("Config not fetched") print("Error: \(error?.localizedDescription ?? "No error available.")") } self.displayWelcome() } }