问题描述
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 中编写代码并导航到您指定的屏幕。