gpt4 book ai didi

flutter - 如何在前台接收推送通知时显示 flutter 本地通知?

转载 作者:行者123 更新时间:2023-12-03 02:41:35 24 4
gpt4 key购买 nike

目前,我正在使用警报对话框在应用程序处于前台时接收推送通知时显示通知。但我想在 flutter 中显示一些非侵入性的东西,比如本地通知。我如何在我的应用程序中实现它?这是我目前的实现:

FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
@override
void initState() {
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
showDialog(
context: context,
builder: (context) => AlertDialog(
content: ListTile(
title: Text(message['notification']['title']),
subtitle: Text(message['notification']['body']),
),
actions: <Widget>[
FlatButton(
child: Text('Ok'),
onPressed: () => Navigator.of(context).pop(),
),
],
),
);
print("onMessage: $message");
},
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");
Navigator.pushNamed(context, '/notify');
},
onResume: (Map<String, dynamic> message) async {
print("onResume: $message");
},
);
}

最佳答案

由于我没有得到答案,所以我自己尝试并弄清楚了。
不要忘记添加 flutter_local_notifications库到您的项目以处理此代码
https://pub.dev/packages/flutter_local_notifications
已测试并适用于 Android

FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
@override
void initState() {
var initializationSettingsAndroid =
new AndroidInitializationSettings('@mipmap/ic_launcher');
var initializationSettingsIOS = new IOSInitializationSettings();
var initializationSettings = new InitializationSettings(
initializationSettingsAndroid, initializationSettingsIOS);
flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: onSelectNotification);
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
showNotification(
message['notification']['title'], message['notification']['body']);
print("onMessage: $message");
},
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");
Navigator.pushNamed(context, '/notify');
},
onResume: (Map<String, dynamic> message) async {
print("onResume: $message");
},
);
}
Future onSelectNotification(String payload) async {
showDialog(
context: context,
builder: (_) {
return new AlertDialog(
title: Text("PayLoad"),
content: Text("Payload : $payload"),
);
},
);
}
void showNotification(String title, String body) async {
await _demoNotification(title, body);
}

Future<void> _demoNotification(String title, String body) async {
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'channel_ID', 'channel name', 'channel description',
importance: Importance.Max,
playSound: true,
sound: 'sound',
showProgress: true,
priority: Priority.High,
ticker: 'test ticker');

var iOSChannelSpecifics = IOSNotificationDetails();
var platformChannelSpecifics = NotificationDetails(
androidPlatformChannelSpecifics, iOSChannelSpecifics);
await flutterLocalNotificationsPlugin
.show(0, title, body, platformChannelSpecifics, payload: 'test');
}
它就像一个魅力。希望能帮助别人:)

关于flutter - 如何在前台接收推送通知时显示 flutter 本地通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60722108/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com