FCM 和 AwesomeNotifications 显示通知两次

问题描述 投票:0回答:1

我使用 Firebase Cloud Messaging 作为我的推送通知服务,并使用 AwesomeNotifications 包作为通知徽章服务。一切正常,但只有一个问题,通知显示两次,一个来自

FCM
,另一个来自
AwesomeNotifications
我已经在用户点击时设置了
AwesomeNotifications
服务逻辑,所以我不需要显示通知
FCM
,我该怎么做?

这里是所有启动函数调用的地方

  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,
);

}

这是前台和后台处理程序

 @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');
  }

我读过类似的问题通知在颤振上显示两次但没有得到答案

flutter dart push-notification notifications firebase-cloud-messaging
1个回答
0
投票

向您的设备发送消息/通知时,请确保它是通知而不是数据。 doc提到,如果您发送数据,它可能会作为推送通知被忽略。仅使用它来向应用程序发送数据包。

此外,假设您的通知是在应用程序处于后台时发送的。在这种情况下,Firebase 会自动发送推送通知,因此您无需在应用程序中手动触发显示通知。

© www.soinside.com 2019 - 2024. All rights reserved.