The code for the app in which initializing firebase:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
// TODO: implement initState
super.initState();
FirebaseMessaging.instance.getInitialMessage();
///foreground notification
FirebaseMessaging.onMessage.listen((message) {
if (message.notification != null) {
print(message.notification!.title);
print(message.notification!.body);
}
});
///background & opened notification
///tapped on notification
FirebaseMessaging.onMessageOpenedApp.listen((message) {
final routeFromMessage = message.data['route'];
Navigator.of(context).pushNamed(routeFromMessage);
});
}
Now if the code worked correctly, it should have navigated me to the respective routed page and printed the notification title and body in the console.
But instead I am getting errors like this:
D/FLTFireMsgReceiver( 9928): broadcast received for message
W/FLTFireMsgService( 9928): A background message could not be handled in Dart as no onBackgroundMessage handler has been registered.
W/FirebaseMessaging( 9928): Missing Default Notification Channel metadata in AndroidManifest. Default value will be used.
E/flutter ( 9928): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Navigator operation requested with a context that does not include a Navigator.
E/flutter ( 9928): The context used to push or pop routes from the Navigator must be that of a widget that is a descendant of a Navigator widget.
E/flutter ( 9928): #0 Navigator.of.<anonymous closure>
package:flutter/…/widgets/navigator.dart:2553
E/flutter ( 9928): #1 Navigator.of
package:flutter/…/widgets/navigator.dart:2560
E/flutter ( 9928): #2 _MyAppState.initState.<anonymous closure>
package:note_reminder/main.dart:42
E/flutter ( 9928): #3 _rootRunUnary (dart:async/zone.dart:1434:47)
E/flutter ( 9928): #4 _CustomZone.runUnary (dart:async/zone.dart:1335:19)
E/flutter ( 9928): #5 _CustomZone.runUnaryGuarded (dart:async/zone.dart:1244:7)
E/flutter ( 9928): #6 _BufferingStreamSubscription._sendData (dart:async/stream_impl.dart:341:11)
E/flutter ( 9928): #7 _DelayedData.perform (dart:async/stream_impl.dart:591:14)
E/flutter ( 9928): #8 _StreamImplEvents.handleNext (dart:async/stream_impl.dart:706:11)
E/flutter ( 9928): #9 _PendingEvents.schedule.<anonymous closure> (dart:async/stream_impl.dart:663:7)
E/flutter ( 9928): #10 _rootRun (dart:async/zone.dart:1418:47)
E/flutter ( 9928): #11 _CustomZone.run (dart:async/zone.dart:1328:19)
E/flutter ( 9928): #12 _CustomZone.runGuarded (dart:async/zone.dart:1236:7)
E/flutter ( 9928): #13 _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1276:23)
E/flutter ( 9928): #14 _rootRun (dart:async/zone.dart:1426:13)
E/flutter ( 9928): #15 _CustomZone.run (dart:async/zone.dart:1328:19)
E/flutter ( 9928): #16 _CustomZone.runGuarded (dart:async/zone.dart:1236:7)
E/flutter ( 9928): #17 _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1276:23)
E/flutter ( 9928): #18 _microtaskLoop (dart:async/schedule_microtask.dart:40:21)
E/flutter ( 9928): #19 _startMicrotaskLoop (dart:async/schedule_microtask.dart:49:5)
E/flutter ( 9928):
I/OpenGLRenderer( 9928): Davey! duration=567777ms; Flags=1, FrameTimelineVsyncId=76328, IntendedVsync=19427849009190, Vsync=19427849009190, InputEventId=0, HandleInputStart=19427850193500, AnimationStart=19427850214100, PerformTraversalsStart=19427851010200, DrawStart=19427851815400, FrameDeadline=19427899009188, FrameInterval=19427850132400, FrameStartTime=16666666, SyncQueued=19427852843800, SyncStart=19427853556300, IssueDrawCommandsStart=19427855478900, SwapBuffers=19427864095600, FrameCompleted=19995626844200, DequeueBufferDuration=7109800, QueueBufferDuration=670800, GpuCompleted=19995626844200, SwapBuffersCompleted=19427873795500, DisplayPresentTime=25614609433559128,
Also note that I am trying to route to a screen that I am using with a custom navbar.
Code of routes:
home: OnBoardingPage(),
routes: {
'done': (context) => DonePage(),
'create': (context) => CreatePage(),
'history': (context) => HistoryPage(),
Code of my Home Page:
class _HomePageState extends State<HomePage> {
int _currentIndex = 0;
final screens = [DonePage(), CreatePage(), HistoryPage()];
@override
Widget build(BuildContext context) => SafeArea(
child: Scaffold(
body: screens[_currentIndex],
)