flutter重定向到不同页面点击不同种类的通知

sgtfey8w  于 2023-05-01  发布在  Flutter
关注(0)|答案(3)|浏览(159)

我有两种firestore触发器来向用户发送通知-一种触发器是在新用户创建请求表单时触发的,另一种触发器是在用户收到来自另一个用户的消息时触发的。对于每种情况下,我想重定向用户到2个不同的页面上通知点击。第一种情况为notifications页,第二种情况为chatRoom页。这是我的代码,我不知道如何根据2种不同的情况重定向到2个不同的页面。请给予我一些建议。

@override
 void initState() {
  super.initState();
 _firebaseMessaging.configure(
    onMessage: (Map<String, dynamic> message) async {

  var data = message['data'];
  if (data['screen'].toString() == 'ChatClass') {
    Navigator.push(... (context) => ChatRoom()));
  } else {
    Navigator.push(... (context) => Notifications()));
  }
}, onLaunch: (Map<String, dynamic> message) async {

  var data = message['data'];
  if (data['screen'].toString() == 'ChatClass') {
    Navigator.push(... (context) => ChatRoom()));
  } else {
    Navigator.push(... (context) => Notifications()));
  }
}, onResume: (Map<String, dynamic> message) async {
  
  var data = message['data'];
  if (data['screen'].toString() == 'ChatClass') {
    Navigator.push(... (context) => ChatRoom()));
  } else {
    Navigator.push(...(context) => Notifications()));
  }
});

}
这是我的AndroidManifest文件;

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myappName">
//
    
<application
    android:name=".Application"
    android:label="myapp"
    android:icon="@mipmap/ic_launcher">
    
    <activity
        android:name=".MainActivity"
        android:launchMode="singleTop"
        android:theme="@style/LaunchTheme"
        android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
        android:hardwareAccelerated="true"
        android:windowSoftInputMode="adjustResize">
        //
        <meta-data
          android:name="io.flutter.embedding.android.NormalTheme"
          android:resource="@style/NormalTheme"
          />
        //
        <meta-data
          android:name="io.flutter.embedding.android.SplashScreenDrawable"
          android:resource="@drawable/launch_background"
          />
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_channel_id"
            android:value="high_importance_channel" />
        
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
        <intent-filter>
            <action android:name="FLUTTER_NOTIFICATION_CLICK" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    //
    <meta-data
        android:name="flutterEmbedding"
        android:value="2" />
</application>
z8dt9xmd

z8dt9xmd1#

FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage notification) {
     var data = message['data'];

     if (message.data['type'] == 'chat') {
          Navigator.pushNamed(context, '/notification', arguments: ChatArguments(notification);
     }
});
juud5qan

juud5qan2#

需要从firebase控制台发送的Here is data

notificationSecondCall() {
    firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) async {

        // we received notification when app is in foreground 

        var data = message['data'];
        if (data['screen'].toString() == "ChatClass") {
          gotoChatRoom();  //==  your navigator method to Chat room class
        } else {
          gotoNotifications();   //==  your navigator method for Notification class
        }
      },

      onResume: (Map<String, dynamic> message) async {

        // we received notification when app is in background

        var data = message['data'];
        if (data['screen'].toString() == "ChatClass") {
          gotoChatRoom();  //==  your navigator method to Chat room class
        } else {
          gotoNotifications();   //==  your navigator method for Notification class
        }
      },

      onLaunch: (Map<String, dynamic> message) async {},
    );

    firebaseMessaging.requestNotificationPermissions(
        const IosNotificationSettings(
            sound: true, badge: true, alert: true, provisional: false));
  }

您可以参考link要点来初始化FCM数据

6ojccjat

6ojccjat3#

也许你可以创建一些枚举?
enum通知类型{ }
您的通知将多一个字段
class Notification{ final NotificationType type; }
你可以从你的消防站拿过来检查一下

相关问题