Update Asset Configurations

This example shows:

  • Initialize asset tracking: Initialize Asset tracking with necessary configurations such as API key, fake GPS configuration, and data tracking configuration.
  • Create and Bind Asset: Creates a new asset profile and binds it to the asset tracking service. Ensures that the asset is ready for tracking.
  • Update Data Tracking Configurations: Update data tracking configurations such as batch size, batch window, and storage size. This is achieved by creating a new DataTrackingConfig object and calling the setDataTrackingConfig() method of the asset tracking service.
  • Update Location Configurations: Update location tracking configurations such as tracking mode and smallest displacement. This is done by creating a new LocationConfig object and calling the updateLocationConfig() method of the asset tracking service.
  • Update Notification Configurations: Update notification configurations based on the platform (Android or iOS). For Android, it updates the Android notification configuration, for iOS, it updates the iOS notification configuration. These configurations include settings such as notification channel ID, notification channel name, and asset enable notification title.

update_tracking_configuration.dart view source

1
import 'dart:convert';
2
import 'dart:io';
3
4
import 'package:flutter/material.dart';
5
import 'package:nb_asset_tracking_flutter/nb_asset_tracking_flutter.dart';
6
import 'package:nb_asset_tracking_flutter_example/util/consts.dart';
7
import 'package:nb_asset_tracking_flutter_example/util/toast_mixin.dart';
8
import 'package:uuid/uuid.dart';
9
10
class UpdateConfigurationExample extends StatefulWidget {
11
@override
12
UpdateConfigurationExampleState createState() => UpdateConfigurationExampleState();
13
}
14
15
class UpdateConfigurationExampleState extends State<UpdateConfigurationExample>
16
with ToastMixin
17
implements OnTrackingDataCallBack {
18
bool bindAsset = false;
19
final assetTracking = AssetTracking();
20
String locationInfo = "";
21
String configInfo = "";
22
String assetId = "";
23
bool isTracking = false;
24
25
@override
26
void initState() {
27
super.initState();
28
initAssetTracking();
29
createAndBindAssetId();
30
}
31
32
void initAssetTracking() {
33
assetTracking.initialize(apiKey: accessKey);
34
assetTracking.setFakeGpsConfig(allow: true);
35
assetTracking.addDataListener(this);
36
}
37
38
@override
39
Widget build(BuildContext context) {
40
return Scaffold(
41
appBar: AppBar(
42
toolbarHeight: 0,
43
),
44
body: Container(
45
padding: EdgeInsets.all(16),
46
child: Column(
47
crossAxisAlignment: CrossAxisAlignment.start,
48
children: [
49
ElevatedButton(
50
onPressed: assetId.isNotEmpty
51
? () async {
52
var locationConfig = LocationConfig(trackingMode: TrackingMode.custom, smallestDisplacement: 40);
53
await assetTracking.updateLocationConfig(config: locationConfig);
54
var locationConfigResult = await assetTracking.getLocationConfig();
55
setState(() {
56
configInfo = "locationConfigInfo: ${jsonEncode(locationConfigResult.data)}";
57
});
58
}
59
: null,
60
child: const Text("Update Location Config"),
61
),
62
ElevatedButton(
63
onPressed: assetId.isNotEmpty
64
? () async {
65
var androidNotificationConfig =
66
AndroidNotificationConfig(channelId: "testChannelId", channelName: "newChannelName");
67
var iOSNotificationConfig = IOSNotificationConfig();
68
iOSNotificationConfig.showAssetEnableNotification = false;
69
iOSNotificationConfig.showAssetDisableNotification = true;
70
var assetEnableConfig = AssetEnableNotificationConfig(identifier: "iosIdentifier");
71
assetEnableConfig.title = "New asset enable notification title";
72
iOSNotificationConfig.assetEnableNotificationConfig = assetEnableConfig;
73
74
if (Platform.isAndroid) {
75
assetTracking.setAndroidNotificationConfig(config: androidNotificationConfig);
76
var androidConfig = await assetTracking.getAndroidNotificationConfig();
77
setState(() {
78
configInfo = "notificationConfig: ${jsonEncode(androidConfig.data)}";
79
});
80
} else {
81
assetTracking.setIOSNotificationConfig(config: iOSNotificationConfig);
82
var iosConfig = await assetTracking.getIOSNotificationConfig();
83
setState(() {
84
configInfo = "notificationConfig: ${jsonEncode(iosConfig.data)}";
85
});
86
}
87
}
88
: null,
89
child: const Text("Update Notification Config"),
90
),
91
ElevatedButton(
92
onPressed: assetId.isNotEmpty
93
? () async {
94
var dataTrackingConfig = DataTrackingConfig(
95
dataUploadingBatchSize: 15, dataUploadingBatchWindow: 30, dataStorageSize: 5000);
96
await assetTracking.setDataTrackingConfig(config: dataTrackingConfig);
97
var dataTrackingConfigInfo = await assetTracking.getDataTrackingConfig();
98
setState(() {
99
configInfo = "dataTrackingConfigInfo: ${jsonEncode(dataTrackingConfigInfo.data)}";
100
});
101
}
102
: null,
103
child: const Text("Update DataTracking Config"),
104
),
105
Padding(
106
padding: const EdgeInsets.only(top: 18.0),
107
child: Text(configInfo),
108
),
109
Padding(
110
padding: const EdgeInsets.only(top: 28.0),
111
child: Text(locationInfo),
112
),
113
],
114
),
115
),
116
);
117
}
118
119
void createAndBindAssetId() async {
120
AssetProfile profile = AssetProfile(
121
customId: const Uuid().v4().toString(), name: "test asset", description: "asset descriptions", attributes: {});
122
AssetResult result = await assetTracking.createAsset(profile: profile);
123
if (result.success) {
124
String assetID = result.data;
125
var assetResult = await assetTracking.bindAsset(customId: assetID);
126
if (assetResult.success) {
127
showToast("Asset ${result.data} bind success");
128
setState(() {
129
assetId = assetResult.data;
130
bindAsset = true;
131
});
132
} else {
133
showToast(assetResult.msg.toString());
134
}
135
} else {
136
showToast(result.msg.toString());
137
}
138
}
139
140
@override
141
void onLocationFailure(String message) {}
142
143
@override
144
void onLocationSuccess(NBLocation location) {
145
setState(() {
146
locationInfo = "------- Location Info ------- \n"
147
"Provider: ${location.provider} \n"
148
"Latitude: ${location.latitude}\n"
149
"Longitude: ${location.longitude}\n"
150
"Altitude: ${location.altitude}\n"
151
"Accuracy: ${location.accuracy}\n"
152
"Speed: ${location.speed}\n"
153
"Bearing: ${location.heading}\n"
154
"Time: ${location.timestamp}\n";
155
});
156
}
157
158
@override
159
void onTrackingStart(String message) {
160
setState(() {
161
isTracking = true;
162
});
163
}
164
165
@override
166
void onTrackingStop(String message) {
167
setState(() {
168
isTracking = false;
169
locationInfo = "";
170
});
171
}
172
173
@override
174
void dispose() {
175
super.dispose();
176
assetTracking.removeDataListener(this);
177
assetTracking.stopTracking();
178
}
179
}

