-
Notifications
You must be signed in to change notification settings - Fork 609
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
On web plugins, adListener
only the last listener is added
#1699
Comments
This issue needs more information before it can be addressed. Please see the Contributing Guide for how to create a Sample App. Thanks! |
please find the demo repo at https://github.com/miqmago/capacitor-plugins-issue-1699.
Then in import { SplashScreen } from '@capacitor/splash-screen';
import { Camera } from '@capacitor/camera';
+import { LocalNotifications } from '@capacitor/local-notifications';
+
+const IS_WORKING_CASE = false;
window.customElements.define(
'capacitor-welcome',
@@ -89,7 +92,7 @@ window.customElements.define(
`;
}
- connectedCallback() {
+ async connectedCallback() {
const self = this;
self.shadowRoot.querySelector('#take-photo').addEventListener('click', async function (e) {
@@ -108,6 +111,35 @@ window.customElements.define(
console.warn('User cancelled', e);
}
});
+
+
+ LocalNotifications.addListener('localNotificationActionPerformed', async () => {
+ console.log('localNotificationActionPerformed');
+ });
+
+ if (IS_WORKING_CASE) {
+ await new Promise(resolve => setTimeout(resolve, 1000));
+ }
+
+ LocalNotifications.addListener('localNotificationReceived', () => {
+ console.log('localNotificationReceived');
+ });
+
+ if (IS_WORKING_CASE) {
+ await new Promise(resolve => setTimeout(resolve, 1000));
+ }
+
+ LocalNotifications.schedule({
+ notifications: [{
+ title: 'Hey',
+ body: 'Test notification',
+ id: new Date().getTime(),
+ schedule: {
+ at: new Date(new Date().getTime() + 1000),
+ },
+ actionTypeId: 'received',
+ }],
+ });
}
}
); In this line there is a variable to make it work or to make it fail. When Tested in Chrome 116.0.5845.96 (Compilació oficial) (arm64). |
Thanks for the issue, I've verified it and created a new issue in capacitor repository since I think the problem is in As workaround, you can use the plugins with |
Hi @jcesarmobile thanks for reporting on the right repo!! Just to clarify, I'm not sure to understand how to use Do you mean to place await on export class AppRoot {
async componentWillLoad() {
await someMethod();
LocalNotifications.addListener('localNotificationActionPerformed', (action) => {
console.log('localNotificationActionPerformed', action);
});
await maybeWeirdAwaitCouldAppearHere();
LocalNotifications.addListener('localNotificationReceived', (notif) => {
console.log('localNotificationReceived', notif);
});
}
} |
putting an await before every plugin call
|
Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of the plugin, please create a new issue and ensure the template is fully filled out. |
Bug Report
When creating a capacitor plugin, some listeners don't fire. Multiple instances of the plugin are created each time
addListener
is called. The problem I think is with this pattern published in https://capacitorjs.com/docs/plugins/tutorial/web and also used in many plugins:In plugins that are first called on
async componentWillLoad
, it might create a new implementation of the plugin every timeregisterPlugin
is called. I'm not sure whyregisterPlugin
is called many times, but the fact is that it is. It might be by theasync
but I'm not sure.The problem is that when attaching listeners, the listener will be added to any new instance of the plugin and other instances does not have the listener.
So for example, for LocalNotifications, if you have something like this when the app first starts (have tried without the
maybeWeirdAwaitCouldAppearHere
and behaviour is the same, but it could also happen):LocalNotifications
will only havelocalNotificationReceived
event attached as it is the last one. No calls tolocalNotificationActionPerformed
will be received, as the second instance was a new instance without the first listener attached.Possible solution
The solution I've found on the plugins I've developed is to change little bit the implementation of registration:
I've also tried with
LocalNotifications
and it works fine. If this is the solution, it should also be posted on documentation: https://capacitorjs.com/docs/plugins/tutorial/webAlso all plugins using this
registerPlugin
pattern should be fixed.Plugin(s)
So far I've seen this behaviour in
@capacitor/local-notifications
and in some plugins that I've designed myself.I suspect that it affects to any plugin developed with web
registerPlugin
mentioned pattern.Capacitor Version
Platform(s)
Web
The text was updated successfully, but these errors were encountered: