Skip to content

Commit

Permalink
Update notifications API request methods, fix activity not ending
Browse files Browse the repository at this point in the history
  • Loading branch information
ivirtex committed Nov 1, 2023
1 parent 1f652af commit 6ee9e03
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 24 deletions.
25 changes: 4 additions & 21 deletions packages/notifications_api/lib/src/notifications_api.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Dart imports:
import 'dart:convert';
import 'dart:developer';
import 'dart:io';

Expand Down Expand Up @@ -32,19 +31,11 @@ class NotificationsApi {

/// Sends the given [token] to the API.
Future<void> sendLiveActivityPushToken(String token) async {
final request = Uri.https(baseUrl, '/starcat/v1/push_token');
final request = Uri.https(baseUrl, '/starcat/v1/push_token/$token');

log('sendLiveActivityPushToken: $request');

final response = await _httpClient.post(
request,
headers: {
'Content-Type': 'application/json',
},
body: jsonEncode({
'token': token,
}),
);
final response = await _httpClient.post(request);

if (response.statusCode != HttpStatus.created &&
response.statusCode != HttpStatus.ok) {
Expand All @@ -53,19 +44,11 @@ class NotificationsApi {
}

Future<void> invalidateLiveActivityPushToken(String token) async {
final request = Uri.https(baseUrl, '/starcat/v1/push_token');
final request = Uri.https(baseUrl, '/starcat/v1/push_token/$token');

log('invalidateLiveActivityPushToken: $request');

final response = await _httpClient.delete(
request,
headers: {
'Content-Type': 'application/json',
},
body: jsonEncode({
'token': token,
}),
);
final response = await _httpClient.delete(request);

if (response.statusCode == HttpStatus.notFound) {
throw TokenAlreadyInvalidatedException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,11 @@ class NotificationsRepository {
},
// ignore: body_might_complete_normally_nullable
ended: (state) async {
log('Activity is inactive, invalidating token');
log('Activity is inactive, invalidating token ${state.activityId}');

try {
await _notificationsApi.invalidateLiveActivityPushToken(id);
await _notificationsApi
.invalidateLiveActivityPushToken(activityPushToken!);
} catch (e) {
log('Failed to invalidate push token', error: e);

Expand All @@ -101,7 +102,7 @@ class NotificationsRepository {
Future<void> cancelLiveActivityTracking(String id) async {
await activityUpdateStream!.cancel();
await _liveActivitiesPlugin.endActivity(id);
await _notificationsApi.invalidateLiveActivityPushToken(id);
await _notificationsApi.invalidateLiveActivityPushToken(activityPushToken!);
activityPushToken = null;
}
}

0 comments on commit 6ee9e03

Please sign in to comment.