Skip to content

Commit

Permalink
fix: push notification flow. (#458)
Browse files Browse the repository at this point in the history
* fix: push notification flow.

* revert: add rxdart again.

Signed-off-by: Sahil Kumar <[email protected]>

* chore: add todos.

Signed-off-by: Sahil Kumar <[email protected]>

---------

Signed-off-by: Sahil Kumar <[email protected]>
Co-authored-by: Deven Joshi <[email protected]>
  • Loading branch information
xsahil03x and deven98 authored Aug 14, 2023
1 parent 620d281 commit 616f719
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 156 deletions.
27 changes: 23 additions & 4 deletions dogfooding/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'package:uni_links/uni_links.dart';
import 'firebase_options.dart';
import 'repos/app_repository.dart';
import 'repos/auth_repo.dart';
import 'repos/token_service.dart';
import 'repos/user_repository.dart';
import 'src/routes/app_routes.dart';
import 'src/routes/routes.dart';
Expand All @@ -21,13 +22,31 @@ import 'src/utils/providers.dart';

@pragma('vm:entry-point')
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
await Firebase.initializeApp();
await AppRepository.initStreamVideo();
// As this runs in a separate isolate, we need to initialize and connect the
// user to StreamVideo again.
final appRepo = AppRepository();
await appRepo.beginSession();
final authRepo = AuthRepository(
tokenService: TokenService(),
streamVideo: appRepo.videoClient,
streamChat: appRepo.chatClient,
googleSignIn: GoogleSignIn(hostedDomain: 'getstream.io'),
);

final credentials = await UserRepository.instance.getUserCredentials();
if (credentials == null) {
// The user is not logged in, so we don't need to handle the message.
return;
}

await authRepo.loginWithUserInfo(credentials.user);

// Once the setup is done, we can handle the message.
await _handleRemoteMessage(message);
}

