Reputation: 21
I have a problem with Notification on Flutter app. I would like to redirect the user from a notification to a specific page. I have right now something like this:
void initState() {
super.initState();
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print("onMessage: $message");
_goToMessage(message);
},
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");
_goToMessage(message);
},
onResume: (Map<String, dynamic> message) async {
print("onResume: $message");
_goToMessage(message);
},
);
_firebaseMessaging.requestNotificationPermissions(
const IosNotificationSettings(
sound: true, badge: true, alert: true, provisional: true));
_firebaseMessaging.onIosSettingsRegistered
.listen((IosNotificationSettings settings) {
print("Settings registered: $settings");
});
_firebaseMessaging.getToken().then((String token) {
print("Push Messaging token: $token");
});
}
void _goToMessage(message) {
Navigator.pushNamed(context, DidacticList.routeName);
}
This code gives me an error:
Navigator operation requested with a context that does not include a Navigator
.
After receiving a push notification code from onLaunch()
or onResume()
executing.
Upvotes: 0
Views: 552
Reputation: 3469
You can pass context from main.dart like following, while initializing Push Notification.
put this in your build method of the MaterialApp Widget.
// init Firebase Push Notification Service...
PushNotificationsManager(context).init();
After BuildContext initialized you can use it as per your choice.
Here's the PushNotificationsManager Class for your reference.
class PushNotificationsManager {
bool _initialized = false;
static BuildContext _context;
Stream<RemoteMessage> _receivedMessages;
PushNotificationsManager._(_context);
factory PushNotificationsManager(BuildContext context) {
_context = context;
return _instance;
}
static final PushNotificationsManager _instance =
PushNotificationsManager._(_context);
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging.instance;
Future<void> init() async {
if (!_initialized) {
await _firebaseMessaging.requestPermission();
_receivedMessages = FirebaseMessaging.onMessageOpenedApp;
// * Implement on Background Message like follwing...
// FirebaseMessaging.onBackgroundMessage((message) => );
// TODO : Implement on message received...
_receivedMessages.listen((message) {});
_initialized = true;
}
}
}
You can use listen or onBackgroundMessage method to navigate to any page on navigation click.
Upvotes: 1