Flutter firebase push notification not routing to specific page

ぃ、小莉子 提交于 2020-04-16 05:48:13

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/55936801/flutter-firebase-push-notification-not-routing-to-specific-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!