【问题标题】:onNotificationOpened and getInitialNotification not getting triggered react-native-firebaseonNotificationOpened 和 getInitialNotification 没有被触发 react-native-firebase
【发布时间】:2019-01-20 23:11:11
【问题描述】:

我在我的 react native 项目[android 平台] 中使用了 react-native-firebase 来显示应用通知。有了这个库,我可以在应用程序处于前台/后台/关闭状态时显示通知。

根据 react native firebase 文档,onNotificationOpened 应在通知点击打开应用时触发。但是,在我的情况下,这不会发生,永远不会调用 onNotificationOpened 方法,并且 getInitialNotification 方法总是会获得空值。

我也在使用 react-native-splash-screen 库来显示启动画面。

这是我的代码来自 App.js >> componentDidMount()

 firebase.messaging().requestPermission()
            .then(() => {

                const myAppChannel = new firebase.notifications.Android.Channel('my-channel',
                    'my Channel', firebase.notifications.Android.Importance.Max)
                    .setDescription('my channel');
                firebase.notifications().android.createChannel(myAppChannel);


                this.messageListener = firebase.messaging().onMessage((message) => {

                    const {title, body} = message.data;
                    const notificationDisplay = new firebase.notifications.Notification()
                        .setNotificationId('notificationId')
                        .setTitle(title)
                        .setBody(body)
                        .setData({
                            key1: 'value1',
                            key2: 'value2',
                        }).setSubtitle('notification')
                        .setSound('default');
                    notificationDisplay
                        .android.setChannelId('my-channel')
                        .android.setSmallIcon('ic_launcher')
                        .android.setAutoCancel(true).android.setPriority(firebase.notifications.Android.Priority.High);
                        firebase.notifications().displayNotification(notificationDisplay);
                });


                this.notificationOpenedListener = firebase.notifications().onNotificationOpened((notificationOpen: NotificationOpen) => {
                    //never gets called
                    console.log('onNotificationOpened was triggered on notification taped');

                });

            })
            .catch(error => {
                // User has rejected permissions
                console.log("user rejected");
                console.log(error);
            });

AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.VIBRATE" />

    <application
        android:name=".MainApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">

        <service android:name="io.invertase.firebase.messaging.RNFirebaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>
        <service android:name="io.invertase.firebase.messaging.RNFirebaseInstanceIdService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
            </intent-filter>
        </service>
        <service android:name="io.invertase.firebase.messaging.RNFirebaseBackgroundMessagingService" />

        <meta-data
            android:name="com.google.firebase.messaging.default_notification_channel_id"
            android:value="@string/default_notification_channel_id" />

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

        <activity
            android:name=".MainActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
            android:exported="true"
            android:label="@string/app_name"
            android:launchMode="singleTop"
            android:windowSoftInputMode="adjustResize" />
        <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
    </application>

更新:问题是由 react-native-splash-screen 库引起的,我在删除 react-native-splash 后能够解决这个问题-screen 库 来自 android 项目。但是,我仍然不确定如何在使用 react-native-splash-screen 时完成这项工作。

【问题讨论】:

    标签: react-native react-native-android react-native-firebase


    【解决方案1】:

    intent-filter 添加到MainActivityclick-action 到通知JSON 对我有用。

        <intent-filter>
            <action android:name=".MainActivity" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    

    通知 JSON

    {
    "to":"ID",
     "notification" : {
          **"click_action" : ".MainActivity",** 
          "body": "Message Body",
          "title" : "Call Status",
          "sound": "default",
          "badge":"5"
       },
      "data": {
          "type": 1,
          "body": "Message Body",
          "title" : "Call Status"
      }
    }
    

    我的 manifest.xml 看起来像这样

     <application
          tools:replace="android:allowBackup"
          android:name=".MainApplication"
          android:label="@string/app_name"
          android:icon="@mipmap/ic_launcher"
          android:allowBackup="false"
          android:theme="@style/AppTheme">
            <service android:name="io.invertase.firebase.messaging.RNFirebaseMessagingService">
                <intent-filter>
                    <action android:name="com.google.firebase.MESSAGING_EVENT" />
                </intent-filter>
            </service>
            <service android:name="io.invertase.firebase.messaging.RNFirebaseInstanceIdService">
                <intent-filter>
                    <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
                </intent-filter>
            </service>
            <activity
                android:name=".SplashActivity"
                android:theme="@style/SplashTheme"
                android:launchMode="singleTop"
                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:screenOrientation="portrait"
                android:launchMode="singleTop"
                android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
                android:windowSoftInputMode="adjustPan">
                <intent-filter>
                    <action android:name=".MainActivity" />
                    <category android:name="android.intent.category.DEFAULT" />
                </intent-filter>
            </activity>
            <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
    </application>
    

    希望对你有帮助

    【讨论】:

    • 感谢您的回复。我从我的项目中删除了 react-native-splash-screen 并选择了其他一些方法来显示启动画面。但我会试一试。我想ios应该也有同样的问题,请告诉我你是如何解决ios的
    • 你好,你能检查一下这个问题吗@Hariks stackoverflow.com/questions/57171787/…
    • @SelvesanMalakar 你能推荐一个替代 react-native-splash-screen 的方法吗?
    猜你喜欢
    • 1970-01-01
    • 2019-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-25
    相关资源
    最近更新 更多