颤音 - 单个推动器梁事件中无数通知

发布于 2025-02-04 07:21:31 字数 2855 浏览 3 评论 0原文

描述错误

我正在使用Pusher Beam从服务器启动事件,并且我使用Flutter Local Notification在App收到事件时显示该通知。

示例代码要重现问题

我在我的init状态中称为initpusherbeams()(请阅读到最后,我很确定此问题是否存在flutter本地通知)

  @override
  void initState() {
    super.initState();

    _setAuthData().then((_) {
      if (_user?.id != null) initPusherBeams();
    });

    // notification related
    _notiInit();
    _requestPermissions();
    _configureDidReceiveLocalNotificationSubject();
    _configureSelectNotificationSubject();

    // ask for app rating
    WidgetsBinding.instance.addPostFrameCallback((_) => _ratingDialog());
  }

,然后,在initpusherbeams函数中,

  initPusherBeams() async {
    // Let's see our current interests
    await PusherBeams.instance.setDeviceInterests([
      // 'App.Models.User.${_user!.id}',
      'debug-new'
    ]);

    // This is not intented to use in web
    if (!kIsWeb) {
      await PusherBeams.instance.onMessageReceivedInTheForeground(_onMessageReceivedInTheForeground);
    }
  }

  void _onMessageReceivedInTheForeground(Map<Object?, Object?> data) {
    AndroidNotificationDetails androidPlatformChannelSpecifics = const AndroidNotificationDetails(
      'channel',
      'My App Name',
      channelDescription: 'New user registered',
      playSound: false,
      styleInformation: DefaultStyleInformation(true, true),
    );

    const IOSNotificationDetails iOSPlatformChannelSpecifics = IOSNotificationDetails(presentSound: false);

    NotificationDetails platformChannelSpecifics = NotificationDetails(
      android: androidPlatformChannelSpecifics,
      iOS: iOSPlatformChannelSpecifics,
    );

    log(json.encode(data));

    // flutterLocalNotificationsPlugin.show(
    //   0,
    //   'New user registered',
    //   data['body'].toString(),
    //   platformChannelSpecifics,
    //   payload: data['title'].toString(),
    // );
  }

如果我评论flutterlocalnotificationplugin.show,则仅在下面的ScreenShot中看到的事件射击一次。

但是,如果我不输入显示通知部分,这是以下代码,

 flutterLocalNotificationsPlugin.show(
      0,
      'New user registered',
      data['body'].toString(),
      platformChannelSpecifics,
      payload: data['title'].toString(),
    );

事件无休止地射击(如下面的屏幕截图中),并且通知不断出现。

如何出现通知变成某种循环,以及我应该如何解决此问题。提前致谢。

Describe the bug

I am using pusher beams to fire event from server and I use flutter local notification to show the notification when the event is received by app.

Sample code to reproduce the problem

I have called initPusherBeams() in my init state (please read to the end I am quite sure this issues is with flutter local notifications)

  @override
  void initState() {
    super.initState();

    _setAuthData().then((_) {
      if (_user?.id != null) initPusherBeams();
    });

    // notification related
    _notiInit();
    _requestPermissions();
    _configureDidReceiveLocalNotificationSubject();
    _configureSelectNotificationSubject();

    // ask for app rating
    WidgetsBinding.instance.addPostFrameCallback((_) => _ratingDialog());
  }

and then, ininitPusherBeams function, I have

  initPusherBeams() async {
    // Let's see our current interests
    await PusherBeams.instance.setDeviceInterests([
      // 'App.Models.User.${_user!.id}',
      'debug-new'
    ]);

    // This is not intented to use in web
    if (!kIsWeb) {
      await PusherBeams.instance.onMessageReceivedInTheForeground(_onMessageReceivedInTheForeground);
    }
  }

  void _onMessageReceivedInTheForeground(Map<Object?, Object?> data) {
    AndroidNotificationDetails androidPlatformChannelSpecifics = const AndroidNotificationDetails(
      'channel',
      'My App Name',
      channelDescription: 'New user registered',
      playSound: false,
      styleInformation: DefaultStyleInformation(true, true),
    );

    const IOSNotificationDetails iOSPlatformChannelSpecifics = IOSNotificationDetails(presentSound: false);

    NotificationDetails platformChannelSpecifics = NotificationDetails(
      android: androidPlatformChannelSpecifics,
      iOS: iOSPlatformChannelSpecifics,
    );

    log(json.encode(data));

    // flutterLocalNotificationsPlugin.show(
    //   0,
    //   'New user registered',
    //   data['body'].toString(),
    //   platformChannelSpecifics,
    //   payload: data['title'].toString(),
    // );
  }

If I comment out flutterLocalNotificationsPlugin.show, the event fire only once as you can see in below screenshot.

Screenshot without error

but if I uncomment showing notification part which is the following code

 flutterLocalNotificationsPlugin.show(
      0,
      'New user registered',
      data['body'].toString(),
      platformChannelSpecifics,
      payload: data['title'].toString(),
    );

The event fire endlessly (like in the screenshot below) and the notification keep appearing for each event continuously.

Screenshot with error

How come showing notification became some kind of loop and how should I fix this. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

辞慾 2025-02-11 07:21:31

当应用在前景时,我最终显示了Snackbar的通知。当应用在后台时,推动器梁包装处理通知并将其发送到通知中心。这是我的代码

  initPusherBeams() async {
    // Let's see our current interests
    await PusherBeams.instance.setDeviceInterests([
      'App.Models.User.${_user!.id}',
      'debug-new'
    ]);

    // This is not intented to use in web
    if (!kIsWeb) {
      await PusherBeams.instance.onMessageReceivedInTheForeground(_onMessageReceivedInTheForeground);
    }
  }

  void _onMessageReceivedInTheForeground(Map<Object?, Object?> data) {
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(
        content: Text(correctFont(data['body'] as String)),
        duration: const Duration(milliseconds: 3000),
        backgroundColor: brandBrown,
      ),
    );
  }```

I end up showing the notification with SnackBar while the app is in foreground. While the apps is in background, the pusher beams package handle the notification and send it to notification center. Here's my code

  initPusherBeams() async {
    // Let's see our current interests
    await PusherBeams.instance.setDeviceInterests([
      'App.Models.User.${_user!.id}',
      'debug-new'
    ]);

    // This is not intented to use in web
    if (!kIsWeb) {
      await PusherBeams.instance.onMessageReceivedInTheForeground(_onMessageReceivedInTheForeground);
    }
  }

  void _onMessageReceivedInTheForeground(Map<Object?, Object?> data) {
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(
        content: Text(correctFont(data['body'] as String)),
        duration: const Duration(milliseconds: 3000),
        backgroundColor: brandBrown,
      ),
    );
  }```
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文