我使用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个问题中读到了类似的问题,但没有得到答案

推荐答案

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

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

对于本地通知,另一个建议是使用不同的ID来创建本地通知,如下所示:

static int getUniqueNotificationId() {
var randomNumber = Random();
var resultOne = randomNumber.nextInt(2000);
var resultTwo = randomNumber.nextInt(100);
if (resultTwo >= resultOne) resultTwo += 1;
return resultTwo;}

await AwesomeNotifications().createNotification(
    content: NotificationContent(
      id: getUniqueNotificationId(),
      channelKey: 'channel_name',
      title: title,
      body: body,
      notificationLayout: notificationLayout,
      payload: remoteMessage,
      actionType: ActionType.Default,
    ),
  );

Flutter相关问答推荐

如何在Flutter中的textformfield上显示标签文本?

Firebase Auth异常未被catch捕获

Flutter 中不存在URI的目标

如何在Flutter 中播放音频

SupabaseFlutter 集成问题:无法更新行

为什么Flutter 翼扩展卡有上下边框?

Flutter Riverpod,将默认的BTC值改写为我的新状态

如何在不注销的情况下刷新Firebase身份验证用户

flutter Listview构建器溢出

自定义绘画未显示 - Flutter

我无法找到修复此错误的位置,指出无法使用静态访问来访问实例成员

使 Row 中的元素保持在中间的空间,如果它们太大,则将它们包裹起来

更改下拉按钮的背景 colored颜色 - flutter

转换函数上传文件上传Uint8List

当我将主题更改为深色然后返回浅色模式时,Getx 会Flutter 更改主题,它不会加载我的自定义主题

如何格式化网址?

Flutter Bloc 使用 Cubit 监听流和emits 状态

如何使用 Rrect 或类似工具在 flutter 中绘制梯形线?

如何在 Flutter 中使用简写 IF 禁用单选按钮?

既然我们已经有了 Flutter 的内置 setState,为什么还要在 Flutter 中使用 BloC 或 Provider?