应用程序被杀死时,无法通过推送通知点击导航到特定屏幕(Flutter Android)

3
我可以帮您翻译成中文。以下是需要翻译的内容:

我在我的Flutter应用程序中使用Firebase Cloud Messaging。当我点击推送通知时,我无法导航到特定屏幕。当应用程序在前台和后台运行时,它可以正常工作,但是当应用程序被关闭时无法正常工作。

代码:

class FcmHelper {
  final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
  FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
  new FlutterLocalNotificationsPlugin();
  void initializeFcm() {
    var initializationSettingsAndroid =
    new AndroidInitializationSettings('@mipmap/ic_launcher');


    flutterLocalNotificationsPlugin.initialize(initializationSettings,
        onSelectNotification: onSelectNotification);

    _firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) {      
        displayNotification(message);
        return;
      },
      onResume: (Map<String, dynamic> message) {
        displayNotification(message);
        return;
      },
      onLaunch: (Map<String, dynamic> message) {
        displayNotification(message);
        return;
      },

    );
 
  }


  Future displayNotification(Map<String, dynamic> message) async {
    // display notification
  }

  Future onSelectNotification(String payload) async {
    try {
      await flutterLocalNotificationsPlugin.cancelAll();
      var tripDetails = HotelBookingDetails(
          );
      Navigator.pushAndRemoveUntil(
          GlobalVariable.navState.currentContext,
          MaterialPageRoute(builder: (context) => tripDetails),
          ModalRoute.withName('/'));
    } catch (e, s) {
      debugPrint(e.toString());
    }
  }

}

请将文本内容粘贴在此处。 - John Joe
4个回答

1
final noti = FirebaseMessaging.instance.getInitialMessage();

你可以在启动画面中实现这个功能,然后跳转到你想要的界面。


0

onLaunch回调函数会在FirebaseMessaging初始化后触发, 你可以使用navigatorKey进行导航,就像这样

      GlobalVariable.navState.currentState.push(
                        MaterialPageRoute(
                          builder: (_) => YourPage(),
                        ),
                      );

如果这不起作用,尝试在有上下文的时候初始化FirebaseMessaging,也许可以在MateriApp构建器方法中实现。

MaterialApp(
  builder: (context, widget) {
     FcmHelper().initializeFcm();
   }
);

0
FirebaseMessaging.instance.getInitialMessage().then((RemoteMessage message) {
    // add your condition if any
    Navigator.of(context).pushNamedAndRemoveUntil("/notifications", (route) => route.settings.name == "/notifications" ? false : true);
});

0

在我的情况下,我需要在通知的服务器端主体中添加以下给定行:"click_action": "FLUTTER_NOTIFICATION_CLICK",并且Postman JSON如下:{ "to": "foP947GMRmuin1N********************", "priority": "high", "notification": { "title": "Sakib sent a offer", "body": "An electric bike used 2 years", "android_channel_id": "Messages", "count": 10, "notification_count": 12, "badge": 12, "color": "#eeeeee" }, "data": { "click_action": "FLUTTER_NOTIFICATION_CLICK", "type": "msj", "id": "1" } }

然后在您的清单中添加以下代码: <intent-filter> <action android:name="FLUTTER_NOTIFICATION_CLICK" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> 然后应该使用此方法,并且它应该在您的应用程序的initstate方法中: FirebaseMessaging.instance.getInitialMessage().then((RemoteMessage message) { // add your navigation function or, any others });


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接