@@ -10,7 +10,8 @@ class ParseInstallation extends ParseObject {
10
10
static final String keyAppVersion = 'appVersion' ;
11
11
static final String keyAppIdentifier = 'appIdentifier' ;
12
12
static final String keyParseVersion = 'parseVersion' ;
13
- static final List <String > readOnlyKeys = [ //TODO
13
+ static final List <String > readOnlyKeys = [
14
+ //TODO
14
15
keyDeviceToken, keyDeviceType, keyInstallationId,
15
16
keyAppName, keyAppVersion, keyAppIdentifier, keyParseVersion
16
17
];
@@ -24,13 +25,15 @@ class ParseInstallation extends ParseObject {
24
25
25
26
String get deviceToken => super .get <String >(keyDeviceToken);
26
27
27
- set deviceToken (String deviceToken) => set <String >(keyDeviceToken, deviceToken);
28
+ set deviceToken (String deviceToken) =>
29
+ set <String >(keyDeviceToken, deviceToken);
28
30
29
31
String get deviceType => super .get <String >(keyDeviceType);
30
32
31
33
String get installationId => super .get <String >(keyInstallationId);
32
34
33
- set _installationId (String installationId) => set <String >(keyInstallationId, installationId);
35
+ set _installationId (String installationId) =>
36
+ set <String >(keyInstallationId, installationId);
34
37
35
38
String get appName => super .get <String >(keyAppName);
36
39
@@ -42,9 +45,7 @@ class ParseInstallation extends ParseObject {
42
45
43
46
/// Creates an instance of ParseInstallation
44
47
ParseInstallation (
45
- {bool debug,
46
- ParseHTTPClient client,
47
- bool autoSendSessionId})
48
+ {bool debug, ParseHTTPClient client, bool autoSendSessionId})
48
49
: super (keyClassInstallation) {
49
50
_debug = isDebugEnabled (objectLevelDebug: debug);
50
51
_client = client ??
@@ -60,7 +61,8 @@ class ParseInstallation extends ParseObject {
60
61
if (_currentInstallationId == null ) {
61
62
_currentInstallationId = (await _getFromLocalStore ()).installationId;
62
63
}
63
- return _currentInstallationId != null && installation.installationId == _currentInstallationId;
64
+ return _currentInstallationId != null &&
65
+ installation.installationId == _currentInstallationId;
64
66
}
65
67
66
68
/// Gets the current installation from storage
@@ -75,9 +77,12 @@ class ParseInstallation extends ParseObject {
75
77
/// Updates the installation with current device data
76
78
_updateInstallation () async {
77
79
//Device type
78
- if (Platform .isAndroid) set <String >(keyDeviceType, "android" );
79
- else if (Platform .isIOS) set <String >(keyDeviceType, "ios" );
80
- else throw Exception ("Unsupported platform/operating system" );
80
+ if (Platform .isAndroid)
81
+ set <String >(keyDeviceType, "android" );
82
+ else if (Platform .isIOS)
83
+ set <String >(keyDeviceType, "ios" );
84
+ else
85
+ throw Exception ("Unsupported platform/operating system" );
81
86
82
87
//Locale
83
88
String locale = await Devicelocale .currentLocale;
@@ -99,7 +104,8 @@ class ParseInstallation extends ParseObject {
99
104
Future <ParseResponse > create () async {
100
105
var isCurrent = await ParseInstallation .isCurrent (this );
101
106
if (isCurrent) await _updateInstallation ();
102
- ParseResponse parseResponse = await super .create ();
107
+ //ParseResponse parseResponse = await super.create();
108
+ ParseResponse parseResponse = await _create ();
103
109
if (parseResponse.success && isCurrent) {
104
110
saveInStorage (keyParseStoreInstallation);
105
111
}
@@ -110,7 +116,8 @@ class ParseInstallation extends ParseObject {
110
116
Future <ParseResponse > save () async {
111
117
var isCurrent = await ParseInstallation .isCurrent (this );
112
118
if (isCurrent) await _updateInstallation ();
113
- ParseResponse parseResponse = await super .save ();
119
+ //ParseResponse parseResponse = await super.save();
120
+ ParseResponse parseResponse = await _save ();
114
121
if (parseResponse.success && isCurrent) {
115
122
saveInStorage (keyParseStoreInstallation);
116
123
}
@@ -145,4 +152,76 @@ class ParseInstallation extends ParseObject {
145
152
await installation._updateInstallation ();
146
153
return installation;
147
154
}
155
+
156
+ /// Creates a new object and saves it online
157
+ Future <ParseResponse > _create () async {
158
+ try {
159
+ var uri = _client.data.serverUrl + "$keyEndPointInstallations " ;
160
+ var body = json.encode (toJson (forApiRQ: true ));
161
+ if (_debug) {
162
+ logRequest (ParseCoreData ().appName, className,
163
+ ParseApiRQ .create.toString (), uri, body);
164
+ }
165
+ var result = await _client.post (uri, body: body);
166
+
167
+ //Set the objectId on the object after it is created.
168
+ //This allows you to perform operations on the object after creation
169
+ if (result.statusCode == 201 ) {
170
+ final map = json.decode (result.body);
171
+ this .objectId = map["objectId" ].toString ();
172
+ }
173
+
174
+ return handleResponse (this , result, ParseApiRQ .create, _debug, className);
175
+ } on Exception catch (e) {
176
+ return handleException (e, ParseApiRQ .create, _debug, className);
177
+ }
178
+ }
179
+
180
+ /// Saves the current object online
181
+ Future <ParseResponse > _save () async {
182
+ if (getObjectData ()[keyVarObjectId] == null ) {
183
+ return create ();
184
+ } else {
185
+ try {
186
+ var uri =
187
+ "${ParseCoreData ().serverUrl }$keyEndPointInstallations /$objectId " ;
188
+ var body = json.encode (toJson (forApiRQ: true ));
189
+ if (_debug) {
190
+ logRequest (ParseCoreData ().appName, className,
191
+ ParseApiRQ .save.toString (), uri, body);
192
+ }
193
+ var result = await _client.put (uri, body: body);
194
+ return handleResponse (this , result, ParseApiRQ .save, _debug, className);
195
+ } on Exception catch (e) {
196
+ return handleException (e, ParseApiRQ .save, _debug, className);
197
+ }
198
+ }
199
+ }
200
+
201
+ ///Subscribes the device to a channel of push notifications.
202
+ void subscribeToChannel (String value) {
203
+ final List <dynamic > channel = [value];
204
+ this .addUnique ("channels" , channel);
205
+ }
206
+
207
+ ///Unsubscribes the device to a channel of push notifications.
208
+ void unsubscribeFromChannel (String value) {
209
+ final List <dynamic > channel = [value];
210
+ this .removeAll ("channels" , channel);
211
+ }
212
+
213
+ ///Returns an <List<String>> containing all the channel names this device is subscribed to.
214
+ Future <List <dynamic >> getSubscribedChannels () async {
215
+ print ("getSubscribedChannels" );
216
+ final apiResponse =
217
+ await ParseObject (keyClassInstallation).getObject (this .objectId);
218
+
219
+ if (apiResponse.success) {
220
+ var installation = apiResponse.result as ParseObject ;
221
+ print ("achou installation" );
222
+ return Future .value (installation.get <List <dynamic >>("channels" ));
223
+ } else {
224
+ return null ;
225
+ }
226
+ }
148
227
}
0 commit comments