firebase flutter push notificaiton - firebase

I am trying to send push notification from my php application to my flutter apps via Firebase messaging service. while I start the emulator it receives a token from firebase, then i copy that token and can send message. But I want to store that token to my mysql database once i receive the token. I have 2 scenario. For the first time if user install the apps, i'll insert the token to my database so that i can send message using that token. Now I am confused how to update that token every time the user login(not inserting new token again and again to database).
void setupNotification() async{
_firebaseMessaging.
_firebaseMessaging.getToken().then((token){
//insert the token to user database
saveToken(token);
});
Stream<String> fcmStream = _firebaseMessaging.onTokenRefresh;
fcmStream.listen((token) {
//always update the user database with new token
saveToken(token);
});
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async{
print("message while app is open: $message");
},
onResume: (Map<String, dynamic> message) async{
print("message: $message");
},
onLaunch: (Map<String, dynamic> message) async{
print("message: $message");
}
);
}
I mean how to identify that the token is new or old(updated) ?

You need to create a Locally stored json file in your app using path_provider plugin.This file will get initialized everytime user opens the app and will contain a user Model containing token of user.On the very first page of the app in initState check if the user is not null.
IF NULL:
Get fcm token and store it in database.After storing it in database write the token in your Locally stored file.
ELSE:
Do nothing
Next time when same user opens the app again in initstate it will check if user is not null.This time it will not add a new token in database as our code to insert token into database won't execute.
Example:
We will store user as follows.
class User {
String token;
String platform;
User({
this.token = '',
this.platform = '',
});
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['token'] = this.token;
data['platform'] = this.platform;
return data;
}
User.fromJson(Map<String, dynamic> json): this(
platform: json["platform"],
token: json["token"],
);
}
Local Data file:
class LocalData {
static User _user;
static User get user => _user;
/// Flag for Authentication.
static bool get isTokenAdded=> _user != null;
static loadData() async {
final file = File((await getApplicationDocumentsDirectory()).path + '/data.json');
try {
final data = jsonDecode(await file.readAsString());
_user = User.fromJson(data['user']);
} catch (e) {
print(e);
}
}
static writeData() async {
final file = File((await getApplicationDocumentsDirectory()).path + '/data.json');
await file.writeAsString(jsonEncode({
'user': _user?.toJson(),
}));
}
static void addToken(User user) {
LocalData._user = user;
writeData();
}
static void removeToken() {
LocalData._user = null;
writeData();
}
}
App's main function:
void main() async {
/// Load Local Data.
WidgetsFlutterBinding.ensureInitialized();
await LocalData.loadData();
runApp(YourApp());
}
Now in initState of your apps first page you can check if token is already added in database by
if(LocalData.isTokenAdded()){
//do nothing
}else{
//get token and insert it into database then write it in file:
LocalData.user.token = token;
LocalData.writeData();
}

Related

Flutter new phone authentication now do "not a robot" check in seperate browser which make entire procedure slow

