I need to verify by an ID that comes inside the data field that I received from a firebase message. How can I access this field based on the active notifications?
The point is to remove the notification once a page with that ID is opened.
This is what I have to get the notifications
page.dart
final List<ActiveNotification>? activeNotifications =
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()!
.getActiveNotifications();
that gives me the body, channelId, id, title and hascode.
While RemoteMessage message gives me a lot more stuff including a map data.
Is there a way to access this data field through the ActiveNotification?
I'm trying to do the verification with a sample on the body, but it's not a really good pratice giving the circumstances of the project.
What I receive from firebase is sent_at (date), service_id (the id I need to get to), id (other id but not so important), body, and title.
The service_id shouldn't be displayed in the notification tho, otherwise I'd get it through the notification body
Whoever answered and deleted their answer, helped my a lot. So I'm marking this as the solution because it worked. Thank you stranger.
final Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
final Future<SharedPreferences> _savedNotifications =
SharedPreferences.getInstance();
_savedNotifications.then((saveNotifications) {
saveNotifications.setString(
"service_id_${message.messageId}", message.data["service_id"]);
});
#override
void didChangeAppLifecycleState(AppLifecycleState state) {
switch (state) {
case AppLifecycleState.resumed:
onResumed();
break;
case AppLifecycleState.inactive:
onInactive();
break;
case AppLifecycleState.detached:
onDetached();
break;
case AppLifecycleState.paused:
onPaused();
break;
}
}
Future<String?> _getServiceId(title) async {
_savedNotifications.then((saveNotifications) => saveNotifications.reload());
return _savedNotifications.then((saveNotifications) {
_savedNotifications.then(
(value) => value.getKeys().forEach(
(element) async {
if (element.contains('service_id_')) {
String serviceId = value.get(element).toString();
}
},
),
);
});
}
void onResumed() async {
final prefs = await SharedPreferences.getInstance();
prefs.reload();
final List<ActiveNotification>? activeNotifications =
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()!
.getActiveNotifications();
for (ActiveNotification notification in activeNotifications!) {
String? serviceId = await _getServiceId(notification.title);
}
}
Related
How do I update the state with Provider after I've called the API to update it to the BE? I pass the arguments from one screen to another, then after I edit the text, I trigger the API call to the BE and I get the return value of the response and I want to update the state with that response with Provider. How is that possible? Here is the code:
Here I call the API and pass the arguments I've edited in my text fields:
onPressed: () async {
final updatedUser = await await APICall.updateUser(
userID,
updateName,
updateEmail,
);
Provider.of<UserStore>(context, listen: false)
.updateUser(updatedUser);
Navigator.pop(context);
},
Here is the API call where I return the response of the updated User:
Future<User> updateUser(String userID, String name, String email) async {
final response =
await APICalls.apiRequest(Method.PATCH, '/users', this._jsonWebToken,
body: jsonEncode({
"id": userID,
"name": name,
"email": email,
}));
Map<String, dynamic> jsonDecodedResponse = jsonDecode(response.body);
return User(
id: jsonDecodedResponse['data']['id'],
name: jsonDecodedResponse['data']['name'],
email: jsonDecodedResponse['data']['email'],
);
}
Now I wanted to pass that response I've got from the API call to pass it to the providers state:
deleteUser(User list) {
_userList.remove(list);
notifyListeners();
}
addUser(User list) {
_userList.add(list);
notifyListeners();
}
updateUser(User ){//I'm not sure how do define the updateUser method...
notifyListeners();
}
The update works on the BE side, and on the FE only when I refresh the widget, not immediately after the response is returned, which is the way I want it to work.
class UserStore extends ChangeNotifier {
List<User> clientList = [];
void deleteUser(User list) {
clientList.remove(list);
notifyListeners();
}
void addClient(User list) {
clientList.add(list);
notifyListeners();
}
void updateUser(User user){
clientList[clientList.indexWhere((element) => element.id == user.id)] = user;
notifyListeners();
}
}
What you have to do now is to listen to this provider on your widget. When the user will be updated, the changes will be applied to any widget listening to the clientList.
Note I've changed the clientList variable to public, so it can be listened by any widget outside.
I'm trying to use riverpod for login with a laravel backend. Right now I'm just returning true or false from the repository. I've set a form that accepts email and password. The isLoading variable is just to show a circle indicator. I've run the code and it works but not sure if I'm using riverpod correctly. Is there a better way to do it ?
auth_provider.dart
class Auth{
final bool isLogin;
Auth(this.isLogin);
}
class AuthNotifier extends StateNotifier<Auth>{
AuthNotifier() : super(Auth(false));
void isLogin(bool data){
state = new Auth(data);
}
}
final authProvider = StateNotifierProvider((ref) => new AuthNotifier());
auth_repository.dart
class AuthRepository{
static String url = "http://10.0.2.2:8000/api/";
final Dio _dio = Dio();
Future<bool> login(data) async {
try {
Response response = await _dio.post(url+'sanctum/token',data:json.encode(data));
return true;
} catch (error) {
return false;
}
}
}
login_screen.dart
void login() async{
if(formKey.currentState.validate()){
setState((){this.isLoading = true;});
var data = {
'email':this.email,
'password':this.password,
'device_name':'mobile_phone'
};
var result = await AuthRepository().login(data);
if(result){
context.read(authProvider).isLogin(true);
setState((){this.isLoading = false;});
}else
setState((){this.isLoading = false;});
}
}
Since I'm not coming from mobile background and just recently use flutter+riverpod in my recent project, I cannot say this is the best practice. But there are some points I'd like to note:
Use interface such IAuthRepository for repository. Riverpod can act as a dependency injection.
final authRepository = Provider<IAuthRepository>((ref) => AuthRepository());
Build data to send in repository. You should separate presentation, business logic, and explicit implementation for external resource if possible.
Future<bool> login(String email, String password) async {
try {
var data = {
'email': email,
'password': password,
'device_name':'mobile_phone'
};
Response response = await _dio.post(url+'sanctum/token',data:json.encode(data));
return true;
} catch (error) {
return false;
}
}
Do not call repository directly from presentation/screen. You can use the provider for your logic, which call the repository
class AuthNotifier extends StateNotifier<Auth>{
final ProviderReference ref;
IAuthRepository _authRepository;
AuthNotifier(this.ref) : super(Auth(false)) {
_authRepository = ref.watch(authRepository);
}
Future<void> login(String email, String password) async {
final loginResult = await_authRepository.login(email, password);
state = Auth(loginResult);
}
}
final authProvider = StateNotifierProvider((ref) => new AuthNotifier(ref));
On screen, you can call provider's login method
login() {
context.read(authProvider).login(this.email, this.password);
}
Use Consumer or ConsumerWidget to watch the state and decide what to build.
It also helps that instead of Auth with isLogin for the state, you can create some other state. At the very least, I usually create an abstract BaseAuthState, which derives to AuthInitialState, AuthLoadingState, AuthLoginState, AuthErrorState, etc.
class AuthNotifier extends StateNotifier<BaseAuthState>{
...
AuthNotifier(this.ref) : super(AuthInitialState()) { ... }
...
}
Consumer(builder: (context, watch, child) {
final state = watch(authProvider.state);
if (state is AuthLoginState) ...
else if (state is AuthLoadingState) ...
...
})
Instead of using a bool, I like to use enums or class for auth state
enum AuthState { initialize, authenticated, unauthenticated }
and for login state
enum LoginStatus { initialize, loading, success, failed }
How to close a Function without disposing it. I needy this answer because when I log out, I need to close the functions in ChangeNotifier Class.
This Is my ChangeNotifier Class:
class ChatAndRequestProvider extends ChangeNotifier {
bool _areThereNewChatsAndRequests = false;
bool get areThereNewChatsAndRequests => _areThereNewChatsAndRequests;
set areThereNewChatsAndRequests(bool value) {
_areThereNewChatsAndRequests = value;
notifyListeners();
}
List _chatsList = [];
List get chatsList => _chatsList;
set chatsList(List list) {
_chatsList = list;
notifyListeners();
}
getChats() async {
var prefs = await SharedPreferences.getInstance();
print('The getChats Id is ${prefs.getString(kUserId)}');
FirebaseDatabase.instance
.reference()
.child('users')
.child(prefs.getString(kUserId))
.child('friendsArray')
.onValue
.listen((snapshot) {
Map list = snapshot.snapshot.value;
print('map is $list');
var newItems = [];
if (list != null) {
list.forEach((key, value) {
newItems.add(value);
});
chatsList = newItems;
var globalArray = [];
for (var item in newItems) {
if (item[kLastTimestamp] != item[kLastTimestampSeen]) {
areThereNewChatsAndRequests = true;
}
var status;
switch (item['friendsStatus']) {
case 'friends':
status = RequestStatus.alreadyAFriend;
break;
case 'notFriends':
status = RequestStatus.noRequest;
break;
case 'blocked':
status = RequestStatus.userThatBlockedMe;
break;
case 'unblocked':
status = RequestStatus.noRequest;
break;
}
globalArray.add({kUserId: item[kUserId], kTypeOfRequest: status});
}
valuesList = globalArray;
} else {
deleteFromList(null, RequestStatus.alreadyAFriend);
chatsList = [
{kUserId: 'null'}
];
}
});
}
So for example when I log In as user1 and I call this function in the LoadingScreen() I get all of users that are my friends, and I can go to the chats screen List and chat with my friends. Up to this point there is no issue. But when I log out and when I log in with another account lets say user2 and I call this function again, then I get error and two responses because I am calling this function twice. I am not using Auth Packet, I have my own database on MongoDB where I store user Info, but requests and chats are stored on RealTime Database.
So my Question is:
When user1 logs out of my app, I can not call dispose() on provider because if he wants to log in again to another account, he will get an error because Provider was disposed, so how can I stop listening to my database when user logs out and call this function again. Thank You very Much!!
I´m not sure if this works because I don't fully understand the flow of your app but you say that
I can not call dispose() on provider because if he wants to log in
again to another account,
when the users logs out shouldn't the app return to the first screen disposing the provider? (unless you create it in the MaterialApp, I'm not sure about that either). You could save the instance of the Firebase listener and then close it when you log out/ dispose the provider
var _myListener;
getChats() async {
var prefs = await SharedPreferences.getInstance();
print('The getChats Id is ${prefs.getString(kUserId)}');
_myListener = FirebaseDatabase.instance
.reference()
.child('users')
.child(prefs.getString(kUserId))
.child('friendsArray')
.onValue
.listen((snapshot) ...
....
/// The rest of your code
}
void closeListener(){ //call it when the user logs out
_myListener?.close();
}
#override
void dispose(){
closeListener(); // or call it in the dispose if you want
//to dispose and create a new provider when the user logs out/ sign in
super.dispose();
}
I have implemented Firebase Cloud Messaging in my flutter application. Everything works well but I want to store all list of messages locally with shared preferences and retrieve them in another screen. All my logic does not work well as I can't save the messages when the onMessage function is called.
PushNotificationService
class PushNotificationService {
final FirebaseMessaging _fcm = FirebaseMessaging();
List<String> titles;
List<String> msgs;
Future initialise() async {
notiList = List<NotiMessage>();
if (Platform.isIOS) {
// request permissions if we're on android
_fcm.requestNotificationPermissions(IosNotificationSettings());
_fcm.configure();
// For testing purposes print the Firebase Messaging token
String token = await _fcm.getToken();
print("FirebaseMessaging token: $token");
} else{
String token = await _fcm.getToken();
print("FirebaseMessaging token: $token");
}
_fcm.configure(
// Called when the app is in the foreground and we receive a push notification
onMessage: (Map<String, dynamic> message) async {
print('onMessage: $message');
//add list of messages to shared preferences
_setMessage(message);
},
// Called when the app has been closed comlpetely and it's opened
// from the push notification.
onLaunch: (Map<String, dynamic> message) async {
print('onLaunch: $message');
_serialiseAndNavigate(message);
//add list of messages to shared preferences
_setMessage(message);
},
// Called when the app is in the background and it's opened
// from the push notification.
onResume: (Map<String, dynamic> message) async {
print('onResume: $message');
_serialiseAndNavigate(message);
//add list of messages to shared preferences
_setMessage(message);
},
);
}
void _serialiseAndNavigate(Map<String, dynamic> message) {
var notificationData = message['data'];
var view = notificationData['view'];
if (view != null) {
// Navigate to desired page
if (view == 'create_post') {
}
}
}
_setMessage(Map<String, dynamic> message) {
//add list of messages to shared preferences
final notification = message['notification'];
final data = message['data'];
final String title = notification['title'];
final String body = notification['body'];
String mMessage = data['message'];
//add to list
titles.add(title);
msgs.add(mMessage);
//save to shared preferences (does not work)
storeTitles(titles);
storeMsgs(msgs);
print("Title: $title, body: $body, message: $mMessage");
}
void storeTitles(List<String> list) async{
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setStringList("notiTitles", list);
//list returns null
}
void storeMsgs(List<String> list) async{
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setStringList("notiMsgs", list);
}
Future<List<String>> getTitles(List<String> list) async{
SharedPreferences prefs = await SharedPreferences.getInstance();
list = prefs.getStringList("notiTitles");
return prefs.getStringList("notiTitles");
}
Future<List<String>> getMsgs(List<String> list) async{
SharedPreferences prefs = await SharedPreferences.getInstance();
list = prefs.getStringList("notiMsgs");
return prefs.getStringList("notiMsgs");
}
}
Whats the best way to achieve this. I want to save the messages persistently and call them in another screen. Please help me.
The code on saving the List on shared_preferences seems to be ok. The issue might be on how the data is fetched. If you're storing critical data, I suggest to better use something like provider instead. The shared_preferences plugin is unable to guarantee that writes will be persisted to disk after returning as mentioned in its docs.
I have order which have 4 status : preparing , pending , delivering and delivered. and when the order status changed from one to another I wanted to show a notification to the user of the change occurs.
*I have used local notifications plugin. In order page widget shown here it is triggered by a stream above that get the order from fire-base.
*That's why I supposed that each time the status will change the orderPage will be rebuild again and initstate will be recalled and send the new notification msg with new status, but that didn't happen.
Another solution was to use didchangedependency but I got no different result.
This was a missed work I know, but this is what came to my mind.
*What I exactly want is something that make me listen on the status and when changes occur a function " singleNotification" will be called to show the notification.
any Help will be appreciated.
class OrderPage extends StatefulWidget {
const OrderPage({
Key key,
#required this.order,
}) : super(key: key);
final Order order;
#override
OrderPageState createState() {
return new OrderPageState();
}
}
class OrderPageState extends State<OrderPage> {
final DateTime now = DateTime.now().toUtc().add(
Duration(seconds: 3),
);
String title = "notification";
String msg = "";
FlutterLocalNotificationsPlugin localNotificationsPlugin =
FlutterLocalNotificationsPlugin();
initializeNotifications() async {
var initializeAndroid =
AndroidInitializationSettings('#mipmap/ic_launcher');
var initializeIOS = IOSInitializationSettings();
var initSettings = InitializationSettings(initializeAndroid, initializeIOS);
await localNotificationsPlugin.initialize(initSettings);
}
Future singleNotification(
DateTime datetime, String message, String subtext, int hashcode,
{String sound}) async {
var androidChannel = AndroidNotificationDetails(
'channel-id',
'channel-name',
'channel-description',
importance: Importance.Max,
priority: Priority.Max,
);
var iosChannel = IOSNotificationDetails();
var platformChannel = NotificationDetails(androidChannel, iosChannel);
localNotificationsPlugin.schedule(
hashcode, message, subtext, datetime, platformChannel,
payload: hashcode.toString());
}
#override
void initState() {
super.initState();
getMsgState(widget.order.status);
initializeNotifications();
singleNotification(
now,
title,
msg,
98123871,
);
}
#override
void didChangeDependencies() {
super.didChangeDependencies();
getMsgState(widget.order.status);
initializeNotifications();
singleNotification(
now,
title,
msg,
98123871,
);
}
Widget build(BuildContext context) {
return Container();
}
String getMsgState(String orderStatus) {
switch (orderStatus) {
case 'pending':
return msg = "Your order is pending";
break;
case 'preparing':
return msg = "your order is currently preparing";
break;
case 'delivering':
return msg = "your order is currently delivering";
break;
case 'delivered':
return msg = "Your order is delivered";
default:
return msg = "CustomStepState.Complete";
break;
}
}
If I understand correctly, Firebase knows when the order status is changed. And it sends a notification. And you would like to show it to the user.
You can use FCM and Firebase in-app notification. One of my project had a similar requirement whether server does the processing and the Flutter mobile app shows the status. I did the following :
Wrote a small code on the server side which calls Firebase cloud message API with user display message and data payload.
Wrote a code on Flutter mobile app side to display the notification (in the in-app style) if the app is already in the foreground.
Sample code snippet :
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
Helper.write('on message $message');
//Map data = json.decode(message);
this.scaffoldKey.currentState.showSnackBar(SnackBar(content: Text(message['aps']['alert']['body']),));
},
onResume: (Map<String, dynamic> message) async {
Helper.write('on resume $message');
},
onLaunch: (Map<String, dynamic> message) async {
Helper.write('on launch $message');
},
);
analytics.logEvent(name:'firebaseCloudMessaging_Listeners_Done',parameters:null);
}