How to use `notifyListeners` from a BroadcastReceiver Class? #5188


I understand that notifyListeners is only available in the Plugin Class eg

@CapacitorPlugin(name = “CapacitorTimerNotification”)
class CapacitorTimerNotification : Plugin() {
fun showTimerNotification(call: PluginCall) {
notifyListeners(“onPauseTimer”, JSObject())
How can I use notifyListeners from a BroadcastReceiver Class? eg

class TimerNotificationReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val action: String? = intent.action
if (action == “pause”) {
notifyListeners(“onPauseTimer”, JSObject())

You can’t, only plugins can notify listeners, because the listeners are part of the plugin, not of the BroadcastReceiver.

What you can do is to have some way of communicating the BroadcastReceiver with the plugin class, check network plugin in example.

Thanks @jcesarmobile

I described my solution here How to use `notifyListeners` from a BroadcastReceiver Class? · Discussion #5188 · ionic-team/capacitor · GitHub