Why our messages are repeating when we navigate from push notifications?
We've used socket_io_client: ^2.0.0-beta.4-nullsafety.0 to capture real time events, and FCM for push notifications.
Real time events seem fine when we're using app normally, but when we enter the app on clicking on push notifications, socket events are getting listened multiple times(thrice mostly), causing our data to duplicate
socket.on('noti:${getIntAsync(USER_ID)}', (data) { print("incomingNotification() : $data"); }
incomingNotification() gets printed in console multiple times.
Do we need to destroy connection when we close the app? If destroyed then opened can we use socket connection again?
When I do disconnect Socket and reconnect it gets initialised but won't connect unless I do a full restart of the app.
/// Initialising socket
socket = io(
API.SOCKET_BASE,
OptionBuilder()
.setTransports(['websocket'])
.enableAutoConnect()
.enableReconnection()
.setReconnectionDelay(500)
.setReconnectionAttempts(10)
.build());
/// On successful socket connection
socket.onConnect((_) {
print('SOCKET: CONNECTED');
if (getBoolAsync(LOGGED_IN)) {
incomingNotification();
}
});
socket.onDisconnect((data) {
print('SOCKET: DISCONNECTED');
print(data);
if (getBoolAsync(LOGGED_IN)) {
tryReconnect(); // if at any point socket disconnects, and user is logged in try reconnecting
}
});
tryReconnect(){
if (socket.connected != null) {
if (socket.connected == false) {
print('SOCKET: MANUAL CONNECT');
socket.connect();
}}
}
Can you attach the code that handles the connection, disconnection and the part that attaches the event listeners. It might be that when you relaunch the app, new event listeners might be getting attached. This follows by new and old event listeners being triggered, hence printing the log statement twice or thrice.
Can you attach the code that handles the connection, disconnection and the part that attaches the event listeners. It might be that when you relaunch the app, new event listeners might be getting attached. This follows by new and old event listeners being triggered, hence printing the log statement twice or thrice.
@Advait1306 I edited my post with the code.
Is this issue still occurring if you close the app completely from the recents menu and then click on a notification? Also, the code in the post doesn't have the part that attaches event listeners.