You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by GitBox <gi...@apache.org> on 2022/01/26 11:57:57 UTC

[GitHub] [cordova] vipulshah2010 opened a new issue #317: Sending events from native to JS

vipulshah2010 opened a new issue #317:
URL: https://github.com/apache/cordova/issues/317


   ### Issue Type
   <!-- Please check the boxes by putting an x in the [ ] like so: [x] -->
   
   - [ ] Bug Report
   - [ ] Feature Request
   - [x] Support Question
   
   ## Description
   
   ## Information
   Hello, I am looking into a solution for sending events from the native kotlin/java plugin side to javascript. I came across the following method which does that.
   
   `cordovaWebView#sendJavascript`
   
   But this method is now deprecated and documentation suggests that I should use `savedCallbackContext.sendPluginResult(dataResult);` instead.  Do you have an example of it? Also can you please elaborate on where this `savedCallbackContext` is available?. Is this referring to same `CallbackContext` that is available under the `execute` method?
   
   ### Environment, Platform, Device
   Android
   
   ### Version information
   Cordova: Cordova Platforms
   Operating System, Android Studio
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org


[GitHub] [cordova] breautek commented on issue #317: Sending events from native to JS

Posted by GitBox <gi...@apache.org>.
breautek commented on issue #317:
URL: https://github.com/apache/cordova/issues/317#issuecomment-1023404536


   How this is generally done is the JS will register a listener (by invoking a call to native) which will generate a `CallbackContext` for that native call. You can mark that context to be kept alive and store the reference to that `CallbackContext` so that you can continously fire events back to the webview using the same `CallbackContext` instance.
   
   The [cordova-plugin-device-motion](https://github.com/apache/cordova-plugin-device-motion) is a fairly simple plugin that utilises this strategy.
   
   You can see [here](https://github.com/apache/cordova-plugin-device-motion/blob/master/src/android/AccelListener.java#L103) on the "start" call, the plugin stores a reference of the `CallbackContext`, which is marked as kept [here](https://github.com/apache/cordova-plugin-device-motion/blob/master/src/android/AccelListener.java#L120) via `setKeepCallback(true)`. Finally, when the sensor fires a native update, that `CallbackContext` is reused to send data to the webview [here](https://github.com/apache/cordova-plugin-device-motion/blob/master/src/android/AccelListener.java#L293).
   
   Note that the application doesn't need to explicitly hook up this callback context. The plugin itself could listen to the `deviceready` event and make a native call implicitly to hook up the callback context, if that makes more sense for your particular plugin.
   
   We use github issues for tracking issues, and because this doesn't sound like a bug I'll be closing this ticket. I hope the above explains well enough on how to reuse the callback context. If you have further questions, I'd recommend reaching out to our [Slack Community](https://join.slack.com/t/cordova/shared_invite/zt-z70vy6tx-7VNulesO0Qz0Od9QV4tc1Q).


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org


[GitHub] [cordova] breautek closed issue #317: Sending events from native to JS

Posted by GitBox <gi...@apache.org>.
breautek closed issue #317:
URL: https://github.com/apache/cordova/issues/317


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@cordova.apache.org
For additional commands, e-mail: commits-help@cordova.apache.org