我想实现从 firebase (fcm) 接收通知的应用程序,当收到通知时,无论应用程序被终止还是在后台,都将应用程序启动到特定屏幕,我尝试在 onBackgroundMessage 中调用本机代码,但没有任何帮助
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");
printy();
// TODO optional
},
onBackgroundMessage:Platform.isIOS ? null : _backgroundMessageHandler,
onResume: (Map<String, dynamic> message) async {
print("onResume: $message");
printy();
// TODO optional
},
Run Code Online (Sandbox Code Playgroud)
这对我有用。创建通知处理程序:
class NotificationHandler { GlobalKey<NavigatorState> _navigatorKey;
NavigatorState get _navigator => _navigatorKey.currentState!; NotificationHandler(this._navigatorKey) {
listenNotifications();
_navigator.pushAndRemoveUntil<void>(
routeGenerator(routeHome), (route) => false); }
void listenNotifications() =>
NotificationApi.onNotification.stream.listen(onClickedNotification);
void onClickedNotification(String? payload) {
//TODO: Access different screens according to the notification information
_navigator.pushAndRemoveUntil<void>(
routeGenerator(routeHome), (route) => false); } }
Run Code Online (Sandbox Code Playgroud)
我的通知API相关代码:
class NotificationApi {
static final notifications = FlutterLocalNotificationsPlugin();
static final onNotification = BehaviorSubject<String?>();
static notificationDetails() {
return const NotificationDetails(
android: AndroidNotificationDetails('protocol', 'protocol channel',
importance: Importance.max, priority: Priority.max),
iOS: IOSNotificationDetails());
} ... It continues
Run Code Online (Sandbox Code Playgroud)
然后在您的应用程序初始化中对其进行初始化,在我的情况下,在检查登录是否正确后会出现此行:
通知处理程序(rootNavigatorKey);
请注意,这仍在开发和测试中,在初始化之前,在应用程序至少运行一次之前,我还没有经过测试,并且我与我的 WorkManager 发生了一些冲突,这可能会影响,但我希望它能给你一个初始点。
| 归档时间: |
|
| 查看次数: |
477 次 |
| 最近记录: |