【问题标题】:flutter redirect to different pages on click on different kinds of notification点击不同类型的通知,颤动重定向到不同的页面
【发布时间】:2021-01-28 13:05:42
【问题描述】:

我有 2 种 Firestore 触发器来向用户发送通知 - 1 是在新用户创建请求表单时触发,2 是在用户收到另一个用户的消息时触发。对于每种情况,我想在点击通知时将用户重定向到 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>

【问题讨论】:

  • 您必须简单地将数据从您的 fireabse 控制台传递到 json 格式的通知,在此基础上您可以浏览页面
  • @ShubhamNarkhede 感谢 Shubham。我不明白“将数据从firebase控制台传递到json格式的通知”是什么意思。请给我举个例子好吗?
  • 请检查我的回答

标签: flutter push-notification firebase-cloud-messaging


【解决方案1】:

Here is data 您需要从 Firebase 控制台发送

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

【讨论】:

  • 我是在控制台上完成的,并将您的代码写在颤振文件中。当我为这样的云函数编写代码 const message = { "notification": { title: .. , body: .. }, data: {click_action: 'FLUTTER_NOTIFICATION_CLICK', screen: 'ChatClass'}, 并执行代码时,我有一个错误提示 Exactly one of topic, token or condition is required。似乎是什么问题?
  • 您必须在 Firebase 控制台的目标部分中选择应用程序
  • 我在 Firebase 控制台的目标部分中选择了该应用程序,并按照您所说的做了一切,但我无法重定向到不同的页面。我的应用会在上次点击通知时打开。
  • @kyung 请在您的问题中添加您的 android 清单文件。其次请打印此值message['data'] 并检查是否有任何数据进入此变量
  • 我发布了我的 AndroidManifest 文件。另外,我通过我的应用检查了我的message['data']
【解决方案2】:

也许你可以创建一些枚举?

`枚举 NotificationType{

}`

您的通知将多出一个字段

class Notification{ final NotificationType type; }

你可以从你的firestore传递它并检查它吗?

【讨论】:

  • 谢谢凯伦。我应该在哪里创建枚举?在颤振文件中,还是在我的云函数文件中?什么通知会多一个字段?在颤振文件中,还是在我的云函数文件中?请您详细说明一下?
  • 它对我来说是一些新事物,很抱歉可能是不好的解决方案,但我认为你会在你的颤振文件中创建你的枚举,并且当你在你的 firestore 中发出通知时,你可以在更多领域进行像“notificationType:”MessageNotification“”,当你的应用决定获取你的通知时,你会得到那个字段,将它投射到你的枚举字段并检查要显示的通知
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-07
  • 2020-12-28
  • 2020-09-20
  • 1970-01-01
  • 1970-01-01
  • 2015-10-22
相关资源
最近更新 更多