`setNotificationClickListener` Triggering Automatically on Subsequent Notifications Without User Interaction
Closed this issue · 2 comments
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:
-
Initial Setup:
- Add
Pushy.listen
,Pushy.register
, andPushy.setNotificationListener
in the main app’sinitState
. - Add
Pushy.setNotificationClickListener
inHomeDashboard
’sinitState
.
- Add
-
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.
-
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 the
setNotificationClickListener` without user interaction. I suspect this behavior might be a bug in the Pushy Flutter plugin.
Environment:
- Flutter version: 3.19.6
- Pushy Flutter version:^2.0.18
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.
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:
pushy-flutter/lib/pushy_flutter.dart
Lines 42 to 59 in 7432d15
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((_) {});
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.