I use Firebase Cloud Messaging to send notifications to my Flutter app. But it doesn't open a specific route, it only opens the main page.
In my AndroidManifest I added :
<action android:name="FLUTTER_NOTIFICATION_CLICK" />
<category android:name="android.intent.category.DEFAULT" />
And in the firebase console I add this: FirebaseConsoleSnippet
this is my code to receive notification and handle the click:
class PushNotificationService {
final FirebaseMessaging _fcm;
GlobalKey<NavigatorState> _navigationKey = new GlobalKey<NavigatorState>();
GlobalKey<NavigatorState> get navigationKey => _navigationKey;
PushNotificationService(this._fcm);
Future initialise() async {
if (Platform.isIOS) {
_fcm.requestNotificationPermissions(IosNotificationSettings());
}
String token = await _fcm.getToken();
debugPrint("FirebaseMessaging token: $token");
_fcm.configure(
onMessage: (Map<String, dynamic> message) async {
debugPrint("onMessage: $message");
},
onLaunch: (Map<String, dynamic> message) async {
debugPrint("onLaunch: $message");
_serialiseAndNavigate(message);
},
onResume: (Map<String, dynamic> message) async {
debugPrint("onResume: $message");
_serialiseAndNavigate(message);
},
);
}
void _serialiseAndNavigate(Map<String, dynamic> message) {
var notificationData = message['data'];
var view = notificationData['view'];
debugPrint('view: ${view.toString()}');
if (view != null) {
if (view == "events") {
navigationKey.currentState.pushNamed('/events');
}
}
}
}
And this is my console output: ConsoleOutput
In my main file I declared the route like this:
routes: {
'/events': (BuildContext context) => EventsScreen(),
}
Can you help me find the problem? If you need more info please feel free to ask!