我正在编写一个可以按下按钮的应用程序,它将向每个应用程序发送“帮助”通知(这是一个封闭的组,大约100个),我需要通知以弹出该应用程序处于任何状态的信息在中,我对onResume和onLaunch进行了排序,但是当应用程序处于前台时,我无法获取它来显示通知。屏幕很多,我需要通知才能显示在用户所在的任何屏幕上。
我尝试了许多教程,但无法使其正常工作。我的主页上的initState中包含代码。
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(),
},
);
}
}
我没有收到任何错误,什么都没发生。
答案 0 :(得分:1)
我遇到了同样的问题,只是将与Firebase相关的代码从“ app” MyApp移至MyHomePage小部件。在MyHomePage内部,它将具有适当的上下文。 我以此为例https://github.com/FirebaseExtended/flutterfire/blob/master/packages/firebase_messaging/example/lib/main.dart#L240