0

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. 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.

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

Any help will be appreciated :)

4

2 回答 2

2

注意:如果您使用的是普通推送通知(即 GCM 和 APNS),请改用。由于我需要使用另一个第三方推送服务,我需要自己想办法将 SDK(本机)桥接到 React Native。

因此,在解决这个问题几个小时后,我找到了解决问题的方法。该解决方案分为两部分:

  1. emitter,这将在服务器发送推送时发出一个事件。
  2. listener,这将监听您之前发出的事件。

发射器

这发生在本机端(在我的例子中是 Android)

对于这一部分,我从这个库如何使用 GCM 中学到了它。并在这里找到了关于 RN 文档的教程。

基本上在您收到推送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);

听众

监听器将构建在 RN 端。该文档对我有帮助。我之前错过了这个文档,因为它只出现在 RN iOS 文档中。

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

然后在ComponentWillMount()

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

那么你只需要创建handlePush函数并在那里获取参数

handlePush = (event) => {
    console.log('event triggered..');
    console.log(event);
}
于 2017-03-22T04:56:58.157 回答
0

进行推送通知的最佳方式是深度链接。如果您使用的是 React Navigation,它的操作非常简单。深度链接 React Native 您可以定义唯一的 URL,例如 yorApp://employee/1 并轻松导航到该屏幕。

于 2019-01-02T05:03:33.180 回答