消息传递().onNotificationOpenedApp从不被触发,消息传递().getInitialNotification()被触发,但远程消息始终为空 [英] messaging().onNotificationOpenedApp is never triggered, messaging().getInitialNotification() is triggered but remoteMessage is always null

查看:0
本文介绍了消息传递().onNotificationOpenedApp从不被触发,消息传递().getInitialNotification()被触发,但远程消息始终为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是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);

App.js

// other imports
import messaging from '@react-native-firebase/messaging';
import {store, persistor} from './Store';

export default function App() {
  useEffect(() => {
    (async () => await messaging().registerDeviceForRemoteMessages())();

    const unsubscribe = messaging().onMessage(async remoteMessage => {
      store.dispatch(storeNews(remoteMessage));
    });

    messaging().onNotificationOpenedApp(remoteMessage => {
      // The below code gets never executed
      Alert.alert('here');
      console.log(
        'Notification caused app to open from background state:',
        remoteMessage,
      );
    });

    messaging()
      .getInitialNotification()
      .then(remoteMessage => {
        console.log(remoteMessage); // always prints null
        if (remoteMessage) {
          // Never reached
          Alert.alert('here');
          console.log(
            'Notification caused app to open from quit state:',
            remoteMessage,
          );
        }
      });

    return unsubscribe;
  }, []);

  return (
    <>
      <StatusBar barStyle="dark-content" />
      <Provider store={store}>
        <PersistGate loading={null} persistor={persistor}>
          <RootNavigator />
        </PersistGate>
      </Provider>
    </>
  );
}

Reaction-Native信息:

System:
    OS: Windows 10 10.0.18363
    CPU: (8) x64 Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz
    Memory: 6.90 GB / 15.73 GB
  Binaries:
    Node: 10.15.3 - C:Program Files
odejs
ode.EXE
    Yarn: 1.9.4 - C:UsersuserAppDataRoaming
pmyarn.CMD
    npm: 6.14.1 - C:Program Files
odejs
pm.CMD
  SDKs:
    Android SDK:
    API Levels: 21, 22, 23, 24, 25, 26, 27, 28
    Build Tools: 23.0.1, 25.0.0, 26.0.1, 26.0.2, 26.0.3, 27.0.3, 28.0.2, 28.0.3
    System Images: Google APIs Intel x86 Atom, android-27
Intel x86 Atom
  IDEs:
    Android Studio: Version  3.5.0.0 AI-191.8026.42.35.6010548
  npmPackages:
    react: 16.9.0 => 16.9.0
    react-native: 0.61.5 => 0.61.5

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:Usersuser.androidavdNexus_5X_API_27_x86.avd
Based on: Android API 27 Tag/ABI: google_apis/x86

感谢您的帮助。提前表示感谢。

推荐答案

我找到了问题的原因。我的应用程序中有一个本地Splash Screen的配置,这是使用 react-native-splash-screen包所必需的实现。

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

public class SplashActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        try {
            super.onCreate(savedInstanceState);
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            finish();
       }
        catch(Exception e) {
           System.out.println(e.getMessage());
       }

我在AndroidManifest.xml中指定SplashActivity是我的主要活动:

       <activity
          android:name=".SplashActivity"
          android:theme="@style/SplashTheme"
          android:label="@string/app_name">
          <intent-filter>
              <action android:name="android.intent.action.MAIN" />
              <category android:name="android.intent.category.LAUNCHER" />
          </intent-filter>
        </activity>

      <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
        android:windowSoftInputMode="adjustResize"
        android:exported="true" />

然后更改我的MainActivity.java上的一些逻辑,如下所示:

import android.os.Bundle;
import org.devio.rn.splashscreen.SplashScreen;
public class MainActivity extends ReactActivity {
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        SplashScreen.show(this);
        super.onCreate(savedInstanceState);
    }
}

因此事件未正确传递,有关在此Reaction-Native-Firebase Repoissue上查看此评论的更多信息

我对此问题的解决方案是特定于实现的,但您永远不知道什么可能会有帮助。

我的问题是我有一个闪屏活动,这是实际的启动器活动。要在App.js中触发回调,我必须: 将"singleTop"添加到androidManifest.xml中SplashActivity.Java的活动标记中 在SplashActivity.Java的onCreate方法中使用"this.getIntent();"拦截来自Firebase通知打开事件的意图 使用.getExtras()从截获的意图中获取包,并将其附加到转发到.MainActivity的新意图 一旦.MainActivity能够访问该意图,所有回调就开始触发正确的事件。

所以我在May SplashActivity上添加了这个逻辑:

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            intent.putExtras(extras);
        }

现在看起来像:

public class SplashActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        try {
  //  Block of code to try
        super.onCreate(savedInstanceState);

        Intent intent = new Intent(this, MainActivity.class);

        // Pass along FCM messages/notifications etc.
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            intent.putExtras(extras);
        }
        startActivity(intent);
        finish();
}
catch(Exception e) {
  //  Block of code to handle errors
  System.out.println(e.getMessage());
}
    }
}
现在我可以获取getInitialNotification()返回和onNotificationOpenedApp()事件。 在Android上,如果应用程序处于后台或退出状态总是被称为getInitialNotification(),这可能是因为我的逻辑来自Splash Screen。但对我来说,目前还可以,我会在这两种情况下做完全相同的事情

这篇关于消息传递().onNotificationOpenedApp从不被触发,消息传递().getInitialNotification()被触发,但远程消息始终为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