How to create push notification one device to another device like chat app? - flutter

I am working on a chat application, I want to push notification for specific user when his message send me so i should deliver message with notification bar.
/// Get the token, save it to the database for current user
_saveDeviceToken() async {
// Get the current user
String uid = 'jeffd23';
// FirebaseUser user = await _auth.currentUser();
// Get the token for this device
String fcmToken = await _fcm.getToken();
// Save it to Firestore
if (fcmToken != null) {
var tokens = _db
.collection('users')
.document(uid)
.collection('tokens')
.document(fcmToken);
await tokens.setData({
'token': fcmToken,
'createdAt': FieldValue.serverTimestamp(), // optional
'platform': Platform.operatingSystem // optional
});
}
}

Related

FireStore when adding FCM token to User doc, deletes and doesn't store UserId

It's my first time using Firestore Cloud Messaging and I want to get the FCM token for each specific device. For quick development, I added the firebase_auth_ui package, which basically outsources the firebase auth login and registration flow. To capture the user's id and store in their doc, I use a simple function that works fine: and gets the job done:
Future<void> addUserDataToFireStore() async {
CollectionReference users = FirebaseFirestore.instance.collection('users');
String uid = FirebaseAuth.instance.currentUser!.uid;
users.doc(uid).set({
'userId': uid,
// 'displayName': currentUser!.displayName!,
});
}
Now, for some reason when I try to access the registration token, my userId gets deleted. When I try to add the token to the same user doc, the userId gets deleted and the fcm token stays. I generate the token as follows:
generateDeviceToken() async {
String? fcmToken = await FirebaseMessaging.instance.getToken();
final userId = FirebaseAuth.instance.currentUser!.uid;
await FirebaseFirestore.instance
.collection('users')
.doc(userId)
.set({'fcmToken': fcmToken});
}
The issue is when I try to call them both. I can't get the two. The doc will fill with either UserId or FCM, but now both. This is what happens when I try to call both,
Perhaps I should make a method that updates fcm token and not set it everytimg?
When you use "set", the entire document is saved with only that one value. Use "update" to update the document and add your token without removing other content.
generateDeviceToken() async {
String? fcmToken = await FirebaseMessaging.instance.getToken();
final userId = FirebaseAuth.instance.currentUser!.uid;
await FirebaseFirestore.instance
.collection('users')
.doc(userId)
.update({'fcmToken': fcmToken});
}
Future<void> addUserDataToFireStore() async {
CollectionReference users = FirebaseFirestore.instance.collection('users');
String uid = FirebaseAuth.instance.currentUser!.uid;
users.doc(uid).update({
'userId': uid,
// 'displayName': currentUser!.displayName!,
});
}
I ended up changing my logic a little bit. Thanks to #Maniak pointing me in the rigth direction. Solution that worked out was the following:
Future<void> addUserDataToFireStore() async {
final userId = FirebaseAuth.instance.currentUser!.uid;
final userDocRef = FirebaseFirestore.instance.collection('users').doc(userId);
final doc = await userDocRef.get();
if (doc.exists) {
return;
} else {
userDocRef.set({
'userId': userId,
});
}
}
Future<void> generateDeviceToken() async {
String? fcmToken = await FirebaseMessaging.instance.getToken();
final userId = FirebaseAuth.instance.currentUser!.uid;
await FirebaseFirestore.instance
.collection('users')
.doc(userId)
.update({'fcmToken': fcmToken});
}

How to update phone number in Firebase when sim card is changed? [duplicate]

In my Flutter app I use Firebase's phone number authentication as my main form of authentication. After authenticating, I create a user in my users collection with these details:
{
phoneNumber: FirebaseAuth.instance.currentUser().phoneNumber,
displayName: 'Comes from user textbox',
...
}
But say one day a user want's to change their phone number. How do I do this? Because I cannot simply change the user's phone number in the document, because the phone number needs to be authenticated. And after authentication, the user gets a new authUID. Which should then be a new user?
Could someone explain the logic behind a user that wants to keep their profile details but change their number.
In order to achieve this, you can use User.updatePhoneNumber. This allows you to update the phone number of a user.
You would use it in the same manner that you also authenticated with phone number in the first place (using signInWithCredential), i.e. you retrieve a credential using FirebaseAuth.verifyPhoneNumber and pass the credential that you get from either verificationCompleted or your user when they enter the SMS code they received. I will only sketch out what this would look like as I assume that you know how to perform this task:
FirebaseAuth.instance.verifyPhoneNumber(
phoneNumber: phoneNumber,
timeout: const Duration(minutes: 2),
verificationCompleted: (credential) async {
await (await FirebaseAuth.instance.currentUser()).updatePhoneNumber(credential);
// either this occurs or the user needs to manually enter the SMS code
},
verificationFailed: null,
codeSent: (verificationId, [forceResendingToken]) async {
String smsCode;
// get the SMS code from the user somehow (probably using a text field)
final AuthCredential credential =
PhoneAuthProvider.getCredential(verificationId: verificationId, smsCode: smsCode);
await (await FirebaseAuth.instance.currentUser()).updatePhoneNumber(credential);
},
codeAutoRetrievalTimeout: null);
When updatePhoneNumber is called, you probably also want to update your database document. Alternatively, you could listen to onAuthStateChanged and update your document this way.
async function save(phone: string, e) {
e.preventDefault();
const { currentUser:fuser } = firebase.auth();
if(fuser && fuser.phoneNumber !== phone) {
try {
const verifier = new firebase.auth.RecaptchaVerifier('recaptcha-container', {
callback: (response) => console.log('callback', response),
size: 'invisible',
});
const phoneProvider = new firebase.auth.PhoneAuthProvider();
const id = await phoneProvider.verifyPhoneNumber(phone, verifier);
const code = window.prompt('Bitte zugeschickten Code eingeben');
const cred = firebase.auth.PhoneAuthProvider.credential(id, code);
await fuser.updatePhoneNumber(cred);
console.log('phone number changed', id, cred, fuser);
setSuccess(true);
} catch(e) {
console.error(e);
}
}
}

