无法在 android 中使用 flutter 获取后台通知
Can not get background notifications in android using flutter
我正在使用 FCM 通过 flutter 推送通知,前台通知运行良好,但在后台时它没有收到任何通知。
这是我的代码
class Home extends StatefulWidget {
@覆盖
_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),
),
);
}
}
您需要定义一个顶级命名处理程序,
自上次 firebase_messaging 10.0.6 以来。通知将在后台运行
首先更新你的 firebase_messaging 10.0.6
/// 定义一个顶级命名处理程序,background/terminated 消息将
/// 称呼。
///
/// 要验证一切正常,请查看本机平台日志。
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
如果您需要一个真实的项目示例complete example
我正在使用 FCM 通过 flutter 推送通知,前台通知运行良好,但在后台时它没有收到任何通知。
这是我的代码
class Home extends StatefulWidget {
@覆盖 _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),
),
);
}
}
您需要定义一个顶级命名处理程序, 自上次 firebase_messaging 10.0.6 以来。通知将在后台运行 首先更新你的 firebase_messaging 10.0.6
/// 定义一个顶级命名处理程序,background/terminated 消息将 /// 称呼。 /// /// 要验证一切正常,请查看本机平台日志。
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
如果您需要一个真实的项目示例complete example