MaikuB / flutter_local_notifications

A Flutter plugin for displaying local notifications on Android, iOS, macOS and Linux
2.46k stars 1.4k forks source link

Endless notification on a single pusher beams event #1618

Closed gtu-myowin closed 2 years ago

gtu-myowin commented 2 years ago

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.

Screen Shot 2022-06-02 at 9 52 28 PM

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.

Screen Shot 2022-06-02 at 9 53 21 PM

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

MaikuB commented 2 years ago

This is something you'd need to debug further within your own app as the plugin as the plugin should only show notification only because when it has been instructed to do so. This means there's either there's an issue within your app you've yet to track down or a compatibility issue with Pusher where it's processing events for notifications created outside of it's library when it shouldn't do so. Note I'm not familiar with Pusher nor do I use it so this would be something you'll need to look further into yourself or get help from others to do so

gtu-myowin commented 2 years ago

How do you know if it is in my code? As I mentioned, the endless notification is only happening when I show the notification. But never mind, I will try with another notification package and let you know.

MaikuB commented 2 years ago

I didn't say there's definitely an issue with your code. I gave what was one of the potential reasons as I won't know what else your app is doing. Commenting out the call to this plugin doesn't mean the issue is to do with this plugin either as I explained with the other potential reason being an issue with pusher itself

gtu-myowin commented 2 years ago

Just FYI, I have tested pusher beams thoroughly before integrating with your package. I have used AlertDialog to alert the notification and it worked without any issue. Also, logging the data would only log once before. I am not blaming your code either. I just wanted to notify you about this and expecting some helps from the community but it seems no one is active here. I can totally understand you.

MaikuB commented 2 years ago

As you're posting on an issues board for this plugin, those that would typically go on here are those who need to do the same thing. Majority of the community is also use to use Firebase Cloud Messaging (FCM) instead of pusher. With that in mind, I'm not surprised with the lack of response. It's not the usual community site so this is why it's better to go to places like Slack, Stack overflow etc.

Note that just because the pusher SDK works fine in isolation doesn't mean there isn't an issue with it either and I had mentioned that it could be the cause. Have you tried to post an issue with pusher though?

MaikuB commented 2 years ago

Closing this as there's no evidence to point to this being a problem with this plugin. As mentioned in previous post, you should also look into reporting an issue with Pusher