如何从推送通知导航到BottomNavigationBar上的特定选项卡?

时间:2019-04-05 00:20:09

标签: flutter

我认为我有一种方法涉及设置进入这样的通知标签:

class NotificationUtil {

static void subscribeToNotifications(BuildContext context, GlobalKey<ScaffoldState> scaffoldKey) async {
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
_firebaseMessaging.configure(
  onMessage: (Map<String, dynamic> message) async {
    print("onMessage: $message");
    _showNotification(scaffoldKey, message);
  },
  onLaunch: (Map<String, dynamic> message) async {
    print("onLaunch: $message");
    await PreferenceUtil.setNotificationKey();
  },
  onResume: (Map<String, dynamic> message) async {
    print("onResume: $message");
    await PreferenceUtil.setNotificationKey();
  },
);

await _firebaseMessaging.getToken();
_firebaseMessaging.requestNotificationPermissions(
    const IosNotificationSettings(sound: true, badge: true, alert: true));
_firebaseMessaging.onIosSettingsRegistered
    .listen((IosNotificationSettings settings) {
  print("Settings registered: $settings");
});

FirebaseUser firebaseUser = await AuthUtil.getFirebaseUser();
_firebaseMessaging
    .subscribeToTopic(firebaseUser.email.replaceAll('@', '%'));
}

 static void _showNotification(GlobalKey<ScaffoldState>  scaffoldKey, Map<String, dynamic> message) {
scaffoldKey.currentState.showSnackBar(new SnackBar(
  content: new Text(message['notification']['body'] as String),
));
 }
 }

但是看起来从后台恢复时未调用initState。

  @override
  void initState() {
super.initState();
PreferenceUtil.showNotifications().then((showNotification) async {
  print("the show notification state is:" +showNotification.toString());
  await PreferenceUtil.clearNotificationKey();
  if (showNotification) {
    setState(() {
      _selectedIndex = 1;
    });
  }
});

}

1 个答案:

答案 0 :(得分:0)

总体方法是将int传递给屏幕,该屏幕标识在导航至带有底部栏的屏幕时要选择的选项卡位置。

class MainPage extends StatefulWidget {
  final int selectedIndex;

  MainPage(this.selectedIndex);

  @override
  _MainPageState createState() => _MainPageState(selectedIndex);
}

我不得不采用一种机制来跟踪当前正在处理的推送通知,因为从推送通知打开应用程序时,处理将陷入无限循环。

这是对我有用的更完整的实现。

通知处理:

class NotificationUtil {
static void subscribeToNotifications(
  BuildContext context, GlobalKey<ScaffoldState> scaffoldKey) async {
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
_firebaseMessaging.configure(
  onMessage: (Map<String, dynamic> message) async {
    print("onMessage: $message");
    _showNotification(scaffoldKey, message);
  },
  onLaunch: (Map<String, dynamic> message) async {
    print("onLaunch: $message");
    processIncomingNotification(context, message);
  },
  onResume: (Map<String, dynamic> message) async {
    print("onResume: $message");
    processIncomingNotification(context, message);
  },
);

await _firebaseMessaging.getToken();
_firebaseMessaging.requestNotificationPermissions(
    const IosNotificationSettings(sound: true, badge: true, alert: true));
_firebaseMessaging.onIosSettingsRegistered
    .listen((IosNotificationSettings settings) {
  print("Settings registered: $settings");
});

FirebaseUser firebaseUser = await AuthUtil.getFirebaseUser();
_firebaseMessaging
    .subscribeToTopic(firebaseUser.email.replaceAll('@', '%'));
}

static void processIncomingNotification(BuildContext context, Map<String, dynamic> message) async {
String incomingMessage = message["data"]["google.message_id"] as String;
String currentMessage = await PreferenceUtil.getCurrentMessage();
if (incomingMessage == currentMessage) {
  return;
}
await PreferenceUtil.setCurrentMessage(incomingMessage);
Navigator.pushReplacementNamed(context, "/notifications");
}

static void _showNotification(
  GlobalKey<ScaffoldState> scaffoldKey, Map<String, dynamic> message) {
scaffoldKey.currentState.showSnackBar(new SnackBar(
  content: new Text(message['notification']['body'] as String),
));
}
}

使用命名路线来选择标签:

   class MyApp extends StatelessWidget {
 @override
Widget build(BuildContext context) {
return MaterialApp(
  debugShowCheckedModeBanner: false,
  title: COMPANY_NAME,
  theme: ThemeData.light().copyWith(
      primaryColor: const Color(0xFF13172a),
      accentColor: const Color(0xFFA8C048)),
  home: HomePage(title: COMPANY_NAME),
  routes: {
    '/notifications': (context) => MainPage(1),
  }
);
}
}

主页(缩写):

class MainPage extends StatefulWidget {
  final int selectedIndex;

  MainPage(this.selectedIndex);

  @override
  _MainPageState createState() => _MainPageState(selectedIndex);
}

class _MainPageState extends State<MainPage> {
  GlobalKey<ScaffoldState> scaffoldKey = new GlobalKey<ScaffoldState>();
  int _selectedIndex = 0;

  final _widgetOptions = [
    PropertyListPage(),
    NotificationListPage(),
    MorePage(),
  ];

  _MainPageState(this._selectedIndex);