使用OneSignal Flutter 收到重复通知

x759pob2  于 2023-05-01  发布在  Flutter
关注(0)|答案(1)|浏览(199)

我在我的Flutter应用程序上集成了onesignal通知,一切都按预期工作,但我收到了重复的通知。具体来说,当应用程序打开时(在前台),我会在应用程序中收到通知并显示一个小部件,但我也会收到与手机通知相同的通知。
下面是在前台接收通知的代码

// this method is being called in main()
// show notification when the app is opened
    OneSignal.shared
        .setNotificationWillShowInForegroundHandler(_localNotificationsHandler);
static _localNotificationsHandler(OSNotificationReceivedEvent event) {
    AppSnackbar.openNotificationsSnackbar(
      title: event.notification.title ?? '',
      msg: event.notification.body ?? '',
    );
  }

这是推送通知的代码

static Future<void> sendNotification({
    required List<String> tokens,
    required String title,
    required String message,
  }) async {
    try {
      var notification = OSCreateNotification(
        playerIds: tokens,
        content: message,
        heading: title,
      );

      await OneSignal.shared.postNotification(notification);
    } catch (e) {
      print(e);
    }
  }
6vl6ewon

6vl6ewon1#

解决方案是在处理应用程序上收到的通知后添加这行代码

event.complete(null);

相关问题