Reputation: 6082
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?
Upvotes: 5
Views: 5689
Reputation: 906
Here is another solution, using the message_id which is and unique value from the notification, so we can save the last id of the notification using shared prefrerences and compare with the current notification:
processNotification(message, BuildContext context) async {
try {
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
String lastMessageId = sharedPreferences.getString('last_message_id');
String currentMessageId = message['data']['google.message_id'];
//COMPARE NOTIFICATIONS ID
if(currentMessageId != lastMessageId) {
//SET LAST NOTIFICATION ID
sharedPreferences.setString('last_message_id', currentMessageId);
//SHOW A DIALOG OR NAVIGATE TO SOME VIEW, IN THIS CASE TO A VIEW
String screen = message['data']['screen'];
Navigator.of(context).pushNamed(screen);
}
} catch (e) {
print('ERROR PROCESSING NOTIFICATION');
print(e);
}
}
Now we can call this function in the config:
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print('on message $message');
processNotification(context,message);
},
onResume: (Map<String, dynamic> message) async {
print('on resume $message');
processNotification(context,message);
},
onLaunch: (Map<String, dynamic> message) async {
processNotification(context,message);
},
);
Upvotes: 1
Reputation: 11
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');
}
}
Upvotes: 1
Reputation: 11
I know, it's a bit ugly, but I know only this way:
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
Just create final fln = FlutterLocalNotificationsPlugin();
and use fln.cancelAll()
when you need.
Upvotes: 1
Reputation: 121
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
Upvotes: 2
Reputation: 647
try to configure the firebaseMessaging in initState instead of in a custom method. That should work :)
Upvotes: 0