I am using FCM to push notifications with flutter , foreground notifications works well but when in background it does not get any notification .
here is my code
class Home extends StatefulWidget {
@override _HomeState 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),
),
);
}
}