收到仅数据的Firebase云消息后,将Android React Native App带到前台 [英] Bring Android React Native App to Foreground On Receipt of Data Only Firebase Cloud Message
问题描述
我成功地将 Firebase Cloud Messaging与react-native-firebase结合使用在Android设备之间仅发送和接收数据消息.
I am successfully using Firebase Cloud Messaging with react-native-firebase to send and receive data only messages between Android devices.
当设备收到消息时,如果该应用程序在后台或已被杀死,我想将该应用程序置于前台.在两种情况下,我都收到将显示console.log()
的通知,但是我在弄清楚如何以最佳方式唤醒"应用程序/将其置于前台方面遇到困难:
When a device receives a message, if the app is in the background or has been killed I would like to bring the app to the foreground. I am getting notifications that will display a console.log()
under both circumstances, but I'm having trouble figuring out how to best approach 'waking up' the app / bring it to the foreground:
firebase.messaging().onMessage((message) => {
// Process your message as required
console.log('You got a message!', message);
});
}
以上代码在两种情况下均按预期执行.
The above code is executed as expected under both circumstances.
我是否可以查看React Native软件包以帮助实现我想做的事情,还是应该考虑写一些Java
?!
Are there React Native packages I can look at to help achieve what I'm trying to do or should I be considering having to write some Java
?!
谢谢.
我正在尝试实现此但不确定是否可以将这样的代码直接添加到我的React Native应用程序中?
I am trying to achieve something like this but not sure if it's possible to add code like this straight in to my React Native application?
我发现了此软件包react-native-invoke-app 使用 HeadlessJS .
HeadlessJS要求您在index.js
中使用此代码:
HeadlessJS requires you to use this code in your index.js
:
AppRegistry.registerHeadlessTask('SomeTaskName', () => require('SomeTaskName'));
但是Firebase Cloud Messaging
已经需要您在同一位置提供以下代码行,我想它的作用与以上相同:
However Firebase Cloud Messaging
already needs you to provide this line of code in the same place, which I'm guessing does the same thing as above:
AppRegistry.registerHeadlessTask('RNFirebaseBackgroundMessage', () => backgroundMessaging);
在react-native-invoke-app
文档之后,然后将调用放置到firebase.messaging().onMessage((message))
回调中,以调用我的应用程序,如下所示:
Following the react-native-invoke-app
docs I am then placing the call to invoke my app inside the firebase.messaging().onMessage((message))
callback like so:
firebase.messaging().onMessage((message) => {
// Process your message as required
invokeApp();
console.log('You got message!', message);
});
}
但是,这并没有使我的应用程序成为前台.
However this is not bringing my app to the foreground.
再次感谢您提供的任何帮助.
Again, any help with this much appreciated.
我现在已经将invokeApp()
调用移到了Firebase backgroundNotificationListener
函数内部,这是一项无头任务,似乎是正确的方法:
I've now moved the invokeApp()
call inside the Firebase backgroundNotificationListener
function which is a headless task which seems like the correct approach:
import invokeApp from 'react-native-invoke-app';
const incomingMessage = async (message) => {
// handle your message
invokeApp();
return Promise.resolve();
}
export default incomingMessage;
但是当调用incomingMessage
函数时,该应用仍不会出现在前台.
But the app will still not come to the foreground when the incomingMessage
function is called.
我在react-native-invoke-app
上提出了此问题github页面,其中包含我所面临问题的更多详细信息.
I have raised this issue on the react-native-invoke-app
github page which has more details on the issues I'm facing.
推荐答案
不起作用的原因是react-native-invoke-app
不支持Android版本>8.如果有人知道该软件包的替代品,请在下方评论,谢谢.
The reason this doesn't work is that react-native-invoke-app
doesn't support Android versions > 8. If anyone knows an alternative to this package please comment below, thank you.
这篇关于收到仅数据的Firebase云消息后,将Android React Native App带到前台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!