【发布时间】:2020-12-22 08:12:54
【问题描述】:
有一个颤振应用程序。正在使用 FCM 发送推送通知。
所需的行为是在单击通知时,它应该进入显示通知详细信息的屏幕。该屏幕已实现。
在应用程序处于后台时单击通知,单击将进入所需的屏幕,但当应用程序在单击时启动时,它打开的是主屏幕而不是所需的通知屏幕。
推送通知服务代码。
class PushNotificationsManager {
PushNotificationsManager._();
factory PushNotificationsManager() => _instance;
static final PushNotificationsManager _instance = PushNotificationsManager._();
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
bool _initialized = false;
Future<void> init() async {
if (!_initialized) {
// For iOS request permission first.
_firebaseMessaging.requestNotificationPermissions();
//configure
_firebaseMessaging.configure(
//Triggered if a message is received whilst the app is in foreground
onMessage: (Map<String, dynamic> message) async {
print("onMessage: $message");
// _navigateToItemDetail(message);
// _showItemDialog(message);
},
//this line causes hot reload error
// onBackgroundMessage: myBackgroundMessageHandler,
//Triggered if a message is received if the app was terminated
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");
_navigateToItemDetail(message);
},
//Triggered if a message is received whilst the app is in background
onResume: (Map<String, dynamic> message) async {
print("onResume: $message");
_navigateToItemDetail(message);
},
);
// For testing purposes print the Firebase Messaging token
String token = await _firebaseMessaging.getToken();
print("FirebaseMessaging token: $token");
_initialized = true;
}
}
void _navigateToItemDetail(Map<String, dynamic> message) {
print("Navigating to notification screen");
bool isValid = message["data"]["title"] != null && message["data"]["body"] != null;
if(!isValid) return;
navigatorKey.currentState.pushNamedAndRemoveUntil(notificationScreenRoute, (route) => false, arguments: message);
// navigatorKey.currentState.pushNamed(notificationScreenRoute, arguments: message);
}
}
main.dart -- 这里我正在初始化推送通知服务
//global navigator key,used where buildContext is not available (in business logic)
final GlobalKey<NavigatorState> navigatorKey = new GlobalKey<NavigatorState>();
void main() async {
WidgetsFlutterBinding.ensureInitialized();
//init Hive
await initHiveDb();
PushNotificationsManager _pushNotificationManager = PushNotificationsManager();
_pushNotificationManager.init();
// get the dynamic link service
final DynamicLinkService _dynamicLinkService = DynamicLinkService();
// call handle dynamic links
await _dynamicLinkService.handleDynamicLinks();
//run app, restrict orientaton to portrait up
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
systemNavigationBarColor: kMaroonColor
));
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]).then((_) {
runApp(
MaterialApp(
debugShowCheckedModeBanner: true,
theme: (appLightTheme).copyWith(
//textTheme: GoogleFonts.latoTextTheme()
),
onGenerateRoute: RouteGenerator.generateRoute,
navigatorKey: navigatorKey,
home: App()
)
);
});
}
【问题讨论】:
标签: firebase flutter push-notification firebase-cloud-messaging