3 minute read
AtClientManager.getInstance()
.setCurrentAtSign
accepts the following arguments: currentAtSign,
namespace and the preferences.
AtClientManager.getInstance().setCurrentAtSign('@alice', 'wavi', <preferences>);
The AtClientManger
Instance has getter atClient
which returns an
instance of AtClient
.
AtClient atClient = atClientManager.atClient;
The AtClientManager
instance has a late initialized variable
notificationService
which is for accessing notification service methods.
NotificationService notificationService = atClientManager.notificationService;
The AtClientManager
instance has a late initialized variable syncService
which is for invoking the sync.
SyncService syncService = atClientManager.syncService;
Note: Above code should be executed every time when the @sign is switched to get the right instances representing the new @sign.
Get the AtClientManager
instance initially.
Access the notificationService
variable using atClientManager instance.
Listen to notifications via callback and no filter. Ideally you don’t want to do this.
/// AtClientManager instance.
AtClientManager atClientManager = AtClientManager.getInstance();
/// NotificationService variable.
NotificationService notificationService = atClientManager.notificationService;
/// Listen to notifications.
notificationService.subscribe().listen((notification) {
_notificationCallback(notification);
});
Listen to notifications via callback and filter notification key by regex. You can also come up with regexes that match other types of keys. Ex: ‘wavi | buzz’ or alternatively multiple listeners can also be registered.
notificationService.subscribe(regex: '.wavi').listen((notification) {
_notificationCallback(notification);
});
Send notification. Await variant.
/// AtKey
AtKey phoneKey = AtKey()
..key = 'phone'
..sharedWith = '@bob🛠'
..sharedBy = '@alice🛠';
/// AtKey's Value (String)
String atValue = '+1 100 200 300';
/// Update the value and capture the notification result.
NotificationResult notificationResponse = await notificationService
.notify(NotificationParams.forUpdate(phoneKey, value: atValue));
Validating if a notification failed in the await variant.
if(notificationResponse.notificationStatusEnum == NotificationStatusEnum.undelivered) {
// Do something on notification error.
print(notificationResponse.atClientException);
} else{
// Do something on successful delivery response.
print('Successfully delivered notification.');
}
The other notifications are as follows -
/// Delete notification
NotificationResult notificationResponse = await notificationService.notify(NotificationParams.forDelete(phoneKey));
/// Text notify
NotificationResult notificationResponse = await notificationService.notify(NotificationParams.forText('phone', '@bob🛠'));
Send notification using Callback.
notificationService.notify(
NotificationParams.forUpdate(atKey, value: atValue),
onDone: _onSuccessCallback,
onError: _onErrorCallback,
);
void _onSuccessCallback(notificationResult){
// Do something on successful delivery response.
print(notificationResult);
}
void _onErrorCallback(notificationResult){
// Do something on notification error
print(notificationResponse.atClientException);
}
The atClientManger
instance has getter atClient
which returns an instance
of AtClient
.
final AtClient atClient = atClientManager.atClient;
CRUD operations
atClient.put(<params>)
atClient.delete(<params>)
Syncing the data. Apps no longer have to use SyncStrategy
or
isDedicated
flag or manually call sync
. All sync requests will be
internally kept in a queue and synced to the server at periodic time
interval (approx. 15 seconds). If remote server is updated from some other
device, then those changes will be also synced at periodic intervals.
syncService = atClientManager.syncService;
syncService.sync(onDone: _onSuccessCallback);
void _onSuccessCallback(syncResult){
print(syncResult);
}
Optionally, call setOnDone
for global onDone callback. Call this method
to set the Global onDone callback. This method will be called when a sync is
completed. When a specific onDone function is passed to the sync function,
then the specific onDone is called.
syncService.setOnDone(onDone: _onSuccessCallback);