pushy / pushy-flutter

The official Pushy SDK for Flutter apps.
Apache License 2.0
21 stars 18 forks source link

`setNotificationClickListener` Triggering Automatically on Subsequent Notifications Without User Interaction #72

Closed aungmyopaing890 closed 1 month ago

aungmyopaing890 commented 1 month ago

I am experiencing an issue with the Pushy.setNotificationClickListener method in my Flutter application. The method is supposed to handle routing when a user clicks on a notification. However, the function is being triggered automatically when a new notification is received, without the user actually clicking on the notification.

Steps to Reproduce:

  1. Initial Setup:

    • Add Pushy.listen, Pushy.register, and Pushy.setNotificationListener in the main app’s initState.
    • Add Pushy.setNotificationClickListener in HomeDashboard’s initState.
  2. First Notification:

    • Send the first notification for the details page when the app is killed or quit.
    • Click the notification from the list or banner. The app opens and navigates to the details page as expected.
  3. Second Notification:

    • Send the second notification for the profile page while the app is still open from the first notification.
    • Observe that the app receives the second notification and the setNotificationClickListener function is triggered automatically without clicking the notification.

Expected Behavior: The setNotificationClickListener should only trigger when the user clicks on the notification, not when the notification is received while the app is open.

Actual Behavior: The setNotificationClickListener is being triggered automatically upon receiving a new notification, causing unintended navigation based on the first notification's data. `@pragma('vm:entry-point') Future backgroundNotificationListener(Map<String, dynamic> data) async { Pushy.listen(); Pushy.setNotificationIcon('ic_launcher'); debugPrint("BackgroundNotificationListener"); debugPrint('Received notification: $data'); String notificationTitle = MasterConfig.app_name; String notificationText = data['message'] ?? ''; if (Platform.isAndroid) { debugPrint("Testing: From Notify"); Pushy.notify(notificationTitle, notificationText, data); } Pushy.clearBadge(); }

static Future initPlatformState() async { Pushy.listen(); Pushy.setNotificationIcon('ic_launcher'); try { String deviceToken = await Pushy.register(); if (deviceToken != "Loading...") { await MasterSharedPreferences.instance.setDeviceToken(deviceToken); } } catch (error) { debugPrint('Error: ${error.toString()}'); } Pushy.toggleInAppBanner(false); Pushy.setNotificationListener(backgroundNotificationListener); debugPrint("Testing: End Of init notii Plactform State"); }

Pushy.setNotificationClickListener((Map<String, dynamic> data) { final String barcode = data['barcode'] ?? ''; final String type = data['type'] ?? ''; if (barcode.isNotEmpty) { context.pushNamed( RoutePaths.parcelDetails, extra: ParcelDetailsIntentHolder( filterBy: "barcode", parcelId: barcode, ), ); } else if (type.isNotEmpty) { context.pushNamed( RoutePaths.profileDetails, ); } }); **Additional Context:** The issue occurs when the app is open after navigating from the first notification. Subsequent notifications trigger thesetNotificationClickListener` without user interaction. I suspect this behavior might be a bug in the Pushy Flutter plugin.

Environment:

Conclusion: This issue is affecting the user experience as it leads to unintended navigation within the app. I would appreciate any guidance or fixes for this issue.


This detailed report should help the Pushy Flutter team understand the problem and provide a solution or workaround.

hmG3 commented 1 month ago

I think, in your case, the reason is that you call Pushy.listen() inside the background notification listener, which is wrong. As per the documentation, it should be called only once at the initialization phase. The reason for that is that under the hood, it checks whether the click listener is subscribed and calls it: https://github.com/pushy/pushy-flutter/blob/7432d15e66858a8b453bb268c4c1ab8a9ef8272a/lib/pushy_flutter.dart#L42-L59

However, I have faced a similar false behavior. In my case, the root cause was that after a user logged out, the click listener was still subscribed, and for a new user logged in, push notifications were triggered for an unknown reason. The fix for me was to explicitly unsubscribe for the listeners on sign out:

Pushy.setNotificationClickListener((_) {});
Pushy.setNotificationListener((_) {});
pushy commented 1 month ago

Hi @aungmyopaing890, Thanks for reaching out. We'd be glad to assist with your inquiry.

@hmG3 is correct. We were able to reproduce the issue after adding Pushy.listen() inside the backgroundNotificationListener.

Please remove the invocation of Pushy.listen() from your backgroundNotificationListener. Please only call Pushy.listen() once, from within initPlatformState(), and the problem with the click listener being incorrectly invoked will be resolved.

Please let us know if there's anything else we can help with.