在点击不同类型的通知时颤动重定向到不同的页面
flutter redirect to different pages on click on different kinds of notification
我有 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>
也许你可以创建一些枚举?
`枚举通知类型{
}`
您的通知将多一个字段
class Notification{ final NotificationType type; }
你可以从你的 firestore 传递它并检查它吗?
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));
}
您可以参考这个 gist link 来获取您的初始化 FCM 数据
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage notiofication) {
var data = message['data'];
if (message.data['type'] == 'chat') {
Navigator.pushNamed(context, '/notiofication',
arguments: ChatArguments(notification);
}
});
我有 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>
也许你可以创建一些枚举?
`枚举通知类型{
}`
您的通知将多一个字段
class Notification{ final NotificationType type; }
你可以从你的 firestore 传递它并检查它吗?
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));
}
您可以参考这个 gist link 来获取您的初始化 FCM 数据
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage notiofication) {
var data = message['data'];
if (message.data['type'] == 'chat') {
Navigator.pushNamed(context, '/notiofication',
arguments: ChatArguments(notification);
}
});