Closed aungmyopaing890 closed 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((_) {});
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.
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:
Pushy.listen
,Pushy.register
, andPushy.setNotificationListener
in the main app’sinitState
.Pushy.setNotificationClickListener
inHomeDashboard
’sinitState
.First Notification:
Second Notification:
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 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();
}
setNotificationClickListener
is being triggered automatically upon receiving a new notification, causing unintended navigation based on the first notification's data. `@pragma('vm:entry-point') Futurestatic 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:
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.