I solved this issue (App crashes upon phone authentication after changing package name - Flutter) of app crash by adding implementation "androidx.browser:browser:1.2.0" into app/build.gradle dependencies.
But NOW whole phone authentication procedure got changed. Now app open a browser to do Not a robot test. But I don't want app to open a browser just to verify it's not a robot it make entire process slow and ugly. Below is the video example. How to get rid of this issue? It shows app firebase address in the browser link too.
Video example of issue is below
https://drive.google.com/file/d/1G7noQWyyAHvyTo_Te0v6d2O3IvaiClAw/view?usp=sharing
Below is the code snippet of verifyPhone function.
Future<dynamic> verifyPhone(phoneNo, BuildContext context) async {
var completer = Completer<dynamic>();
dynamic newUserResult;
Future<String> getOTPresult() async {
print("Dialog shown");
await showModalBottomSheet(
context: context,
backgroundColor: Colors.transparent,
builder: (context) => Container(
height: 270,
child: OTPBottomSheet(controller: _otpController),
),
);
return _otpController.text;
}
// >>>>>>>>>>>>> On Complete
final PhoneVerificationCompleted verificationComplete =
(AuthCredential authCred) async {
print(" I N S I D E C O M P L E T E ");
newUserResult = await signInWithPhoneNumber(authCred);
completer.complete(newUserResult);
};
// >>>>>>>>>>>>> On Timeout
final PhoneCodeAutoRetrievalTimeout autoRetrieve = (String verID) {
print("\n2. Auto retrieval time out");
completer.complete(newUserResult);
};
// >>>>>>>>>>>>> On manual code verification
final PhoneCodeSent smsCodeSent =
(String verID, [int forceCodeResend]) async {
print(" I N S I D E C O D E S E N T");
var OTPDialogResult = await getOTPresult();
if (OTPDialogResult != null) {
AuthCredential authCred = PhoneAuthProvider.credential(
verificationId: verID, smsCode: OTPDialogResult);
newUserResult = AuthService().signInWithPhoneNumber(authCred);
if (!completer.isCompleted) {
completer.complete(newUserResult);
}
}
};
// >>>>>>>>>>>>> On Ver failed
final PhoneVerificationFailed verificationFailed =
(Exception authException) {
completer.complete(newUserResult);
};
await FirebaseAuth.instance
.verifyPhoneNumber(
phoneNumber: phoneNo,
timeout: Duration(seconds: 50),
verificationCompleted: verificationComplete,
verificationFailed: verificationFailed,
codeSent: smsCodeSent,
codeAutoRetrievalTimeout: autoRetrieve,
).catchError((error) {
print(error.toString());
});
print("New user result at the end before await: " + newUserResult.toString());
newUserResult = await completer.future;
print("New user result at the end after await: " + newUserResult.toString());
return newUserResult;
}
signInWithPhoneNumber function
Future signInWithPhoneNumber(AuthCredential authCreds) async {
try {
UserCredential result = await FirebaseAuth.instance.signInWithCredential(authCreds);
User customUser = result.user;
return _userFormFirebaseUser(customUser).getuid;
}
CustData _userFormFirebaseUser(User user) {
print("----> Inside _userFormFirebaseUser and user ID: " + user.uid);
return user != null
? CustData(
custId: user.uid,
)
: null;
}
// --- CustData model class
class CustData {
String custId;
String custName;
String custPhNo;
String custContactNO;
DateTime custDateOfBirth;
Map<String, dynamic> address;
String cartID;
CustData({
this.custId,
this.custName,
this.custPhNo,
this.custDateOfBirth,
this.address,
this.cartID,
this.custContactNO,
});
CustData.initial() : custId = '';
String get getuid => this.custId;
}
Already making the whole procedure slow...
Try this Auth Version and it will work well for you :)
implementation com.google.firebase:firebase-auth:19.3.1
The reason behind this is explained in the official documentation https://firebase.google.com/docs/auth/android/phone-auth#enable-app-verification
reCAPTCHA verification: In the event that SafetyNet cannot be used, such as when the user does not have Google Play Services support, or when testing your app on an emulator, Firebase Authentication uses a reCAPTCHA verification to complete the phone sign-in flow. The reCAPTCHA challenge can often be completed without the user having to solve anything. Please note that this flow requires that a SHA-1 is associated with your application.
To solve it you have to :
In the Google Cloud Console, enable the Android DeviceCheck API for your project. The default Firebase API Key will be used, and needs to be allowed to access the DeviceCheck API.
If you haven't yet specified your app's SHA-256 fingerprint, do so from the Settings Page of the Firebase console. Refer to Authenticating Your Client for details on how to get your app's SHA-256 fingerprint.

Converting Firebase Token to String using Flutter

