Reputation: 1515
I couldn't find any article/blog/docs explaining how to fire an event from native Android/iOS to javascript with Cordova.
How to implement such async communication?
Upvotes: 3
Views: 1339
Reputation: 53301
This is how cordova-plugin-network-information
does it
var cordova = require('cordova');
cordova.fireDocumentEvent('offline');
Then you listen for it like this:
document.addEventListener("offline", yourCallbackFunction, false);
Statusbar plugin does it like this:
cordova.fireWindowEvent('statusTap');
Then you listen for it like this:
window.addEventListener('statusTap', yourCallbackFunction);
In the native part, it's just a like any other plugin callback, for the statusbar plugin it's like this for iOS
CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:payload];
[result setKeepCallbackAsBool:YES];
[self.commandDelegate sendPluginResult:result callbackId:_eventsCallbackId];
The important part is the setKeepCallbackAsBool
set to YES
, that's for allowing to call the callback multiple times and the callback is the one firing the event.
Upvotes: 3