I am using Firebase Cloud Messaging package for flutter.
I want that whenever a user taps on the received notification, the flutter app opens and navigate to a particular screen depending on the data sent by fcm.
But I am not able to achieve desired behaviour when either app is in the background or app is closed since onResume and onLaunch handler are never called. I have tried a number of solution like creating notification channel , using package like
flutter_local_notifications etc. But nothing worked for me.
Here is my FirebaseMessagingService class to handle fcm related tasks
class FirebaseMessagingService {
final FirebaseMessaging _fcm = FirebaseMessaging();
final RouterService _routerService = locator<RouterService>();
final LocalStorageService _localStorage = locator<LocalStorageService>();
Future initialise() async {
if (Platform.isIOS) {
_fcm.requestNotificationPermissions(IosNotificationSettings());
}
_fcm.configure(
onMessage: (Map<String, dynamic> message) async {
Map<String, dynamic> arguments = {};
Map<String, dynamic> data = message['data'];
if (data.containsKey('argument')) {
if (data.containsKey('value')) {
arguments[data['argument']] = data['value'];
}
}
_routerService.navigationKey.currentState
.pushReplacementNamed('${data['route']}', arguments: arguments);
},
onLaunch: (Map<String, dynamic> message) async {
Map<String, dynamic> arguments = {};
Map<String, dynamic> data = message['data'];
if (data.containsKey('argument')) {
if (data.containsKey('value')) {
arguments[data['argument']] = data['value'];
}
}
_routerService.navigationKey.currentState
.pushReplacementNamed('${data['route']}', arguments: arguments);
},
onResume: (Map<String, dynamic> message) async {
Map<String, dynamic> arguments = {};
Map<String, dynamic> data = message['data'];
if (data.containsKey('argument')) {
if (data.containsKey('value')) {
arguments[data['argument']] = data['value'];
}
}
_routerService.navigationKey.currentState
.pushReplacementNamed('${data['route']}', arguments: arguments);
},
onBackgroundMessage: _myBackgroundMessageHandler);
}
static Future<dynamic> _myBackgroundMessageHandler(
Map<String, dynamic> message) async {
final RouterService _routerService = locator<RouterService>();
if (message.containsKey('data')) {
// Handle data message
Map<String, dynamic> arguments = {};
Map<String, dynamic> data = message['data'];
if (data.containsKey('argument')) {
if (data.containsKey('value')) {
arguments[data['argument']] = data['value'];
}
}
_routerService.navigationKey.currentState
.pushReplacementNamed('${data['route']}', arguments: arguments);
}
if (message.containsKey('notification')) {
// Handle notification message
final dynamic notification = message['notification'];
}
}
}
Can anyone help me out with the issue??
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…