Flutter:本地通知插件显示两次通知
Flutter: Local Notifications Plugin showing notification twice
我在特定时间从我的服务器收到通知,向用户显示通知。但是,每当我的设备收到来自 Firebase Cloud Messaging 的消息时,FlutterLocalNotificationsPlugin
最终会显示两个通知,如下所示。
我已经确认我从我的服务器收到了一个通知,我认为它只是在前端的某个地方处理不正确。
这是我在后台调用它的代码
main
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}');
if (message.notification != null) {
print(message.notification.title);
print(message.notification.body);
flutterLocalNotificationsPlugin.show(
message.notification.hashCode,
message.notification.title,
message.notification.body,
NotificationDetails(
android: AndroidNotificationDetails(
channel.id,
channel.name,
channel.description,
icon: 'launch_background',
),
iOS: IOSNotificationDetails()
));
}
// Create a notification for this
}
const AndroidNotificationChannel channel = AndroidNotificationChannel(
'high_importance_channel', // id
'High Importance Notifications', // title
'This channel is used for important notifications.', // description
importance: Importance.high,
);
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
// Starting FlutterFire through the suggestion found here
// https://firebase.flutter.dev/docs/overview
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
/// Create an Android Notification Channel.
/// We use this channel in the `AndroidManifest.xml` file to override the
/// default FCM channel to enable heads up notifications.
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
/// Update the iOS foreground notification presentation options to allow
/// heads up notifications.
await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
alert: true,
badge: true,
sound: true,
);
runApp(MyApp());
}
class MyApp extends StatefulWidget {
MyApp({Key key}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
User _user;
FirebaseAnalytics analytics;
bool _error = false;
void _handleLogin(User user) {
// print("Handle Login");
setState(() {
_user = user;
});
}
@override
void initState() {
super.initState();
// if we opened the app through a notification, it will send us via this
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
print('On Message Opened!');
print(message);
});
}
@override
Widget build(BuildContext context) {
}
}
以防万一有人遇到同样的问题。我重新阅读了 FlutterFire 上的文档,其中提到如果您拥有当前最新的 Flutter SDK 和 Firebase,那么它会尝试自动显示它。我的错误是我除此之外还实施了 FlutterLocalNotifications
,并且我收到了两个通知,一个是手动的,另一个是自动的。
向您的设备发送消息时,请确保它是 notification
而不是 data
。该文档提到如果您发送 data
它可能会被忽略为推送通知。仅使用它来向应用程序发送数据包。
注释这部分代码可以解决问题。
/// 更新 iOS 前台通知显示选项以允许
/// 抬头通知。
等待
FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
alert: true,
badge: true,
sound: true, );
我在特定时间从我的服务器收到通知,向用户显示通知。但是,每当我的设备收到来自 Firebase Cloud Messaging 的消息时,FlutterLocalNotificationsPlugin
最终会显示两个通知,如下所示。
我已经确认我从我的服务器收到了一个通知,我认为它只是在前端的某个地方处理不正确。
这是我在后台调用它的代码
main
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}');
if (message.notification != null) {
print(message.notification.title);
print(message.notification.body);
flutterLocalNotificationsPlugin.show(
message.notification.hashCode,
message.notification.title,
message.notification.body,
NotificationDetails(
android: AndroidNotificationDetails(
channel.id,
channel.name,
channel.description,
icon: 'launch_background',
),
iOS: IOSNotificationDetails()
));
}
// Create a notification for this
}
const AndroidNotificationChannel channel = AndroidNotificationChannel(
'high_importance_channel', // id
'High Importance Notifications', // title
'This channel is used for important notifications.', // description
importance: Importance.high,
);
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
// Starting FlutterFire through the suggestion found here
// https://firebase.flutter.dev/docs/overview
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
/// Create an Android Notification Channel.
/// We use this channel in the `AndroidManifest.xml` file to override the
/// default FCM channel to enable heads up notifications.
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
/// Update the iOS foreground notification presentation options to allow
/// heads up notifications.
await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(
alert: true,
badge: true,
sound: true,
);
runApp(MyApp());
}
class MyApp extends StatefulWidget {
MyApp({Key key}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
User _user;
FirebaseAnalytics analytics;
bool _error = false;
void _handleLogin(User user) {
// print("Handle Login");
setState(() {
_user = user;
});
}
@override
void initState() {
super.initState();
// if we opened the app through a notification, it will send us via this
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
print('On Message Opened!');
print(message);
});
}
@override
Widget build(BuildContext context) {
}
}
以防万一有人遇到同样的问题。我重新阅读了 FlutterFire 上的文档,其中提到如果您拥有当前最新的 Flutter SDK 和 Firebase,那么它会尝试自动显示它。我的错误是我除此之外还实施了 FlutterLocalNotifications
,并且我收到了两个通知,一个是手动的,另一个是自动的。
向您的设备发送消息时,请确保它是 notification
而不是 data
。该文档提到如果您发送 data
它可能会被忽略为推送通知。仅使用它来向应用程序发送数据包。
注释这部分代码可以解决问题。
/// 更新 iOS 前台通知显示选项以允许 /// 抬头通知。 等待
FirebaseMessaging.instance.setForegroundNotificationPresentationOptions( alert: true, badge: true, sound: true, );