Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Analyze the full record of real-time remote configuration in iOS

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "analyzing the full record of real-time remote configuration in iOS". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Preface

What do you do when you need to roll out remote configuration changes quickly? Maybe the last changes you made didn't work, and you want to undo them as soon as possible. Or if there is a problem with your application, you need to disable the entire feature.

Calling fetchWithExpirationDuration:completionHandler: (with less expiration time) more frequently is one way, but Firebase discourages it. You risk being taken off the shelves.

What if you need to invalidate the remote configuration cache without having to hard-code the short expiration time? Instead of repeatedly asking the server for updates, let the server notify you of any changes. Firebase remote configuration is now integrated with cloud capabilities, so execution can be triggered when the configuration is released or rolled back. This means that you can have a function send a silent push notification to your application to let it know that the configuration has changed.

When a callback is triggered in AppDelegate, a silent push notification is not displayed to the user. By setting the option content_available to true, you can even have iOS launch your application in the background (or restore it) when the push notification arrives. One problem: if the user kills it manually, iOS will not be able to start your application in the background. This will continue until the next reboot (after the first unlock). Other than that, you have no other questions.

Realize

This process boils down to:

Subscribe an application to a Firebase Cloud Messaging topic create a cloud function triggered when a remote configuration change handles silent push notifications in AppDelegate invalidate the remote configuration cache on next startup

How to set up Firebase cloud messaging is beyond the scope of this article, but documentation is a good starting point.

1. Subscribe to a topic

After the push notification is up and running, we need to subscribe the application to the topic. We will specialize in remote configuration.

Messaging.messaging () .subscribe (toTopic: "REMOTE_CONFIG") {error in if let error = error {debugPrint ("Could not subscribe to Remote Config topic", error)}}

two。 Create a cloud service

Cloud Function is just a Node.js script, so you need to use Node.js. Net on your computer. At the time of this writing, Cloud Functions supports Node V6 or Node V8, where V6 is the default.

After you get the Node.js, install Firebase CLI first.

Npm install-g firebase-tools

Then use the CLI tool for authentication.

Firebase login

Now create a folder for the function, and cd enters it and initializes the Firebase project.

Firebase init functions

This launches the CLI wizard. Finish it, and you'll be ready.

It is time to add a cloud feature that will send silent push notifications to the REMOTE_CONFIG channel. In the function directory of the newly created project, you will find index.js. Open it and replace the content with the following:

Const functions = require ('firebase-functions'); const admin = require (' firebase-admin'); admin.initializeApp (functions.config () .firebase); exports.pushConfig = functions.remoteConfig.onUpdate (versionMetadata = > {/ / Create FCM payload to send data message to REMOTE_CONFIG topic. Const payload = {data: {CONFIG_STATE: 'STALE'}}; const options = {content_available: true}; / / Use the Admin SDK to send the ping via FCM. Return admin. Messaging () .sendToTopic ('REMOTE_CONFIG', payload, options). Then (response = > {console.log (response); return null;});})

We use notifications to pass the payload with the key CONFIG_STATE. This will signal to your application that the remote configuration data has changed. If necessary, the option content_available is necessary for iOS to restore the application or start it in the background.

Now that this feature is in place, we can deploy it to Firebase so that it can run.

Firebase deploy-only functions

This will be deployed to the Firebase project selected in the firebase init function. To deploy to another project, first use firebase to add it using-- add. Once added, you can use my-project-alias to switch projects running firebase.

3. Handle remote push

We need to implement the method in AppDelegate: didReceiveRemoteNotification:fetchCompletionHandler: to handle incoming push notifications. If the userInfo dictionary has the key CONFIG_STATE (the one we added to the notification payload), then it is the notification we are looking for.

Func application (_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @ escaping (_: UIBackgroundFetchResult)-> Void) {if userInfo ["CONFIG_STATE"]! = nil {UserDefaults.standard.set (true, forKey: "CONFIG_STATE") UserDefaults.standard.synchronize ()} completionHandler (UIBackgroundFetchResult.newData)}

It's easy to call fetchWithExpirationDuration:completionHandler here: it has very little expiration time to retrieve the updated value at that time. However, doing so causes all application instances of notifications to request new values at roughly the same time. This is likely to limit the application, so we will postpone it until the next release.

4. Invalidate the remote configuration cache

We just need to save the flag to UserDefaults and check it before getting the Remote Config value.

Var expirationDuration: TimeInterval = 43200 / / 12hs of cache by defaultif UserDefaults.standard.bool (forKey: "CONFIG_STATE") {UserDefaults.standard.set (false, forKey: "CONFIG_STATE") UserDefaults.standard.synchronize () expirationDuration = 0} RemoteConfig.fetch (withExpirationDuration: expirationDuration) {[weak self] status, error in RemoteConfig.remoteConfig (). ActivateFetched ()}

If the configuration is out of date, we will bypass the cache by setting the expiration duration to 0. This forces a full reload without having to wait for the cache to expire.

These are all the settings. Now go to the remote configuration panel and publish the changes. Then look at the Features tab.

This is the end of the content of "full record of Real-time remote configuration in iOS". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report