Does Firebase Analytics Work Offline?

后端 未结 5 1563
悲&欢浪女
悲&欢浪女 2020-12-09 16:24

I know Firebase in general works offline, and syncs whenever an opportunity. Does the same thing apply to Firebase-Analytics for mobile (Android, iOS) apps?

If yes (

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-09 17:10

    It's possible to bypass the 72 hours time limit on analytics. First, you need to add the permission

    
    

    in your manifest file. Before logging events, check if internet is available using this method:

    private boolean isNetworkAvailable() {
            ConnectivityManager connectivityManager
                    = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
            return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    }
    

    using the method above, check if the user is online if so, log events as usual, if not, don't log it right now but save the event name and its parameters as string, using SharedPreferences.

    if(isNetworkAvailable()){
    // the user is online, log the events
    }else{
    // Don't log the events, save the event name and its parameters using SharedPreferences
    }
    

    Register BroadcastReceiver listening connectivity change events, add the following in your manifest file:

    
            
                
                
            
    
    

    In the class extending BroadcastReceiver, check if network is available and log the events saved by SharedPreferences

    public class NetworkEnabledBroadcastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if(isNetworkAvailable(context)){
            //Check if there are saved events and parameters using the same named
            //SharedPreferences used for saving the events and parameters, log if 
            //so, then clear the data. 
            }
        }
        private boolean isNetworkAvailable(Context context) {
            ConnectivityManager connectivityManager
                    = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
            return activeNetworkInfo != null && activeNetworkInfo.isConnected();
        }
    }
    

提交回复
热议问题