Flutter App Firebase 推送通知不在后台
Flutter App Firebase push notification not coming in background
当应用 terminated/Remove 从最近的后台应用收到通知时,我遇到了问题。
我已经在 build.gradle 文件中完成 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;
}
}
告诉我我遗漏了什么或做错了什么。
感谢大家的回复。
我找到了为什么我的后台处理程序不工作的解决方案,因为我 运行在调试模式下安装我的应用程序。根据 FlutterFire Cloud messaging Doc 如果您杀死您的应用程序,后台方法在调试模式下不起作用。
您可以通过 运行 调试您的应用程序来测试您的后台处理程序方法,然后通过切换应用程序使其不再处于前台。
如果您想从最近的任务中检查应用程序 terminate/kill 之后,然后 运行 应用程序处于 发布模式 。工作正常
再次感谢大家。
当应用 terminated/Remove 从最近的后台应用收到通知时,我遇到了问题。
我已经在 build.gradle 文件中完成 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;
}
}
告诉我我遗漏了什么或做错了什么。
感谢大家的回复。
我找到了为什么我的后台处理程序不工作的解决方案,因为我 运行在调试模式下安装我的应用程序。根据 FlutterFire Cloud messaging Doc 如果您杀死您的应用程序,后台方法在调试模式下不起作用。
您可以通过 运行 调试您的应用程序来测试您的后台处理程序方法,然后通过切换应用程序使其不再处于前台。
如果您想从最近的任务中检查应用程序 terminate/kill 之后,然后 运行 应用程序处于 发布模式 。工作正常
再次感谢大家。