Hee
Hee

Reputation: 187

flutter redirect to different pages on click on different kinds of notification

I have 2 kinds of firestore triggers to send notifications to users - 1 is triggered when a new user creates a request form and 2 is triggered when a user receives a message from another user. For each case, I want to redirect users to 2 different pages on notification click. notifications page for the first case, and chatRoom page for the second case. Here is my code, and I can't figure out how to redirect to 2 different pages based on 2 different cases. Please give me some advice.

  @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()));
  }
});

}

Here is my AndroidManifest file;

<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>

Upvotes: 1

Views: 8205

Answers (3)

Abhishek Singh
Abhishek Singh

Reputation: 29

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

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

Upvotes: 2

Shubham Narkhede
Shubham Narkhede

Reputation: 2130

Here is data that you need to send from the firebase console

  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));
  }

You can refer to this gist link for your initialize FCM data

Upvotes: 0

Karen Ghazaryan
Karen Ghazaryan

Reputation: 155

maybe you can create some enum?

`enum NotificationType{

}`

and your notification will have one more field

class Notification{ final NotificationType type; }

and you can pass it from your firestore and check it?

Upvotes: 0

Related Questions