我使用Firebase Cloud Messaging作为我的推送通知服务,使用AwesomeNotifications包作为通知徽章服务.一切正常,但只有一个问题,那就是通知显示了两次,一个来自FCM
,另一个来自AwesomeNotifications
我已经在用户点击上设置了我的AwesomeNotifications
服务逻辑,所以我不需要来自FCM
的通知显示,我如何try 它?
这里是所有初始化函数调用的位置
static Future<void> initFcm() async {
try {
fcm = FirebaseMessaging.instance;
// notification settings handler
await _setupFcmNotificationSettings();
// generate token if it not already generated and store it on shared pref
await _generateFcmToken();
// background and foreground handlers
FirebaseMessaging.onMessage.listen(_fcmForegroundHandler);
FirebaseMessaging.onBackgroundMessage(_fcmBackgroundHandler);
} catch (error) {
Logger().e(error);
}
}
这是FCM请求权限
static Future<void> _setupFcmNotificationSettings() async {
//show notification with sound and badge
fcm.setForegroundNotificationPresentationOptions(
alert: true,
sound: true,
badge: true,
);
//NotificationSettings settings
await fcm.requestPermission(
alert: true,
badge: true,
sound: true,
provisional: true,
);
}
这是前台后台处理程序(&A)
@pragma('vm:entry-point')
static Future<void> _fcmBackgroundHandler(RemoteMessage message) async {
devLog(text: 'Message: ${message.toMap()}');
// Notification Component
AwesomeNotificationsHelper.showNotification(
id: 1,
title: message.notification?.title ?? 'Title',
body: message.notification?.body ?? 'Body',
payload: message.data
.cast(), // pass payload to the notification card so you can use it (when user click on notification)
);
}
//handle fcm notification when app is open
static Future<void> _fcmForegroundHandler(RemoteMessage message) async {
// Notification Component
AwesomeNotificationsHelper.showNotification(
id: 1,
title: message.notification?.title ?? 'Title',
body: message.notification?.body ?? 'Body',
payload: message.data
.cast(), // pass payload to the notification card so you can use it (when user click on notification)
);
}
这里是用户点击AwesomeNotifications
的动作
@pragma("vm:entry-point")
static Future<void> onActionReceivedMethod(
ReceivedAction receivedAction) async {
Map<String, String?>? payload = receivedAction.payload;
String? route = payload?['to'];
if (route != null && route.isNotEmpty) {
if (!router.canPop()) {
router.go(HomeScreen.route);
}
router.push(route);
}
devLog(text: 'payload => $payload');
}
我从这Notification show twice on flutter个问题中读到了类似的问题,但没有得到答案