我使用FCM推送通知与扑动,前台通知工作良好,但在后台它没有得到任何通知。
这是我的代码
class Home extends StatefulWidget {
@override_homeestate createState() =>_HomeState ();}
class _HomeState extends State<Home> {
String token = '';
final FirebaseMessaging firebaseMessaging = FirebaseMessaging.instance;
@override
void initState() {
getToken();
firebaseMessaging.requestPermission(
alert: true, badge: true, provisional: true, sound: true);
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
print("onMessage: $message");
});
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
print("onMessageOpenedApp: $message");
});
super.initState();
}
void getToken() async {
token = (await firebaseMessaging.getToken())!;
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(child: Text("Token : $token")),
floatingActionButton: FloatingActionButton(
onPressed: () {
print(token);
},
child: Icon(Icons.print),
),
);
}
}
您需要定义一个顶级命名处理程序,自从上次firebase_messaging 10.0.6。通知将在后台工作首先更新你的firebase_messaging 10.0.6
///定义一个顶级命名处理程序,后台/终止消息将///电话。//////检查本地平台的日志。
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
// If you're going to use other Firebase services in the background, such as Firestore,
// make sure you call `initializeApp` before using other Firebase services.
await Firebase.initializeApp();
print('Handling a background message ${message.messageId}');
}
查看这里的完整示例:firebase_messaging/example
如果你需要一个真实的项目示例完整的示例