Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android messaging().onNotificationOpenedApp从未触发,messaging().getInitialNotification()已触发,但remoteMessage始终为空_Android_React Native_Background_Messaging_React Native Firebase - Fatal编程技术网

Android messaging().onNotificationOpenedApp从未触发,messaging().getInitialNotification()已触发,但remoteMessage始终为空

Android messaging().onNotificationOpenedApp从未触发,messaging().getInitialNotification()已触发,但remoteMessage始终为空,android,react-native,background,messaging,react-native-firebase,Android,React Native,Background,Messaging,React Native Firebase,我使用的是react native firebase v6.4.0。我成功地向setBackgroundMessageHandler注册了一个后台处理程序,一切正常。现在,我正在尝试处理应用程序处于后台时的通知点击/退出,并且我正在使用onNotificationOpenedApp/getInitialNotification。我面临的问题是,第一个方法从未被触发,而第二个方法被触发,但是remoteMessage参数总是null。我还尝试生成一个发布版本,但结果是一样的 index.js //

我使用的是react native firebase v6.4.0。我成功地向
setBackgroundMessageHandler
注册了一个后台处理程序,一切正常。现在,我正在尝试处理应用程序处于后台时的通知点击/退出,并且我正在使用
onNotificationOpenedApp
/
getInitialNotification
。我面临的问题是,第一个方法从未被触发,而第二个方法被触发,但是
remoteMessage
参数总是
null
。我还尝试生成一个发布版本,但结果是一样的

index.js

// imports

messaging().setBackgroundMessageHandler(async remoteMessage => {
  // storing the message with redux
});

function HeadlessCheck({isHeadless}) {
  return isHeadless ? null : <App />;
}

AppRegistry.registerComponent(appName, () => HeadlessCheck);

package.json:

{
  ...
  "@react-native-firebase/app": "^6.4.0",
  "@react-native-firebase/messaging": "^6.4.0",
  ...
}
我尝试在物理Android设备和Android模拟器上运行代码,得到了相同的行为

物理设备规格:

Model code: SM-G975F 
Android version: 10
模拟器规格:

Name: Nexus_5X_API_27_x86
Device: Nexus 5X (Google)
Path: C:\Users\user\.android\avd\Nexus_5X_API_27_x86.avd
Based on: Android API 27 Tag/ABI: google_apis/x86


谢谢你的帮助。非常感谢。

我找到了问题的原因。我的应用程序中有一个本机启动屏幕的配置,这是使用该软件包所必需的实现

我添加了一个名为
SplashActivity.java
的新闻文件:

公共类SplashActivity扩展了AppCompatActivity{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
试一试{
super.onCreate(savedInstanceState);
意向意向=新意向(此,MainActivity.class);
星触觉(意向);
完成();
}
捕获(例外e){
System.out.println(e.getMessage());
}
我在
AndroidManifest.xml
中命令SplashActivity作为我的主要活动:

  • 关于这个问题的更多解释
我对这个问题的解决方案是相当具体的实现,但您永远不知道什么可能有用

我的问题是我有一个splash活动,这是实际的启动程序活动。要在App.js中启动回调,我必须: 将“singleTop”添加到AndroidManifest.xml中SplashActivity.java的活动标记中 在SplashActivity.java的onCreate方法中使用“this.getIntent();”从firebase通知打开事件截取意图 使用.getExtras()从截获的意图中获取捆绑包,并将其附加到转发到.MainActivity的新意图 一旦.MainActivity访问了intent,所有回调就开始触发正确的事件

  • 实施以解决问题
因此,我在活动中添加了以下逻辑:

Bundle extras=getIntent().getExtras();
如果(附加值!=null){
意图.临时演员(临时演员);
}
现在看来:

公共类SplashActivity扩展了AppCompatActivity{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
试一试{
//要尝试的代码块
super.onCreate(savedInstanceState);
意向意向=新意向(此,MainActivity.class);
//传递FCM消息/通知等。
Bundle extras=getIntent().getExtras();
如果(附加值!=null){
意图.临时演员(临时演员);
}
星触觉(意向);
完成();
}
捕获(例外e){
//处理错误的代码块
System.out.println(e.getMessage());
}
}
}
现在我可以获取
getInitialNotification()
返回和
onNotificationOpenAdapp()
事件。
在Android上,如果应用程序处于后台或退出状态,则始终称为getInitialNotification(),这可能是因为我的逻辑来自于SplashScreen。但对我来说,现在还可以,我会在这两个方面做完全相同的事情

我有同样的问题,你的解决方案非常出色。谢谢你,非常出色,但太复杂了。任何简单的解决方案?@kasra不需要单独的活动,你可以直接使用SplashScreen。show(这个)在MainActivity中,您将收到这两个事件的getInitialNotification()
Model code: SM-G975F 
Android version: 10
Name: Nexus_5X_API_27_x86
Device: Nexus 5X (Google)
Path: C:\Users\user\.android\avd\Nexus_5X_API_27_x86.avd
Based on: Android API 27 Tag/ABI: google_apis/x86