Here are the code highlights for each of the four functionalities:

  1. Initialize Asset Tracking

    1. assetTracking.initialize(apiKey: accessKey): Initializes asset tracking with the provided API key.
    2. assetTracking.setFakeGpsConfig(allow: true): Allows using fake GPS configurations.
    3. assetTracking.addDataListener(this): Adds a data listener to track data updates.
    4. assetTracking.setDataTrackingConfig(config: DataTrackingConfig(baseUrl: baseUrlStaging)): Sets data tracking configurations such as the base URL.
  2. Create and Bind Asset

    1. AssetProfile profile = AssetProfile(...): Defines the profile of the asset to be created.
    2. AssetResult result = await assetTracking.createAsset(profile: profile): Creates a new asset with the specified profile.
    3. var assetResult = await assetTracking.bindAsset(customId: assetID): Binds the created asset to the tracking service.
  3. Update Data Tracking Configurations

    1. var dataTrackingConfig = DataTrackingConfig(...): Defines the data tracking configurations to be updated.
    2. await assetTracking.setDataTrackingConfig(config: dataTrackingConfig): Updates the data tracking configurations.
    3. var dataTrackingConfigInfo = await assetTracking.getDataTrackingConfig(): Retrieves the updated data tracking configurations.
  4. Update Location Configurations

    1. var locationConfig = LocationConfig(...): Defines the location configurations to be updated.
    2. await assetTracking.updateLocationConfig(config: locationConfig): Updates the location tracking configurations.
    3. var locationConfigResult = await assetTracking.getLocationConfig(): Retrieves the updated location tracking configurations.
  5. Update Notification Configurations

    1. For Android:
      1. var androidNotificationConfig = AndroidNotificationConfig(...): Defines the Android notification configurations.
      2. assetTracking.setAndroidNotificationConfig(config: androidNotificationConfig);: Sets the Android notification configurations.
      3. var androidConfig = await assetTracking.getAndroidNotificationConfig();: Retrieves the updated Android notification configurations.
    2. For iOS:
      1. var iOSNotificationConfig = IOSNotificationConfig(...): Defines the iOS notification configurations.
      2. assetTracking.setIOSNotificationConfig(config: iOSNotificationConfig);: Sets the iOS notification configurations.
      3. var iosConfig = await assetTracking.getIOSNotificationConfig();: Retrieves the updated iOS notification configurations.

© 2024 NextBillion.ai all rights reserved.