BlueID Synchronization
BlueID Synchronization
Introduction
After a successful initialization of your BlueID Mobile Device instance, it's time to synchronize to find out if BlueID Tokens were already issued to it. The duration of a synchronization strongly depends on the internet connection and can be quite time consuming in case of a bad network connection. Thus we strongly recommend to ensure the user interface won't be blocked during the synchronization process.
Synchronization trigger
A synchronization can be triggered in different ways:
-
Manually by the user
-
Periodically while the App is in background
-
Automatically when the App enters foreground
-
Automatically by notifications from the backend
Depending on your use case an individual combination of these approaches might be best.
Last synchronization date
In both Mobile SDKs exists the method getLastSynchronizationDate to determine the date of the last successfully completed synchronization. Depending on your use case you might need this date to find out if another synchronization needs to get triggered to get the most recent BlueID Tokens on the Mobile Device.
iOS
The synchronization method of the BlueID SDK for iOS is implemented asynchronously and thus won't block your main UI thread unless you use UI Controls like UIActivityIndicatorView until the completion handler fires. In many use cases it's more convenient for the user if he doesn't have to synchronize manually, but instead the app triggers synchronization automatically to ensure that BlueID Tokens are always up to date on the Mobile Device. This can be achieved by the following approaches:
Synchronization while App in background
Important: The synchronization in background is only possible, if the BlueID Mobile Device was initialized with BlueIDKeychainSecurityLevel set to BlueIDKeychainSecurityLevelBackgroundTasksEnabled. Otherwise the synchronization will fail when the iPhone is locked, because the keychain access is blocked then.
For this use case, iOS offers the Background Fetch mode. To use this background mode, you need to add the following lines to your Info.plist file:
<key>UIBackgroundModes</key>
<array>
<string>fetch</string>
</array>
Furthermore you need to specify the minimum background fetch interval, which means the minimum time interval in seconds between subsequent background fetches, in AppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[application setMinimumBackgroundFetchInterval:UIApplicationBackgroundFetchIntervalMinimum];
return YES;
}
In the snippet above the interval is set to UIApplicationBackgroundFetchIntervalMinimum, which is the smallest fetch interval supported by the OS. In Tests with our Apps this setting led to an interval of around 30 minutes. Please keep in mind, that the time interval you specify is just an indication for the OS, which finally decides based on the needs of other apps and available resources, when your App will get the time to fetch data. When a good opportunity arises, the OS calls the App delegate's method application:performFetchWithCompletionHandler: after launching your App in background:
- (void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
if ([[BlueIDMobileDevice sharedInstance] isInitialized]) {
[[BlueIDMobileDevice sharedInstance] synchronizeWithCompletionHandler:^(NSError *error) {
if (error) {
completionHandler(UIBackgroundFetchResultFailed);
} else {
completionHandler(UIBackgroundFetchResultNewData);
}
}];
}
}
Synchronization when App enters foreground
Due to the undetermined nature of the Background Fetch mode, it's useful to synchronize the App too, when it enters foreground. Therefore you just have to implement a suitable method of the App delegate (e.g. applicationWillEnterForeground:) appropriately. As BlueID SDK 2.8 and higher supports synchronization and command execution at the same time, this won't effect the performance of a command execution.
Synchronization by notification from backend
This solution ensures, that your App gets immediately notified when new tokens are available that need to get synchronized. This can be achieved by implementing Apple Push Notifications not only in the App but also in your backend. Detailed information can be found in Apple's Local and Remote Notification Programming Guide.
Android
The synchronization method of the BlueID SDK for Android is implemented synchronously/blocking and thus should not be executed in the UI thread. Instead the BlueID synchronization should be executed in a background thread.
The BlueID synchronization uses a locking mechanism (on a BlueIDMobileDevice instance) so it is safe to start the BlueID synchronization from several threads, but the calls to the BlueID synchronization will not be executed in parallel.
Depending on the requirements of your app the synchronization strategies listed in the introduction can be applied.
Periodical Synchronization
If your app requires periodical BlueID synchronization and synchronization while your app is not in the foreground you could execute the BlueID synchronization in a started Android Service.
The service can generate Android Broadcasts to notify Activities that the UI should be updated. The Activities should then register an Android BroadcastReceiver.
Executing the synchronization periodically will put additional strain on the battery of the Mobile Device.
Synchronization when App enters foreground
In order to start the BlueID synchronization when your app enters foreground you can use the lifecycle events of the active Activity, for instance. An applicable event is the onStart() callback, but be aware that this callback is also triggered after configuration changes.
import de.baimos.blueid.sdk.api.BlueIDMobileDevice;
import de.baimos.blueid.sdk.api.SdkForAndroid;
import de.baimos.blueid.sdk.api.exceptions.RemoteException;
...
public class MyActivity extends Activity {
...
@Override
public void onStart(){
super.onStart();
this.synchronizeInBackground();
}
public void synchronizeInBackground(){
final BlueIDMobileDevice sdk = SdkForAndroid.getInstance(this);
if(this.sdk != null && this.sdk.isInitialized()) {
new android.os.AsyncTask<Void, Void, Boolean>() {
protected Boolean doInBackground(Void... voids) {
boolean success = false;
try {
sdk.synchronize();
success = true;
} catch (RemoteException ex) {
Log.i(TAG, "BlueID SDK synchronization failed.");
}
return success;
}
protected void onPostExecute(Boolean result) {
if (result) {
MyActivity.this.updateUIAfterSync();
}
}
}.execute();
}
}
...
}
Synchronization by notification from backend
This solution ensures, that your App gets immediately notified when new tokens are available that need to get synchronized. For implementing push notifications services like Firebase Cloud Messaging can be used.