How do I print a value from an instance of 'User' in a flutter?

class User {
String token;
User({this.token});
}
class AuthService {
final String url = 'https://reqres.in/api/login';
final controller = StreamController<User>();
Future<User> signIn(String email, String password) async {
final response =
await post(url, body: {'email': email, 'password': password});
final data = jsonDecode(response.body);
final user = _userFromDatabaseUser(data);
// print(user.token);
controller.add(user);
return user;
}
//create user obj based on the database user
User _userFromDatabaseUser(Map user) {
return user != null ? User(token: user['token']) : null;
}
//user stream for provider
Stream<User> get user {
return controller.stream;
}
}
//in Sign in page
onPressed: () async {
if (_formKey.currentState.validate()) {
dynamic result = await _auth.signIn(email, password);
print(result); // Instance of 'User'
}
}
I am new to flutter and want to make an app that only authenticated users. I'm trying to read user token data from a stream. then check that token is not null if I got token then goto home page otherwise it will show error how do I print or store token value?
You can do is when you get the user after the sign In:
User result = await _auth.signIn(email, password);
Then to see the data you can do is
print(result.token);
which will give you the token, and then you can use the shared prefrences to store your token and access it.
Check out the docs for the it: https://pub.dev/packages/shared_preferences
You can override Object.toString method.
you can add this method in your User class to print the token instead of Instance of 'User'.
#override
String toString() {
// TODO: change the below return to your desired string
return "token: $token";
}
You can print using
print(userModel.toString());

Unable to get the Onesignal userid when the user install the app first time in Flutter

I want to store the onesignal User Id in the database when the user install my app first time. I wrote the following code which is not allowing me to do so.
However when the user Logged out from the app and re-login then I am able to retrieve the onesignal user ID and able to save in the Database.
Future<void> initPlatformState(username) async {
if (!mounted) return;
OneSignal.shared.setLogLevel(OSLogLevel.verbose, OSLogLevel.none);
OneSignal.shared.setRequiresUserPrivacyConsent(_requireConsent);
var settings = {
OSiOSSettings.autoPrompt: false,
OSiOSSettings.promptBeforeOpeningPushUrl: true
};
OneSignal.shared
.setSubscriptionObserver((OSSubscriptionStateChanges changes) {
print("SUBSCRIPTION STATE CHANGED: ${changes.jsonRepresentation()}");
});
OneSignal.shared.setPermissionObserver((OSPermissionStateChanges changes) {
print("PERMISSION STATE CHANGED: ${changes.jsonRepresentation()}");
});
OneSignal.shared.setEmailSubscriptionObserver(
(OSEmailSubscriptionStateChanges changes) {
print("EMAIL SUBSCRIPTION STATE CHANGED ${changes.jsonRepresentation()}");
});
await OneSignal.shared
.init("MY Onesignal APP ID", iOSSettings: settings);
OneSignal.shared
.setInFocusDisplayType(OSNotificationDisplayType.notification);
var status = await OneSignal.shared.getPermissionSubscriptionState();
onesignalUserId = status.subscriptionStatus.userId;
print("player ID: "+ onesignalUserId.toString()); // printing only in re-login
_saveOneSignalId(onesignalUserId,username); // my save funtion into the DB
}
You can call the below function inside your main.dart file when initialising the One Signal to get playerId/userId. The below function is called when user opend the app for first time or when the userId (playerId) changes.
OneSignal.shared.setSubscriptionObserver((OSSubscriptionStateChanges changes) async {
String onesignalUserId = changes.to.userId;
print('Player ID: ' + onesignalUserId);
}
The playerId/userId can be used to send test notification or notifications to particular user.
Please try this.
OneSignal.shared.setSubscriptionObserver((OSSubscriptionStateChanges changes)
async{
var status = await OneSignal.shared.getPermissionSubscriptionState();
if (status.subscriptionStatus.subscribed){
String onesignalUserId = status.subscriptionStatus.userId;
print('Player ID: ' + onesignalUserId);
}

Flutter: Firebase user's name doesn't get updated when user logs in for the first time

I am trying to fetch a user's ID when a person is logged in with their firebase account. When I try to login a new user for the first time, I can fetch their id however the user's details only reflect after the person navigates from one page to another using the navigation bar. Is there a way I can get rid of that? I want the user's display name to reflect as soon as they create a new account.
This is my current code that I call in my initState()
getCurrentUser() async {
try {
final user = await _auth.currentUser();
if (user != null) {
setState(() {
loggedInUser = user;
id = user.uid;
username = user.displayName;
});
}
} catch (e) {
print(e);
}
}