Skip to content

Commit 84ff74f

Browse files
Brazold3xvn
andauthored
fix(push): send createDevice request only once per token change (#777)
* send createDevice request only once per token change * store token when request succeeds * PR tweaks --------- Co-authored-by: Deven Joshi <[email protected]>
1 parent 07914a5 commit 84ff74f

File tree

5 files changed

+52
-5
lines changed

5 files changed

+52
-5
lines changed

packages/stream_video/lib/src/stream_video.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ class StreamVideo extends Disposable {
262262
/// Connects the user to the Stream Video service.
263263
Future<Result<UserToken>> connect({
264264
bool includeUserDetails = true,
265+
bool registerPushDevice = true,
265266
}) async {
266267
if (currentUserType == UserType.anonymous) {
267268
_logger.w(() => '[connect] rejected (anonymous user)');
@@ -271,6 +272,7 @@ class StreamVideo extends Disposable {
271272
}
272273
_connectOperation ??= _connect(
273274
includeUserDetails: includeUserDetails,
275+
registerPushDevice: registerPushDevice,
274276
).asCancelable();
275277
return _connectOperation!
276278
.valueOrDefault(Result.error('connect was cancelled'))
@@ -293,6 +295,7 @@ class StreamVideo extends Disposable {
293295

294296
Future<Result<UserToken>> _connect({
295297
bool includeUserDetails = false,
298+
bool registerPushDevice = true,
296299
}) async {
297300
_logger.i(() => '[connect] currentUser.id: ${_state.currentUser.id}');
298301
if (_connectionState.isConnected) {
@@ -339,7 +342,9 @@ class StreamVideo extends Disposable {
339342
_subscriptions.add(_idAppState, lifecycle.appState.listen(_onAppState));
340343

341344
// Register device with push notification manager.
342-
pushNotificationManager?.registerDevice();
345+
if (registerPushDevice) {
346+
pushNotificationManager?.registerDevice();
347+
}
343348

344349
if (pushNotificationManager != null) {
345350
_subscriptions.add(

packages/stream_video_flutter/CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
## Unreleased
2+
3+
🔄 Dependency updates
4+
* Updated firebase dependencies to fix Xcode 16 build issues
5+
6+
✅ Added
7+
* Added `registerPushDevice` optional parameter (default as true) to `StreamVideo.connect()` method that can prevent automatic push token registration.
8+
9+
🐞 Fixed
10+
* Automatic push token registration done by `StreamVideo` now stores registered token in `SharedPreferences` and will now only perform API call when token changes.
11+
112
## 0.5.5
213

314
🐞 Fixed

packages/stream_video_push_notification/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## Unreleased
2+
3+
🔄 Dependency updates
4+
* Updated firebase dependencies to fix Xcode 16 build issues
5+
16
## 0.5.5
27
* Sync version with `stream_video_flutter` 0.5.5
38

packages/stream_video_push_notification/lib/src/stream_video_push_notification.dart

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'package:firebase_messaging/firebase_messaging.dart';
44
import 'package:flutter_callkit_incoming/entities/entities.dart';
55
import 'package:flutter_callkit_incoming/flutter_callkit_incoming.dart';
66
import 'package:rxdart/rxdart.dart';
7+
import 'package:shared_preferences/shared_preferences.dart';
78
import 'package:stream_video/stream_video.dart' hide CallEvent;
89
import 'package:stream_video_push_notification/stream_video_push_notification_platform_interface.dart';
910

@@ -20,6 +21,9 @@ const _idCallRejected = 6;
2021

2122
/// Implementation of [PushNotificationManager] for Stream Video.
2223
class StreamVideoPushNotificationManager implements PushNotificationManager {
24+
static const userDeviceTokenKey = 'io.getstream.userDeviceToken';
25+
static const userDeviceTokenVoIPKey = 'io.getstream.userDeviceTokenVoIP';
26+
2327
/// Factory for creating a new instance of [StreamVideoPushNotificationManager].
2428
/// /// Parameters:
2529
/// * [callerCustomizationCallback] callback providing customized caller data used for call screen and CallKit call. (for iOS this will only work for foreground calls)
@@ -67,6 +71,8 @@ class StreamVideoPushNotificationManager implements PushNotificationManager {
6771
}) : _client = client {
6872
if (CurrentPlatform.isWeb) return;
6973

74+
SharedPreferences.getInstance().then((prefs) => _sharedPreferences = prefs);
75+
7076
subscribeToEvents() {
7177
_subscriptions.add(
7278
_idCallEnded,
@@ -155,6 +161,7 @@ class StreamVideoPushNotificationManager implements PushNotificationManager {
155161
final StreamVideoPushParams pushParams;
156162
final CallerCustomizationFunction? callerCustomizationCallback;
157163
final bool registerApnDeviceToken;
164+
late SharedPreferences _sharedPreferences;
158165

159166
final _logger = taggedLogger(tag: 'SV:PNManager');
160167
bool? _wasWsConnected;
@@ -174,13 +181,24 @@ class StreamVideoPushNotificationManager implements PushNotificationManager {
174181
return;
175182
}
176183

177-
void registerDevice(String token, bool isVoIP) {
178-
_client.createDevice(
184+
void registerDevice(String token, bool isVoIP) async {
185+
final tokenKey = isVoIP ? userDeviceTokenVoIPKey : userDeviceTokenKey;
186+
187+
final storedToken = _sharedPreferences.getString(tokenKey);
188+
if (storedToken == token) return;
189+
190+
_client
191+
.createDevice(
179192
id: token,
180193
voipToken: isVoIP,
181194
pushProvider: pushProvider.type,
182195
pushProviderName: pushProvider.name,
183-
);
196+
)
197+
.then((result) {
198+
if (result is Success) {
199+
_sharedPreferences.setString(tokenKey, token);
200+
}
201+
});
184202
}
185203

186204
if (CurrentPlatform.isIos && registerApnDeviceToken) {
@@ -197,6 +215,11 @@ class StreamVideoPushNotificationManager implements PushNotificationManager {
197215
.listen((token) => registerDevice(token, true)));
198216
}
199217

218+
Future<void> removedStoredTokens() async {
219+
await _sharedPreferences.remove(userDeviceTokenKey);
220+
await _sharedPreferences.remove(userDeviceTokenVoIPKey);
221+
}
222+
200223
@override
201224
void unregisterDevice() async {
202225
final token = await getDevicePushTokenVoIP();
@@ -209,6 +232,8 @@ class StreamVideoPushNotificationManager implements PushNotificationManager {
209232
if (apnToken != null) {
210233
_client.deleteDevice(id: apnToken);
211234
}
235+
236+
removedStoredTokens();
212237
}
213238

214239
@override

packages/stream_video_push_notification/pubspec.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ dependencies:
2424
rxdart: ^0.28.0
2525
stream_video: ^0.5.5
2626
uuid: ^4.2.1
27-
27+
shared_preferences: ^2.3.2
28+
2829
dev_dependencies:
2930
build_runner: ^2.4.4
3031
flutter_lints: ^2.0.2

0 commit comments

Comments
 (0)