I have an application that is supposed to send the Firebase Token from my Flutter app to an ASP.Net App Server. The endpoint on the app server works - the request from the Flutter app to the App Server is not working.
The reason it is not working is because when I try to send the token, the token doesn't appear to have arrived yet - it's of type Future. How do I turn that token into a string when it finally arrives?
I've tried turning the token directly into a string in the fcmStream.Listen function, I've also tried turning it into a string using _firebaseMessaging.getToken. Neither of them work
FirebaseMessaging _firebaseMessaging = new FirebaseMessaging();
#override
void initState() {
// TODO: implement initState
super.initState();
location.onLocationChanged().listen((value) {
if (this.mounted) {
setState(() {
currentLocation = value;
});
}
});
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) {
print('on message $message');
},
onResume: (Map<String, dynamic> message) {
print('on resume $message');
},
onLaunch: (Map<String, dynamic> message) {
print('on launch $message');
},
);
_firebaseMessaging.requestNotificationPermissions(
const IosNotificationSettings(sound: true, badge: true, alert: true));
String clientToken = _firebaseMessaging.getToken().then((token) {
print("Token Init: " + token.toString());
}
).toString();
BackendService.postToken(clientToken.toString(), "email#gmail.com");
#override
Stream<String> fcmStream = _firebaseMessaging.onTokenRefresh;
fcmStream.listen((token) {
/*print("Token On Refresh: " + token);
BackendService.postToken(token.toString(), "email#gmail.com");*/
}
);
fcmStream.toString();
class BackendService {
static Future<String> postToken(String token, String hostEmail) async {
final responseBody = (await http.get(
'realurlomitted/.../Meets/RegisterDevice?token=$token&hostEmail=$hostEmail')).body;
print(" Response: " + responseBody.toString());
return responseBody.toString();
}
}
Whenever the token.toString prints, it prints the token just fine - I can see that. It just seems like whenever it tries to make the post using http, the token hasn't arrived from whatever getToken is.
If I can turn that Futrure into a string by awaiting it or something, it would solve my problem so that the $token parameter is the token as a string.
More specifically, my request URL should look like:
https://-----/Meets/RegisterDevice?token=c6V49umapn0:Jdsf90832094890s324&hostEmail=email#gmail.com
But it looks like:
https://-----/Meets/RegisterDevice?token=instance of Future<Dynamic>&hostEmail=email#gmail.com
In the Flutter debugger
As you said, awaiting the future will solve your problem. You can write an async function and put the code in your initState inside it and use await, or you can do this:
_firebaseMessaging.getToken().then((token) {
final tokenStr = token.toString();
// do whatever you want with the token here
}
);
This style is now available
FirebaseAuth.instance.currentUser().then((user) {
if (user != null) {
user.getIdToken().then((token) {
Map<dynamic,dynamic> tokenMap = token.claims;
print(tokenMap['sub']);
});
}
});
so this complete code
#override
void initState() {
FirebaseAuth.instance.currentUser().then((user) {
if (user != null) {
user.getIdToken().then((token) {
Map<dynamic,dynamic> tokenMap = token.claims;
print(tokenMap['sub']);
});
}
});
super.initState();
}

How to use timer in flutter app for changing data?

I am using shared preferences to store the token, email, username and other user details when a user logs in using firebase authentication. The firebase token expires in every one hour so I need to refresh the token on the basis of when the user has returned to the app which I am doing in getCurrentUser() function below. I want to know that if a user has logged in my app, used it for 5 minutes or so and then close the application, will that timer function would still be listening and call the function after the timeout or not?
If it doesn't do so then How can I achieve checking this?
void checkTokenValidity(int time) {
Timer(Duration(seconds: time), () async {
print('token timed out');
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString('token', 'expired');
prefs.remove("currentUser");
});
}
Future<String> getCurrentUser() async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
final String currentToken = prefs.getString('token');
final String cuser = prefs.getString('currentUser');
print("current: $cuser");
if (cuser != null && currentToken != 'expired') {
print('signed in and $currentToken');
SharedPreferences prefs = await SharedPreferences.getInstance();
String token = prefs.getString('token');
String uid = prefs.getString('userId');
String email = prefs.getString('userEmail');
String photo = prefs.getString('photo');
_authenticatedUser =
User(email: email, id: uid, token: token, photo: photo);
return 'success';
} else if (currentToken == 'expired') {
print('token is expired');
final FirebaseUser user = await FirebaseAuth.instance.signInAnonymously();
var token = await user.getIdToken();
prefs.setString('token', token);
String uid = prefs.getString('userId');
String email = prefs.getString('userEmail');
String photo = prefs.getString('photo');
_authenticatedUser =
User(id: uid, email: email, token: token, photo: photo);
checkTokenValidity(3600);
return 'token';
} else {
print('user is null');
return null;
}
}
In my authentication function which is not here, I have called checkTokenValidity(3600) just after the user successfully logs in.
I have also tried using FirebaseUser user = await FirebaseAuth.instance.currentUser(); but that also didn't solve the problem.
You went the wrong way. The right way is to add error handler on 401 (Unauthorized) error and handle it by refreshing token and retrying the same query.

