firebase - 如何从后台打开我的应用程序并导航到接收通知消息的页面?
问题描述
我需要在收到通知消息时自动打开应用程序。颤振有可能吗?
下面是处理后台消息的代码,它可以工作。
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
await Firebase.initializeApp();
print('Handling a background message ${message.messageId}');
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
......
runApp(
MaterialApp(
debugShowCheckedModeBanner: false,
home: MyApp()
)
);
我需要的是我有一个单独的页面,当执行该特定功能时需要弹出一个单独的页面(当应用程序在后台时)。谁能帮我解决这个问题!提前致谢
解决方案
await _firebaseMessaging.subscribeToTopic('topic name');
_firebaseMessaging.configure
(
// The onMessage function triggers when the notification is
received while we are running the app.
onMessage: (message) async
{
setState(()
{
messageTitle = message["notification"]["title"];
messageDescription = message["notification"]["description"];
notificationAlert = "New Notification Alert";
});
},
// The onResume function triggers when we receive the notification alert in the device notification bar and opens the app through the push notification itself. In this case, the app can be running in the background or not running at all.
onResume: (message) async
{
print("ON RESUME");
setState(()
{
messageTitle = message["data"]["title"];
messageDescription = message["notification"]["description"];
notificationAlert = "Application opened from Notification";
});
},
onLaunch: (Map<String, dynamic> message) async // Called when app is terminated
{
print("onLaunch: $message");
var data = message["data"];
print(data);
// Navigator.pushNamed(context, "details");
}
);
在此代码中,onResume函数将帮助您从后台运行应用程序,以便您可以在 onResume 中编写代码并导航到您指定的屏幕。
推荐阅读
- css - 只有当它们达到最小宽度时才使弹性项目换行
- php - 如何将肥皂服务器中创建的会话传输到肥皂客户端代码?
- amazon-web-services - 在 CodeDeploy 中替换 EBS 环境变量
- r - 如何在 R 中将具有多个 LHS 的模型的结果制成表格?
- reactjs - 部署 React 应用,Router 不正确
- android - Gradle 没有在 Android Studio 中构建简单的测试
- python - 使用 Groupby 的 Pandas 窗口未按预期工作
- css - 将样式注入测试页面
- azure-ad-b2c - Azure AD B2C 电子邮件登录名存储
- python-3.x - 乘法 Python 脚本给出错误的输出