Search code examples
flutterdartpusherflutter-local-notification

Flutter - Endless notification on a single pusher beams event


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.


Solution

  • 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,
          ),
        );
      }```