Flutter:本地通知插件显示两次通知
Posted
技术标签:
【中文标题】Flutter:本地通知插件显示两次通知【英文标题】:Flutter: Local Notifications Plugin showing notification twice 【发布时间】:2021-10-16 23:42:05 【问题描述】:我在特定时间从我的服务器接收通知,以向用户显示通知。但是,每当我的设备收到来自 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)
【问题讨论】:
【参考方案1】:以防万一有人偶然发现同样的问题。我重新阅读了FlutterFire 上的文档,其中提到如果您拥有当前最新的 Flutter SDK 和 Firebase,那么它将尝试自动显示它。我的错误是除此之外我还实现了FlutterLocalNotifications
,我收到了两个通知,一个是手动的,另一个是自动的。
向您的设备发送消息时,请确保它是notification
而不是data
。文档提到,如果您发送data
,它可能会被忽略为推送通知。仅用于将数据包发送到应用程序。
【讨论】:
有什么方法可以为IOS手动处理notification
类型?所以它没有自动显示?
在这种情况下,您可能希望以data
类型而不是notification
的形式发送通知。您可以选择通过FlutterLocalNotifications
手动显示它
是的,我知道数据不会自动触发。但不幸的是,我无法从服务器端更改它,并希望从 Flutter 端处理它。【参考方案2】:
注释这部分代码可以解决问题。
/// 更新 iOS 前台通知展示选项以允许 /// 抬头通知。 等待
FirebaseMessaging.instance.setForegroundNotificationPresentationOptions( 警报:是的, 徽章:真实, 声音:真实,);
【讨论】:
不适用于我的情况以上是关于Flutter:本地通知插件显示两次通知的主要内容,如果未能解决你的问题,请参考以下文章