当应用程序终止或从后台“最近使用的应用程序”中删除时,我遇到了无法接收通知的问题。
我已经在build.gradle文件中的app或项目级别完成了所有必需的android应用程序设置。当应用程序处于打开状态或在最近使用的应用程序中时,我能够接收推送通知。
库版本
firebase_messaging: ^11.2.0
firebase_core: ^1.10.0
flutter_local_notifications: ^9.1.4
这是我的代码。
await Firebase.initializeApp();
FirebaseMessaging messaging = FirebaseMessaging.instance;
messaging.getToken().then((value) {
print('firebase token =$value');
});
FirebaseMessaging.onMessage.listen((RemoteMessage message) async {
//print(event.notification!.body);
RemoteNotification? notification = message.notification;
if (notification != null) {
print("Notification received when app in foreground");
}
});
FirebaseMessaging.onMessageOpenedApp.listen((message) {
print('Message clicked!');
});
await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
alert: true,
badge: true,
sound: true,
);
以下是 BackgroundMessage 处理程序代码
Future<void> _messageHandler(RemoteMessage message) async {
await Firebase.initializeApp();
RemoteNotification? notification = message.notification;
if (notification != null) {
print("Notification received when app in background");
}
}
以下是我的main.dart文件的完整代码。
Future<void> _messageHandler(RemoteMessage message) async {
await Firebase.initializeApp();
RemoteNotification? notification = message.notification;
if (notification != null) {
print("Notification received when app in background");
}
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
FirebaseMessaging.onBackgroundMessage(_messageHandler);
runApp(MyApp());
}
class MyApp extends StatefulWidget {
createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
bool isUserLoggedIn = true;
bool isLogoutAlertShown = false;
final materialAppKey = GlobalKey();
late FirebaseMessaging messaging;
@override
void initState() {
super.initState();
setUpNotification();
}
setUpNotification() async {
messaging = FirebaseMessaging.instance;
messaging.getToken().then((value) {
print('firebase token =$value');
//sendTokenToServer(value);
Pref.setFcmToken(token: '$value');
});
FirebaseMessaging.onMessage.listen((RemoteMessage message) async {
//print(event.notification!.body);
RemoteNotification? notification = message.notification;
if (notification != null) {
print("Notification received when app in foreground");
}
});
FirebaseMessaging.onMessageOpenedApp.listen((message) {
print('Message clicked!');
});
await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
alert: true,
badge: true,
sound: true,
);
}
@override
Widget build(BuildContext context) {
return _materialApp();
}
Widget _materialApp() {
return FutureBuilder(
future: _loginState(),
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return MaterialApp(
debugShowCheckedModeBanner: false,
key: materialAppKey,
darkTheme: AppTheme.lightTheme,
theme: AppTheme.lightTheme,
home: isUserLoggedIn == true ?
BottomNavigationContainer() : LoginOptions(),
);
} else {
return Container(color: Colors.white);
}
});
}
Future<void> _loginState() async {
final token = await Pref.getToken();
isUserLoggedIn = token.length > 0 ? true : false;
}
}
建议我缺少什么或者做错了什么。