android - 应用打开时在整个应用中显示 FCM 通知 (Flutter)
问题描述
我现在正在使用 FCM 向我的设备发送推送通知,并且运行良好。但是,当应用程序打开时,我只有在该特定页面中时才会执行 onResume。无论用户在哪个页面(或类)上,我都想在顶部显示通知。基本上我希望通知在全球范围内显示(显示弹出窗口)。任何帮助,将不胜感激。这是显示通知的页面中的代码。
if (Platform.isIOS) {
iosSubscription = _fcm.onIosSettingsRegistered.listen((data) {
_saveDeviceToken();
});
_fcm.requestNotificationPermissions(IosNotificationSettings());
} else {
_saveDeviceToken();
}
_fcm.configure(
onMessage: (Map<String, dynamic> message) async {
print("onMessage: $message");
var temp = message['notification'];
setState(() {
title.add(temp['title']);
body.add(temp['body']);
});
showDialog(
context: context,
builder: (context) => AlertDialog(
content: ListTile(
title: Text(message['notification']['title']),
subtitle: Text(message['notification']['body']),
),
actions: <Widget>[
FlatButton(
color: const Color(0xFF650572),
child: Text('Ok'),
onPressed: () => Navigator.of(context).pop(),
),
],
),
);
},
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");
Navigator.push(
context, MaterialPageRoute(builder: (context) => MessageHandler()));
// TODO optional
},
onResume: (Map<String, dynamic> message) async {
print("onResume: $message");
Navigator.push(context,
MaterialPageRoute(builder: (context) => MessageHandler()));
// TODO optional
},
);
}
解决方案
将您的 MaterialApp 包装在一个包装类中......让我们称之为 FCMWrapper。
class FCMWrapper extends StatefulWidget {
final Widget child;
const FCMWrapper({Key key, this.child}) : super(key: key);
@override
_FCMWrapperState createState() => _FCMWrapperState();
}
class _FCMWrapperState extends State<FCMWrapper> {
@override
Widget build(BuildContext context) {
return Consumer<YourObservable>(
builder: (context, yourObservable, _) {
if (yourObservable != null && yourObservable.isNotEmpty) {
Future(
() => navigatorKey.currentState.push(
PushNotificationRoute(
child: YourViewOnNotification(),
)),
),
);
}
return widget.child;
},
);
}
}
我已经将我的数据存储在一个单独的类的可观察对象中。因此,当我收到通知时,我会更新我的 observable。由于我们正在使用该 observable,因此将调用 PushNotificationRoute。
PushNotificationRoute 只是一个扩展 ModalRoute 的类。
class PushNotificationRoute extends ModalRoute {
final Widget child;
PushNotificationRoute({this.child});
... //Override other methods to your requirement
//This is important
@override
Widget buildPage(BuildContext context, Animation<double> animation,
Animation<double> secondaryAnimation) {
return SafeArea(
child: Builder(builder: (BuildContext context) {
return child;
}),
);
}
...
@override
Duration get transitionDuration => Duration(milliseconds: 200);
}
现在在 main.dart 中声明一个全局键,如
var navigatorKey = GlobalKey<NavigatorState>();
并像包装你的 MaterialApp
...
FCMWrapper(
child: MaterialApp(
navigatorKey: navigatorKey,
title: 'Your App',
...
因此,现在每次收到通知时,您的 observable 都应该更新并推送一条模态路线,该路线将显示在应用程序的任何位置。
推荐阅读
- php - 我应该如何在多个目录中正确包含同一个文件?
- flutter - 如何在颤动上创建大纲 DropdownMenuItem
- reactjs - 向antd Dropdown覆盖属性添加元素导致React单子错误
- opencv - 使用 CUDA 10.2 Threads::Threads 错误构建 OpenCV 4.1.1
- java - 如何让线程等到变量初始化?
- javascript - Vue.js - 在数组中的字符串内添加 html 标签
- flutter - 使用和自定义 Flutter ExtendedTextFeild Pakage 后,当单词高亮时,光标会转到文本字段的开头
- python - AttributeError:“AccumOptimizer”对象没有属性“lr”
- python - 将烧瓶中的系列传递给chart.JS
- nix - 在 nix 中使用最新版本的软件包