I have a custom Java plugin attempting to communicate back to the Angular frontend, but it can not find the listener. I followed the official docs.
In my Java plugin, when the phone is plugged in and the Intent.ACTION_POWER_CONNECTED
broadcast is emitted by the Android device, I call the CommunicateChargingStatus
Java function to emit an event:
@CapacitorPlugin(name="PowerCommunication")
public class PowerCommunication extends Plugin {
// Other functions
public void CommunicateChargingStatus() {
Log.w("POWER-COMMUNICATING", "Should be emitting a signal about charging status");
JSObject ret = new JSObject();
ret.put("value", "some value");
notifyListeners("myPluginEvent", ret);
}
}
On the Angular side, I get the Java plugin here:
interface PowerCommunicationInterface extends Plugin {}
export const PowerCommunicationPlugin = registerPlugin<PowerCommunicationInterface>("PowerCommunication");
And in my main app I have my the corresponding listener set up as such:
@Component({
selector: 'app-root',
templateUrl: 'app.component.html',
styleUrls: ['app.component.scss']
})
export class AppComponent {
constructor(
// Fields
) {
PowerCommunicationPlugin.addListener('myPluginEvent', (info: any) => {
console.log(`Plugin Event: ${info}`)
})
}
// Other logic...
}
However, when I plugin / unplug the power cable, I am getting the following log messages in Android Studio:
2024-05-24 11:18:05.500 6346-6346 POWER-COMMUNICATING com.project.name W Should be emitting a signal about charging status
2024-05-24 11:18:05.500 6346-6346 Capacitor/...munication com.project.name V Notifying listeners for event myPluginEvent
2024-05-24 11:18:05.500 6346-6346 Capacitor/...munication com.project.name D No listeners found for event myPluginEvent
Any idea why my Java code is unable to find the corresponding listener in Angular?
──────────────────────────────────────────────────────
Ionic:
Ionic CLI : 7.1.1 (C:\Users\Username\AppData\Roaming\npm\node_modules\@ionic\cli)
Ionic Framework : @ionic/angular 7.6.0
@angular-devkit/build-angular : 16.2.10
@angular-devkit/schematics : 16.2.10
@angular/cli : 16.2.10
@ionic/angular-toolkit : 10.0.0
Capacitor:
Capacitor CLI : 5.5.1
@capacitor/android : 5.5.1
@capacitor/core : 5.5.1
@capacitor/ios : not installed
Cordova:
Cordova CLI : not installed
Cordova Platforms : not available
Cordova Plugins : not available
Utility:
cordova-res : not installed globally
native-run : 1.7.4
System:
NodeJS : v20.9.0 (C:\Program Files\nodejs\node.exe)
npm : 10.1.0
OS : Windows 10