Goutham Kumar
Goutham Kumar

Reputation: 43

Flutter firebase push notification not routing to specific page

I am trying to navigate to a specific page when a notification is clicked. The onResume and onMessage callbacks are invoked when I click on the notification and I can see the message in the log screen. However, when I try to navigate to a specific page, I am not able to do that and there is no error message in the log too. P.S. When I used a Navigator key to access the state of the context(since in initState, the navigator cannot be used) I got an error saying no context to build. What is the mistake ??

I have tried Navigator.push, Calling a method and routing from within that method, used navigator key.

void initState() {
messaging.configure(
  onMessage: (Map<String, dynamic> message) async {
    print('onMessage: $message');
    Navigator.of(context).push(
        MaterialPageRoute<BuildContext>(builder: (_) => PageContent(value:1)));
  },
  onLaunch: (Map<String, dynamic> message) async {
    print('onLaunch: $message');
    Navigator.of(context).push(
        MaterialPageRoute<BuildContext>(builder: (_) => PageContent(value:2)));
  },
  onResume: (Map<String, dynamic> message) async {
    print('onResume:-  This is the message  $message');
    Navigator.of(context).push(
        MaterialPageRoute<BuildContext>(builder: (_) => MoviesList()));
  },
);

I expect the code to be loaded when the notification is tapped and route to a new page( MoviesList or PageContent in my case). But only my home screen is visible.

Upvotes: 4

Views: 2232

Answers (1)

Aravindh Kumar
Aravindh Kumar

Reputation: 1243

Context is not available in init state

I came across this issue and get resolved using redux concepts

add a key in a global state like appNavigator

sample code for global app state (app_state.dart),

import 'package:built_collection/built_collection.dart';
import 'package:built_value/built_value.dart';
import 'package:flutter/material.dart' hide Builder;

part 'app_state.g.dart';

abstract class AppState implements Built<AppState, AppStateBuilder> {
  factory AppState([AppStateBuilder updates(AppStateBuilder builder)]) =
      _$AppState;

  AppState._();

  static AppState initState() {
    return new AppState((AppStateBuilder b) {
      b
        ..appNavigator = new GlobalKey<NavigatorState>(debugLabel: 'debugLabel')
        .. isLoggedIn = false
        ..isLoading = false;

    });
  }

  // Never change this key through out the app lifecycle
  GlobalKey<NavigatorState> get appNavigator;

  // login state ***************************************************************************
  bool get isLoggedIn;

  // indicates loading state ***************************************************************************
  bool get isLoading;

}

dispatch an action onMessage received from the notification like

onMessage: (Map<String, dynamic> message) async {
    print('onMessage: $message');
store.dispatch(new RedirectUserOnNotification());
  },

and in middleware route to a specific page with conditions validation as you needed.

void redirectuser(Store<AppState> store, RedirectUserOnNotification action,
    NextDispatcher next) async {
    store.state.appNavigator.currentState.pushNamed(someRouteName);
  next(action);
}

Note: I have used build_value concepts in a model file

Upvotes: 1

Related Questions