Home > OS >  In flutter,how to jump to a specified screen(not main screen) after clicking on received background
In flutter,how to jump to a specified screen(not main screen) after clicking on received background

Time:10-27

As title, In flutter,how to redirect to a specified screen(not main scrren) after clicking on received background push notification?

PS: Foreground mode is ok!

Pls see my code, after I listened background message,then...what should I do next?

The following is my main.dart code:

import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import './tabs.dart';
import 'package:firebase_messaging/firebase_messaging.dart';

@pragma('vm:entry-point')
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
  await Firebase.initializeApp();

  print("Handling a background message: ${message.messageId}");
  print("Handling a background message: ${message.data['id']}");
  print("Handling a background message: ${message.data['screen']}");
}

void main(List<String> args) async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);

  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return const MaterialApp(home: Tabs());
  }
}
// return const MaterialApp(home: Tabs());

After clicking on background push notification,jump to a specified screen,not main screen.thanks all.

CodePudding user response:

you can handle the background message after your flutter app has a context.

here the documentation: https://firebase.flutter.dev/docs/messaging/notifications/#handling-interaction

  • handling interaction from background message
  Future<void> setupInteractedMessage() async {
    // Get any messages which caused the application to open from
    // a terminated state.
    RemoteMessage? initialMessage =
        await FirebaseMessaging.instance.getInitialMessage();

    // If the message also contains a data property with a "type" of "chat",
    // navigate to a chat screen
    if (initialMessage != null) {
      _handleMessage(initialMessage);
    }

    // Also handle any interaction when the app is in the background via a
    // Stream listener
    FirebaseMessaging.onMessageOpenedApp.listen(_handleMessage);
  }
  
  void _handleMessage(RemoteMessage message) {
    if (message.data['type'] == 'chat') {
      Navigator.pushNamed(context, '/chat', 
        arguments: ChatArguments(message),
      );
    }
  }

then call the function inside the initState. if your apps has a SplasScreen put the function there. So after the apps initialize, the function will executed before navigate to the mainscreen.

but if you dont have SplashScree, you can put it on top of all function inside the initState on the MainScreen.

  @override
  void initState() {
    super.initState();

    // Run code required to handle interacted messages in an async function
    // as initState() must not be async
    setupInteractedMessage();
  }
  • Related