[英]How to pass data from background android service to react-native
I am building an react-native app.我正在构建一个 react-native 应用程序。 I have a background android service that receives a push notification.我有一个接收推送通知的后台 android 服务。 I need to store the notification information(title, description...) on a react-native database or in asyncstorage... because in my app I have a page that shows all the notifications.我需要将通知信息(标题、描述...)存储在 react-native 数据库或 asyncstorage 中...因为在我的应用程序中,我有一个显示所有通知的页面。 I need that If my app is on background and i receive a notification, when the user open the app the page shows this information.我需要如果我的应用程序在后台并且我收到通知,当用户打开应用程序时,页面会显示此信息。 How I can do this?我怎么能做到这一点?
I try to store the notification on the same database that react-native uses but I can't store the data on the android service.我尝试将通知存储在 react-native 使用的同一个数据库上,但我无法将数据存储在 android 服务上。 Please, help me.请帮我。
Did you try exploring these.你有没有尝试探索这些。
componentWillMount() {
Linking.addEventListener("url", this.handleOpenURL);
OneSignal.addEventListener("ids", this.onIds);
OneSignal.addEventListener("received", this.onReceived);
OneSignal.addEventListener("opened", this.onOpened);
}
And in oneReceived event listener you can store data in async storage received from push notification.在 oneReceived 事件侦听器中,您可以将数据存储在从推送通知接收的异步存储中。
onReceived(data) {
this.props.setData(data);
}
In set data you can have your logics to store data.Well i must say push notifications are not used to fetch data and to store and then to render.These provide just little information to open specific screen .Like i received a notification related to deals.Then pressing upon will automatically opens deals screen through linking not the home page of app.For your case you can send the request on that specific page to fetch data.在设置数据中,您可以拥有存储数据的逻辑。我必须说推送通知不用于获取数据和存储然后渲染。这些只提供很少的信息来打开特定屏幕。就像我收到了与交易相关的通知.然后按下将通过链接而不是应用程序的主页自动打开交易屏幕。对于您的情况,您可以在该特定页面上发送请求以获取数据。
Yes, but when you close the app and after this, someone sends one push notification, the event received and opened etc don't do nothing.是的,但是当您关闭应用程序并在此之后,有人发送一个推送通知,接收和打开的事件等什么都不做。 I tried to make a custom native module that receives this info and try to send an Event.我尝试制作一个接收此信息的自定义本机模块并尝试发送一个事件。 I followed this guide:我遵循了本指南:
https://facebook.github.io/react-native/docs/native-modules-android#sending-events-to-javascript https://facebook.github.io/react-native/docs/native-modules-android#sending-events-to-javascript
But it don't works, always returns "sendEvent called before bundle loaded" java.lang.NullPointerException.但它不起作用,总是返回“加载包之前调用的sendEvent”java.lang.NullPointerException。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.