我正在编写一个应用程序,您可以在其中按下一个按钮,它会向每个应用程序发送"帮助"通知(这将是一个大约 100 人的封闭组(我需要 notifiction 来弹出应用程序处于的任何状态,我已经对 onResume 和 onLaunch 进行了排序,但我无法让它在应用程序处于前台时显示通知。有很多屏幕,我需要通知显示在用户所在的任何屏幕上。
我尝试了很多教程,但就是无法让它工作。我的主页上的初始化状态中有代码。
void main() {
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
FirebaseMessaging _firebaseMessaging = FirebaseMessaging();
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
@override
void initState() {
super.initState();
var initializationSettingsAndroid =
new AndroidInitializationSettings('@mipmap/ic_launcher');
var initializationSettingsIOS = IOSInitializationSettings();
var initializationSettings = InitializationSettings(
initializationSettingsAndroid, initializationSettingsIOS);
flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: onSelectNotification);
_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print("onMessage: $message");
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(),
),
],
),
);
},
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message");
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(),
),
],
),
);
},
onResume: (Map<String, dynamic> message) async {
print("onResume: $message");
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(),
),
],
),
);
},
);
Future onSelectNotification(String payload) async {
if (payload != null) {
debugPrint('notification payload: ' + payload);
var androidPlatformChannelSpecifics = AndroidNotificationDetails(
'your channel id', 'your channel name', 'your channel description',
importance: Importance.Max, priority: Priority.High,ticker:'ticker');
var iOSPlatformChannelSpecifics = IOSNotificationDetails();
var platformChannelSpecifics = NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0, 'plain title', 'plain body', platformChannelSpecifics,
payload: 'item x');
}
}
// @override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Benidorm or Bust',
theme: ThemeData.dark().copyWith(
primaryColor: Color(0xFF0A0E21),
scaffoldBackgroundColor: Color(0xFF0A0E21),
),
initialRoute: '/',
routes: {
'/': (context) => MyHomePage(),
'/setup': (context) => Setup(),
'/dayinit': (context) => Dayinit(),
'/day1': (context) => Day1(),
'/day2': (context) => Day2(),
'/day3': (context) => Day3(),
'/day4': (context) => Day4(),
'/day5': (context) => Day5(),
'/CheckList': (context) => CheckList(),
'/eveinit': (context) => Eveinit(),
'eve1eve': (context) => Eve1Eve(),
// 'push':(context)=> Push(),
},
);
}
}
我没有收到任何错误,只是什么也没发生。
我遇到了同样的问题,只需将与Firebase相关的代码从"应用程序"MyApp移动到MyHomePage小部件即可。在我的主页中,它将具有适当的上下文。 我以此为例 https://github.com/FirebaseExtended/flutterfire/blob/master/packages/firebase_messaging/example/lib/main.dart#L240