Click here to Skip to main content
15,885,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
//root dart file

  FirebaseMessaging.onBackgroundMessage(_backgroundMessageHandler); 
  await NotificationService.instance.initializeNotifications();

  Future<void> _backgroundMessageHandler(RemoteMessage message) async {
  await Firebase.initializeApp();
 }

//In, NotificationService file, I have initialized 
 AwesomeNotification(awesome_notification package),and have 

  //This handles notification actions

  AwesomeNotifications().actionStream.listen((notification) async {
  print(notification.payload);
  if (notification.payload != null) {
    final payload = notification.payload!;
    if (payload['type'] == 'vaccine-records') {
      _appRouter.root.innerRouterOf(DashboardRouter.name)
        ?..innerRouterOf<TabsRouter>(DashboardRoute.name)?.setActiveIndex(2)
        ..navigate(
          VaccineRecordsRouter(
            petId: int.parse(payload['id']!),
            petType: int.parse(payload['pet_type']!),
            petName: notification.title,
          ),
        );
    }
  }
});

 //this listens to new notification from Firebase cloud messaging

FirebaseMessaging.onMessage.listen((message) async {
  print(message.data);
  if (message.data.isNotEmpty) {
    await AwesomeNotifications().createNotificationFromJsonData(message.data);
  } else {
    print('here');
    await AwesomeNotifications().createNotification(
      content: NotificationContent(
        id: 0,
        channelKey: 'basic_channel',
        title: message.notification?.title,
        body: message.notification?.body,
        showWhen: true,
        displayOnForeground: true,
        displayOnBackground: true,
      ),
    );
  }
});
}


What I have tried:

When I tap on the notification, it takes me to homepage of my app. I want it to navigate me to some other screen.When the app is in the foreground and I receive the notification, it takes me to the desired page. But when the app is in the background and the notification is received, it takes me to the homepage.How is this happening since the both time i get AwesomeNotifications().actionStream.listen((notification) async {} to execute?
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900