Skip to content

Commit

Permalink
Fix adding message token error when creating new account
Browse files Browse the repository at this point in the history
- Set an empty token field and update the field 1 second later
- A quick and dirty solution that works after delaying the update by 1 second
  • Loading branch information
rayjasson98 committed Jan 6, 2021
1 parent a27eeb6 commit 84241d4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
18 changes: 15 additions & 3 deletions lib/data/user/repositories/user_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,25 @@ class UserRepository {
String get displayName => FirebaseAuth.instance.currentUser.displayName;

Future<void> saveToken(String token) async {
await _userDoc.update({
await _userDoc.set({
'tokens': FieldValue.arrayUnion([token]),
}, SetOptions(merge: true));
// Quick and dirty solution, it just works.
Future.delayed(Duration(seconds: 1), () {
_userDoc.update({
'tokens': FieldValue.arrayUnion([token]),
});
});
}

Future<void> update(AppUser appUser) async {
await _userDoc.update(appUser.toMap());
Future<void> deleteToken(String token) {
return _userDoc.update({
'tokens': FieldValue.arrayRemove([token]),
});
}

Future<void> update(AppUser appUser) {
return _userDoc.update(appUser.toMap());
}

Future<void> delete() => _userDoc.delete();
Expand Down
10 changes: 7 additions & 3 deletions lib/ui/profile/user_profile_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:farmassist/bloc/authentication/authentication.dart';
import 'package:farmassist/ui/profile/avatar.dart';
import 'package:farmassist/ui/profile/user_info_field.dart';
import 'package:farmassist/ui/widgets/tab_page.dart';
import 'package:farmassist/utils/message_handler.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

Expand Down Expand Up @@ -51,9 +52,12 @@ class _LogOutButton extends StatelessWidget {
borderRadius: BorderRadius.circular(30.0),
),
color: const Color(0xFFFFD600),
onPressed: () => context
.read<AuthenticationBloc>()
.add(AuthenticationLogoutRequested()),
onPressed: () async {
await context.read<MessageHandler>().deleteToken();
context
.read<AuthenticationBloc>()
.add(AuthenticationLogoutRequested());
},
),
),
),
Expand Down
9 changes: 7 additions & 2 deletions lib/utils/message_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ class MessageHandler {

FirebaseMessaging _messaging;
UserRepository _userRepository;
String _token;

Future<void> generateToken() async {
String token = await _messaging.getToken();
await _userRepository.saveToken(token);
_token = await _messaging.getToken();
await _userRepository.saveToken(_token);
_messaging.onTokenRefresh.listen(_userRepository.saveToken);
}

Future<void> deleteToken() {
return _userRepository.deleteToken(_token);
}
}

0 comments on commit 84241d4

Please sign in to comment.