Home > Back-end >  How to send a user to a specific page via firebase notification when the app is terminated in flutte
How to send a user to a specific page via firebase notification when the app is terminated in flutte

Time:02-05

Main Screen

    flutterLocalNotificationsPlugin.initialize(initializationSettings,
        onSelectNotification: (String? payload) async {
      try {
        if (payload != null && payload.isNotEmpty) {
          Navigator.push(
              context,
              MaterialPageRoute(
                  builder: (context) => NewScreen(info: payload.toString())));
        } else {}
      } catch (e) {}
      return;
    });


  void sendPushMessage(String token, String body, String title) async {
    try {
      await http.post(
        Uri.parse('https://fcm.googleapis.com/fcm/send'),
        headers: <String, String>{
          'Content-Type': 'application/json',
          'Authorization':
              'key=thekey'
        },
        body: jsonEncode(<String, dynamic>{
          'priority': 'high',
          'data': <String, dynamic>{
            'click_action': 'FLUTTER_NOTIFICATION_CLICK',
            'status': 'done',
            'body': body,
            'title': title
          },
          "notification": <String, dynamic>{
            "title": title,
            "body": body,
            "android_channel_id": "androidchannelid"
          },
          "to": token,
        }),
      );
    } catch (e) {
      if (kDebugMode) {
        print('error push notifications');
      }
    }
  }

When the notification is received it should be send to the new screen and not the main screen.

This code works when the app is in background or foreground but it is not working when the app is terminated.

What should i do?

CodePudding user response:

Use to getInitialMessage to check if there is a message when a application is opened.

RemoteMessage? initialMessage = await FirebaseMessaging.instance.getInitialMessage();
if (initialMessage != null) {
  // TODO navigate to a specific page
}

See Handling Interaction for details.

  • Related