Future<void> _handleRemoteMessage(RemoteMessage message) async {
await StreamVideo.instance.handlePushNotification(message.data);
Future<bool> _handleRemoteMessage(RemoteMessage message) {
return StreamVideo.instance.handlePushNotification(message.data);
}

Future<void> main() async {
Expand Down
37 changes: 28 additions & 9 deletions dogfooding/lib/repos/app_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,29 @@ import 'user_repository.dart';
class AppRepository {
AppRepository();

late final StreamVideo _streamVideoClient;
late final StreamChatClient _streamChatClient;
StreamVideo? _streamVideoClient;
StreamChatClient? _streamChatClient;

StreamVideo get videoClient => _streamVideoClient;
StreamVideo get videoClient {
final client = _streamVideoClient;
if (client == null) {
throw Exception(
'Please initialise Stream Video by calling AppRepository.beginSession()',
);
}

return client;
}

StreamChatClient get chatClient => _streamChatClient;
StreamChatClient get chatClient {
final client = _streamChatClient;
if (client == null) {
throw Exception(
'Please initialise Stream Chat by calling AppRepository.beginSession()',
);
}
return client;
}

static Future<StreamVideo> initStreamVideo() async {
if (!StreamVideo.isInitialized()) {
Expand All @@ -44,10 +61,10 @@ class AppRepository {
Future<void> beginSession() async {
_streamVideoClient = await initStreamVideo();
unawaited(_setupLogger());
_streamChatClient = _initChat();
_streamChatClient = initStreamChat();
}

StreamChatClient _initChat() {
StreamChatClient initStreamChat() {
return StreamChatClient(
Env.apiKey,
logLevel: Level.INFO,
Expand Down Expand Up @@ -83,7 +100,7 @@ class AppRepository {
Future<Channel> createChatChannel({
required String channelId,
}) async {
final channel = _streamChatClient.channel(
final channel = chatClient.channel(
kMessageChannelType,
id: channelId,
);
Expand All @@ -93,8 +110,10 @@ class AppRepository {
}

Future<void> endSession() async {
await _streamVideoClient.disconnectUser();
await _streamChatClient.disconnectUser();
await _streamVideoClient?.disconnectUser();
_streamVideoClient = null;
await _streamChatClient?.disconnectUser();
_streamChatClient = null;
await UserRepository.instance.clear();
}
}
4 changes: 2 additions & 2 deletions dogfooding/lib/repos/auth_repo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:stream_video_flutter/stream_video_flutter.dart';

import '../env/env.dart';
import '../src/model/user_credentials.dart';
import 'token_service.dart';
import 'user_repository.dart';

class AuthRepository {
Expand All @@ -24,7 +25,6 @@ class AuthRepository {
final GoogleSignIn googleSignIn;

final _logger = taggedLogger(tag: 'SV:LoginViewState');
// late final _googleSignIn = GoogleSignIn(hostedDomain: 'getstream.io');

UserToken? _userToken;

Expand All @@ -51,7 +51,7 @@ class AuthRepository {
id: data.user.id,
image: data.user.image,
name: data.user.name ?? '',
teams: data.user.teams ?? [],
teams: data.user.teams,
role: data.user.role,
);

Expand Down
File renamed without changes.
48 changes: 0 additions & 48 deletions packages/stream_video/lib/src/persistence/shared_prefs_helper.dart

This file was deleted.

21 changes: 8 additions & 13 deletions packages/stream_video/lib/src/stream_video.dart
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,6 @@ class StreamVideo {

final _tokenManager = TokenManager();
final _subscriptions = Subscriptions();
final _sharedPrefsHelper = SharedPrefsHelper();
late final CoordinatorClient _client;
PushNotificationManager? pushNotificationManager;

Expand Down Expand Up @@ -182,7 +181,6 @@ class StreamVideo {
Future<Result<String>> connectUserWithProvider(
UserInfo user, {
required TokenProvider tokenProvider,
bool saveUser = true,
}) async {
_logger.i(() => '[connectUser] user.id : ${user.id}');
if (currentUser != null) {
Expand All @@ -205,9 +203,6 @@ class StreamVideo {
if (result is Failure) {
return result;
}
if (saveUser) {
await _sharedPrefsHelper.saveUserCredentials(user);
}
_subscriptions.add(_idEvents, _client.events.listen(_onEvent));
_subscriptions.add(_idAppState, lifecycle.appState.listen(_onAppState));
await pushNotificationManager?.onUserLoggedIn();
Expand Down Expand Up @@ -284,7 +279,6 @@ class StreamVideo {
}
try {
await _client.disconnectUser();
await _sharedPrefsHelper.deleteSavedUser();
_subscriptions.cancelAll();
_tokenManager.reset();

Expand Down Expand Up @@ -391,16 +385,17 @@ class StreamVideo {
return result;
}

Future<bool> handlePushNotification(Map<String, dynamic> payload) {
return pushNotificationManager?.handlePushNotification(payload) ??
Future.value(false);
Future<bool> handlePushNotification(Map<String, dynamic> payload) async {
final manager = pushNotificationManager;
if (manager == null) return false;

return manager.handlePushNotification(payload);
}

Future<Call?> consumeIncomingCall() {
Future<Call?> consumeIncomingCall() async {
return pushNotificationManager?.consumeIncomingCall().then((data) {
return data?.let((it) => _makeCallFromCreated(data: it));
}) ??
Future.value();
return data?.let((it) => _makeCallFromCreated(data: it));
});
}
}

Expand Down
2 changes: 0 additions & 2 deletions packages/stream_video/lib/stream_video.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@ export 'src/logger/impl/tagged_logger.dart';
export 'src/logger/stream_log.dart';
export 'src/logger/stream_logger.dart';
export 'src/models/models.dart';
export 'src/persistence/shared_prefs_helper.dart';
export 'src/platform_detector/platform_detector.dart';
export 'src/push_notification/push_notification_manager.dart';
export 'src/sfu/data/models/sfu_connection_quality.dart';
export 'src/sfu/data/models/sfu_track_type.dart';
export 'src/sorting/call_participant_sorting_presets.dart';
export 'src/stream_video.dart';
export 'src/token/token.dart';
export 'src/token/token_service.dart';
export 'src/types/other.dart';
export 'src/utils/result.dart';
export 'src/utils/string.dart';
Expand Down
1 change: 0 additions & 1 deletion packages/stream_video/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ dependencies:
sdp_transform: ^0.3.2
synchronized: ^3.1.0
state_notifier: ^0.7.2+1
shared_preferences: ^2.2.0
fixnum: ^1.1.0
dart_webrtc: ^1.0.17
webrtc_interface: ^1.0.13
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'package:flutter/services.dart';
import 'package:rxdart/rxdart.dart';

// TODO: Verify if this is needed anymore?
// We already have FlutterCallkitIncoming.onEvent; (Event.actionCallIncoming)
class StreamVideoPushNotificationEventChannel {
const StreamVideoPushNotificationEventChannel(
{EventChannel eventChannel =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class StreamVideoPushNotificationMethodChannel {
/// The method channel used to interact with the native platform.
final MethodChannel _methodChannel;

// TODO: Verify if this is needed anymore?
// We already have FlutterCallkitIncoming.getDevicePushTokenVoIP();
/// Obtain the Device Push Token VoIp.
Future<String?> getDevicePushTokenVoIP() async {
return await _methodChannel.invokeMethod<String>('getDevicePushTokenVoIP');
Expand Down
Loading

0 comments on commit 616f719

Please sign in to comment.