简体   繁体   English

Android Native 到 React Native 的桥接

[英]Android Native to React Native bridge

I've been searching on how to make a bridge between React Native and Android Native code for a while, but I still don't quite get it.我一直在寻找如何在 React Native 和 Android Native 代码之间架起一座桥梁,但我仍然不太明白。 I've read the documentation here , but I don't quite understand it.我已经阅读了此处的文档,但我不太明白。 What I want to do is, I want to build an apps that utilize push notification, but since I need to push message to China, I can't use GCM (thanks to the great firewall), so I use another third party push SDK.我想做的是,我想构建一个利用推送通知的应用程序,但是由于我需要将消息推送到中国,我不能使用GCM(感谢伟大的防火墙),所以我使用了另一个第三方推送SDK .

I've managed to integrate the push into my apps (resulting a console.log() message whenever I push something), the next step is I want to re-route it to certain page我已经设法将推送集成到我的应用程序中(每当我推送某些内容时都会产生console.log()消息),下一步是我想将其重新路由到某个页面

Any help will be appreciated :)任何帮助将不胜感激 :)

Note: If you are using common push notification (ie GCM and APNS), use this instead.注意:如果您使用的是通用推送通知(即 GCM 和 APNS),请改用 Since I need to use another third party push service, I need to find a way myself to bridge the SDK (which is native) to React Native.由于我需要使用另一个第三方推送服务,我需要自己找到一种方法将 SDK(原生的)桥接到 React Native。

So after several hours tinkering with this problem, I found a solution for my problem.因此,在修补这个问题几个小时后,我找到了解决我的问题的方法。 This solution divided into 2 parts:该解决方案分为两部分:

  1. emitter , this will emits an event whenever the server send a push. emitter ,这将在服务器发送推送时发出一个事件。
  2. listener , this will listen to the event that you emits before. listener ,这将侦听您之前发出的事件。

emitter发射器

This happens on the native side (Android in my case)这发生在本机端(在我的情况下是 Android)

For this part I learnt it from how this library did using GCM.对于这一部分,我从这个库如何使用 GCM 中学到了它。 And found a tutorial here on the RN documentation.并发现了一个教程这里的RN文件上。

Basically after you receive your push on SomeBroadCastReceiver onReceive() function, you can pass the bundle as params in this function基本上在您收到SomeBroadCastReceiver onReceive()函数的推送后,您可以在此函数中将包作为参数传递

reactContext
  .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
  //this eventName is a key so you need to remember it, because you need to call it on the listener
  .emit(eventName, params);

listener听众

The listener will be built on the RN side.侦听器将构建在 RN 端。 this documentation helps me.文档对我有帮助。 I missed this documentation before, because it only appear in the RN iOS docs only.我之前错过了这个文档,因为它只出现在 RN iOS 文档中。

import { NativeEventEmitter, NativeModules } from 'react-native';
//import your already created package name here
const { YourCustomPackageName} = NativeModules;

then in ComponentWillMount()然后在ComponentWillMount()

const yourCustomPackageEmitter = new NativeEventEmitter(YourCustomPackageName);
pushListenerEmitter.addListener(eventName, this.handlePush, this);

then you just need to create handlePush function and get the params there那么你只需要创建handlePush函数并在那里获取参数

handlePush = (event) => {
    console.log('event triggered..');
    console.log(event);
}

The best way to do push notification is Deep Linking.推送通知的最佳方式是深度链接。 If you are using React Navigation its much simple to do.如果您使用的是 React Navigation,则操作起来非常简单。 Deep Linking React Native You can define unique URL like yorApp://employee/1 and navigate easily to that screen.深度链接 React Native您可以定义唯一的 URL,例如 yorApp://employee/1 并轻松导航到该屏幕。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React Native - 桥接 Android Native 到 React Native 性能变慢 - React Native - Bridge Android Native to React Native get Slow Performance 如何在 React Native 中添加 Native Bridge Android 的默认值 - How to add default value of Native Bridge Android in React Native 在com.facebook.react.bridge.NoSuchKeyException中反应本机Android错误 - React Native Android error in com.facebook.react.bridge.NoSuchKeyException 在React Native中将Native Bridge模块导入javascript - Importing a Native Bridge module into javascript in React Native Android反应本机无法获得批量桥接 - Android react native couldn't get batched bridge 反应本机桥接器 - 从 android 视图类发出事件 - React native bridge - emitting events from an android view class React Native Android Bridge 错误:必须在主线程上调用 - React Native Android Bridge Error: Must be called on main thread Android Geofence Transition PendingIntent似乎未运行(反应本机桥) - Android Geofence Transition PendingIntent seems not to run (react-native bridge) 如何修复 React Native android 构建问题“包 com.facebook.react.bridge 不存在” - How to fix React Native android build issue "package com.facebook.react.bridge does not exist" 将 React Native 与 NoClassDefFoundError 集成后,Android 应用程序崩溃:com.facebook.react.bridge.JSPackagerWebSocketClient - Android app crashes after integrating React Native with NoClassDefFoundError: com.facebook.react.bridge.JSPackagerWebSocketClient
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM