Flutter 无法使用snackbar显示通知。是否有一个好的方法通知用户?-颤振
为什么这在颤振中不起作用。Snackbar未显示。还有别的办法吗? 我只想显示通知 不过,控制台中没有错误 此外,我希望通知显示在我当前所在的任何页面上。有人能帮我做到这一点吗 提前谢谢Flutter 无法使用snackbar显示通知。是否有一个好的方法通知用户?-颤振,flutter,dart,Flutter,Dart,为什么这在颤振中不起作用。Snackbar未显示。还有别的办法吗? 我只想显示通知 不过,控制台中没有错误 此外,我希望通知显示在我当前所在的任何页面上。有人能帮我做到这一点吗 提前谢谢 class _KudaBinState extends State<KudaBin> { final _model = MainModel(); final FirebaseMessaging _firebaseMessaging = FirebaseMessaging(); @ove
class _KudaBinState extends State<KudaBin> {
final _model = MainModel();
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
@override
void initState() {
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print("onMessage: $message");
final notification = message['notification'];
Scaffold.of(context).showSnackBar(SnackBar(
content: Text(notification["title"] + " " + notification["body"])));
},
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");
final notification = message['data'];
},
onResume: (Map<String, dynamic> message) async {
print("onResume: $message");
},
);
_firebaseMessaging.requestNotificationPermissions(
const IosNotificationSettings(sound: true, badge: true, alert: true));
}
@override
Widget build(BuildContext context) {
return ScopedModel<MainModel>(
model: _model,
child: MaterialApp(
theme: ThemeData(
brightness: Brightness.light,
primaryColor: Color(0xffe46b10),
accentColor: Color(0xff0f03fc),
textTheme: TextTheme(body1: TextStyle(color: Colors.purple)),
),
home: ScopedModelDescendant(
builder: (BuildContext context, Widget widget, MainModel model) {
return _model.isAuthenticated
? Requests(_model)
: FutureBuilder(
future: _model.autoAuthenticate(),
builder: (context, authResultSnapShot) {
return authResultSnapShot.connectionState ==
ConnectionState.waiting
? SplashScreen()
: WelcomePage(_model);
},
);
}),
debugShowCheckedModeBanner: false),
);
}
}
class\u KudaInstate扩展状态{
最终_model=MainModel();
最终FirebaseMessaging_FirebaseMessaging=FirebaseMessaging();
@凌驾
void initState(){
_firebaseMessaging.configure(
onMessage:(映射消息)异步{
打印(“onMessage:$message”);
最终通知=消息[‘通知’];
Scaffold.of(上下文).showSnackBar(SnackBar(
内容:文本(通知[“标题”]+“”+通知[“正文”);
},
onLaunch:(映射消息)异步{
打印(“onLaunch:$message”);
最终通知=消息['data'];
},
onResume:(映射消息)异步{
打印(“onResume:$message”);
},
);
_firebaseMessaging.requestNotificationPermissions(
const IosNotificationSettings(声音:true、徽章:true、警报:true));
}
@凌驾
小部件构建(构建上下文){
返回范围模型(
模型:_模型,
孩子:MaterialApp(
主题:主题数据(
亮度:亮度,亮度,
原色:颜色(0xffe46b10),
accentColor:Color(0xff0f03fc),
textTheme:textTheme(body1:TextStyle(颜色:Colors.purple)),
),
主页:ScopedModelSecondant(
生成器:(BuildContext上下文、小部件小部件、MainModel){
return\u model.isAuthenticated
?请求(_模型)
:未来建设者(
未来:_model.autoAuthenticate(),
生成器:(上下文,authResultSnapShot){
返回authResultSnapShot.connectionState==
连接状态等待
?防溅屏()
:WelcomePage(_型号);
},
);
}),
debugShowCheckedModeBanner:false),
);
}
}
编辑-它不必是snackbar。但是,根据我当前的代码,有没有办法在所有页面上通知用户?我没有看到任何
Scaffold
小部件,添加一个Scaffold小部件,为Scaffold设置一个GlobalKey()
//inside a state of a widget
final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
build(context){
return Scaffold(
key: _scaffoldKey,
body: //...,
);
}
使用插件进行本地通知,
首先,在FirebaseMessaging
插件之前初始化flatter\u local\u通知
插件,然后在onMessage
中使用flatter local notification显示通知。
您可以更改通知中的样式和其他内容
例如:
onMessage: (Map<String, dynamic> message) async {
print("onMessage: $message");
final notification = message['notification'];
await flutterLocalNotificationsPlugin.show(
1,notification,'subtitle',secondNotificationPlatformSpecifics);
},
onMessage: (Map<String, dynamic> message) async {
print("onMessage: $message");
final notification = message['notification'];
Get.snackbar('Hi',message);
},
onMessage:(映射消息)异步{
打印(“onMessage:$message”);
最终通知=消息[‘通知’];
等待本地通知显示(
1、通知,“副标题”,第二通知平台说明);
},
注意:上面的代码只是一个示例,说明了如何在firebase消息传递配置中使用flatter_local_通知来显示通知。有关完整示例,请参阅
关于Snackbar:
您需要一个Scaffold小部件来显示snackbar。如果你不想用,那么你可以用
例如:
onMessage: (Map<String, dynamic> message) async {
print("onMessage: $message");
final notification = message['notification'];
await flutterLocalNotificationsPlugin.show(
1,notification,'subtitle',secondNotificationPlatformSpecifics);
},
onMessage: (Map<String, dynamic> message) async {
print("onMessage: $message");
final notification = message['notification'];
Get.snackbar('Hi',message);
},
onMessage:(映射消息)异步{
打印(“onMessage:$message”);
最终通知=消息[‘通知’];
Get.snackbar('Hi',message);
},
Firebase消息在应用程序打开时不显示通知。当onMessage
触发时,您需要实现本地通知。它会记录消息吗?@JagrajSingh您能告诉我一些触发本地通知的方法吗?@neuromancer您在谈论什么日志?打印(“onMessage:$message”);这个语句打印出来了吗?SplashScreen和Welcome页面都有scaffold小部件。我不想把它带到material应用程序中,因为我的项目中有很多页面,为每个scafold创建一个全局键在我的项目中是不可行的。我只想通知用户。只有万能的脚手架才能显示SnackBar,否则请使用pub.deva中的FlushBar。您是否有可能使用多个材质应用程序?没有多个脚手架Widgets它们只是一个材质应用程序