flutter Firebase Messaging and sqflite

I try to send a message from my website to users with Firebase and store the message in the user device with sqflite plugin.
When the application in the foreground, everything works good and message insert in the database. But when the application in the background or completely close it just displays notification and data not inserted in the database.
edit:
After setting click_action for notification, Clicking on the notification data will be saved but if the user dismisses notification data lost.
code
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
#override
void initState() {
prepareFirebaseCloudMessaging();
super.initState();
}
void prepareFirebaseCloudMessaging() {
if (Platform.isIOS) IOSPermission();
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
notificationDataHandler(message);
},
onResume: (Map<String, dynamic> message) async {
notificationDataHandler(message);
},
onLaunch: (Map<String, dynamic> message) async {
notificationDataHandler(message);
},
);
}
void notificationDataHandler(Map<String, dynamic> message) async {
try {
var databasesPath = await getDatabasesPath();
String path = join(databasesPath, 'MY_DATABASE_FILE.db');
Database database = await openDatabase(path, version: 1,
onCreate: (Database db, int version) async {
await db.execute(
'CREATE TABLE Messages (id INTEGER PRIMARY KEY, message TEXT, date TEXT, seen INTEGER)');
});
Message messageStore = Message(
null, message["data"]["message"], message["data"]["date"], false);
await database.insert("Messages", messageStore.toMap());
database.close();
}catch(e){
debugPrint(e.toString());
}
}

How to refresh firebase token on Flutter?

I have a Flutter app that uses Firebase messaging to delivery notifications.
This is the base code, it does nothing special, besides saving the token on my DB.
FirebaseMessaging _firebaseMessaging = new FirebaseMessaging();
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) {
},
onResume: (Map<String, dynamic> message) {
},
onLaunch: (Map<String, dynamic> message) {
},
);
_firebaseMessaging.getToken().then((token) {
saveToken(token);
});
Do I have to implement some kind of background service to keep saving the new token on my DB everytime it gets refreshed? I remember using onTokenRefresh() on Android(JAVA) to do this, but I found nothing about it in Flutter (DART).
I read somewhere that the token gets refreshed every 3600 seconds. I wonder if this is true.
No, FCM token doesn't refresh every 3600 seconds. It only refreshes when :
When user Uninstall/Reinstall the app or Clears App Data
You manually delete FCM Instance using FirebaseMessaging().deleteInstanceID()
You can listen to token refresh stream using:
FirebaseMessaging().onTokenRefresh.listen((newToken) {
// Save newToken
});
Hope it helps
You can use firebaseMessaging.onTokenRefresh to get a stream which receives an event each time a new token is received.
Here is an example of subscribing to the firebaseMessaging.onTokenRefresh stream and updating the token if the token has changed:
FirebaseMessaging().onTokenRefresh.listen((token) async {
final prefs = await SharedPreferences.getInstance();
final String firebaseTokenPrefKey = 'firebaseToken';
final String currentToken = prefs.getString(firebaseTokenPrefKey);
if (currentToken != token) {
print('token refresh: ' + token);
// add code here to do something with the updated token
await prefs.setString(firebaseTokenPrefKey, token);
}
});
You can try with this.. as per new updation
FirebaseMessaging.instance.onTokenRefresh.listen((newToken) {
// Save newToken
});
After the user logs in my app logs her in again automatically every 3500 seconds.
I used a Timer like this:
void _timerPressed() {
const timeout = const Duration(seconds: 3500);
new Timer.periodic(timeout, (Timer t) => _handleSignIn());
}
I set the timer in the 'login' button press method after the login has occurred:
void _loginPressed() {
print('The user wants to login with $_email and $_password');
_handleSignIn()
.then((FirebaseUser user) => print(user))
.catchError((e) => print(e));
_timerPressed();
}
(Don't be fooled by the name of the method, '_timerPressed'. I used a button press for testing the technique and haven't gotten around to renaming the method after I tied it in to the login button.)

Resources