这是我的main.dart。当应用程序在前台或后台运行时,我会收到通知,但当应用程序终止/关闭时,我不会收到任何通知。我到处都找过了。但没有一个博客或帖子对我有帮助。
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'dart:io' show Platform;
void main() {
runApp(MyApp());
}
void DialogBox(String Title, String message, context, Function action) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20.0),
),
backgroundColor: Color(0XffC75A53),
title: Text(
Title,
textAlign: TextAlign.center,
style: TextStyle(
color: Color(0xFF111328), fontWeight: FontWeight.w900),
),
content: Text(
message,
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
),
),
actions: <Widget>[
FlatButton(
child: Text(
'OK',
style: TextStyle(
color: Color(0xFF111328),
),
),
onPressed: action,
),
],
);
});
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
final FirebaseMessaging _fcm = FirebaseMessaging();
final FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
StreamSubscription iosSubscription;
@override
void initState() {
super.initState();
var android = AndroidInitializationSettings('mipmap/ic_launcher');
var ios = IOSInitializationSettings();
var platform = InitializationSettings(android, ios);
_flutterLocalNotificationsPlugin.initialize(platform);
if (Platform.isIOS) {
iosSubscription = _fcm.onIosSettingsRegistered.listen((data) {
print(data);
});
_fcm.requestNotificationPermissions(IosNotificationSettings());
}
_fcm.configure(
// app in fg
onMessage: (Map<String, dynamic> message) async {
print("onMessage: $message");
showNotification(message);
DialogBox("Data", message.toString(), context, () {
Navigator.pop(context);
});
},
onLaunch: (Map<String, dynamic> message) async {
print("onLaunch: $message"); // app terminated
// Navigator.pushReplacementNamed(context, HomeScreen.id);
},
onResume: (Map<String, dynamic> message) async {
print("onResume: $message");
DialogBox("Data", message.toString(), context, () {
Navigator.pop(context);
});
// Navigator.pushReplacementNamed(context, HomeScreen.id);
// Navigator.of(context).pushNamed(message['data']['status']);
},
);
}
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
showNotification(Map<String, dynamic> message) async {
var android = AndroidNotificationDetails(
'channel_id', 'channel_name', 'channel_descriptions');
var ios = IOSNotificationDetails();
var platform = NotificationDetails(android, ios);
await _flutterLocalNotificationsPlugin.show(
0,
message['notification']['title'].toString(),
message['notification']['body'].toString(),
platform);
}
@override
void dispose() {
if (iosSubscription != null) iosSubscription.cancel();
super.dispose();
}
}
找到了!这是设备问题,有些设备不支持某些应用程序的后台通知。在其他设备上运行您的应用程序,它将按预期工作
FlutterFire包有一些限制,在9.0.2版本中,我们可以在文档中看到:
在iOS上,如果用户从应用Switcher中刷走应用程序,则必须再次手动重新打开应用程序,以便后台消息重新开始工作。
在Android上,如果用户强制从设备设置中退出应用程序,则必须再次手动重新打开应用程序,以便消息开始工作。
我认为第一个可能是你身上发生的事情。
来源:FlutterFire Docs