I'm using firebase_messaging When notification comes, I'm displaying alert dialog. Below are my code.
showNotification(BuildContext context) {
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print('on message $message');
_showPushNotificationDialog(message['notification']['title'],
message['notification']['body'], context);
},
onResume: (Map<String, dynamic> message) async {
print('on resume $message');
_showPushNotificationDialog(
message['data']['title'], message['data']['body'], context);
},
onLaunch: (Map<String, dynamic> message) async {
print('on launch $message');
_showPushNotificationDialog(
message['data']['title'], message['data']['body'], context);
},
);
}
Where _showPushNotificationDialog
method will call each times when onMessage, onResume, and onLaunch method call.
Facing issue like when my app is in background or terminate mode and notification will come and will tap on notification tray all works good. But when I go on other page and come back to previous one all time _firebaseMessaging.configure(....
method call and its has data so each time my alert dialog pups up.
So how can I clear notification that was clicked by notification tray?
I was having problems with this too. I have this workaround: What i did was to create a class with an static bool and static method:
class MessagingWidget {
static bool _isConfigured = false;
static void configuringFirebase(User currentUser, BuildContext context){
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
if (!_isConfigured) {
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print("onMessage: $message");
final notification = message['notification'];
},
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");
final notification = message['data'];
if(notification['title']!=null){
if(notification['title']=="Testo"){
goToAppointmentsScreen(currentUser,context);
}
}
},
onResume: (Map<String, dynamic> message) async {
print("onResume: $message");
final notification = message['data'];
if(notification['title']!=null){
if(notification['title']=="Testo"){
goToAppointmentsScreen(currentUser,context);
}
}
},
);
_isConfigured = true;
}
}
}
void goToAppointmentsScreen(User currentUser1, BuildContext context1) async {
final bool backFromAppointmentsScreen=await Navigator.push(
context1,
MaterialPageRoute(builder: (context) => Appointment(
currentUser1),
),
);
}
Then I called this method in my init from the routing widget:
@override
void initState(){
super.initState();
refreshServices();
getDirectionBasedOnLocation();
MessagingWidget.configuringFirebase(currentUser, context);
}
I hope this helps you
What I do to prevent onLaunch and onResume method to run again and again is to check the current notification with the last notification (I use shared_preferences)
Here's the snippet:
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print('on message $message');
onMessageReceived(context, message);
},
onResume: (Map<String, dynamic> message) async {
print('on resume $message');
onLaunch(context, message);
},
onLaunch: (Map<String, dynamic> message) async {
print('on launch $message');
onLaunch(context, message);
},
);
.
.
.
void onLaunch(BuildContext context, Map<String, dynamic> remoteMessage) async {
var pref = SharedPreferences.getInstance();
var data = remoteMessage['data'] ?? remoteMessage;
String lastData = '';
await pref.then((prefs) {
lastData = prefs.get('remote_message');
});
if ((data['type'] != null || data['id'] != null) &&
data.toString() != lastData) {
toDetailPageFromPush(
context,
data['type'],
data['id'],
);
pref.then((prefs) {
prefs.setString('remote_message', data.toString());
});
} else {
print('on launch error $remoteMessage');
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With