应用关闭时无法检测到通知

can't detect notification when app closed

所以我一直在尝试做这个应用程序,但出于某种原因,当应用程序关闭时我似乎无法检测到来自 firebase consol 的通知(不是在后台或前台那些工作很好)。

这里是一些相关文件。

componentDidMount() {
            this.notificationDisplayedListener = firebase.notifications().onNotificationDisplayed((notification: Notification) => {
                    // Process your notification as required
                    // ANDROID: Remote notifications do not contain the channel ID. You will have to specify this manually if you'd like to re-display the notification.
            });
            this.notificationListener = firebase.notifications().onNotification((notification: Notification) => {
                    // Process your notification as required
                    notification.android.setChannelId('test-channel')
                    console.log("I'm called haha", notification);
                    firebase.notifications().displayNotification(notification)
            });
            this.messageListener = firebase.messaging().onMessage((message) => {
                    bgMessaging(message)
            })
    }

    componentWillUnmount() {
            this.notificationDisplayedListener();
            this.notificationListener();
    }

android 清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
              package="com.mobalib">
  <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
  <uses-permission android:name="android.permission.VIBRATE" />
  <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

<application
        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>
        <receiver android:name="io.invertase.firebase.notifications.RNFirebaseBackgroundNotificationActionReceiver"
                              android:exported="true">
              <intent-filter>
                <action android:name="io.invertase.firebase.notifications.BackgroundAction"/>
              </intent-filter>
        </receiver>
      </service>
      <service
              android:name="io.invertase.firebase.messaging.RNFirebaseInstanceIdService"
              android:enabled="true"
              android:exported="true">
        <intent-filter>
              <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
        </intent-filter>
      </service>
      <activity
              android:launchMode="singleTop"
              android:name=".MainActivity"
              android:label="@string/app_name"
              android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
              android:windowSoftInputMode="adjustResize">
        <intent-filter>
              <action android:name="android.intent.action.MAIN" />
              <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
      </activity>
      <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
  </application>
</manifest>

index.js:

AppRegistry.registerHeadlessTask('RNFirebaseBackgroundMessage', () => bgMessaging);

最后 bgMesseging.js:

xport default (message: RemoteMessage) => {
    // handle your message
    const title = message.get
    const notification = new firebase.notifications.Notification()
              .setNotificationId('notificationId')
              .setTitle('My notification title')
              .setBody('My notification body')
              .setData({
                      key1: 'value1',
                      key2: 'value2',
              });
    notification
            .android.setChannelId('test-channel')
    firebase.notifications().displayNotification(notification)
    console.log(message);
    return Promise.resolve();

}

TL/dr是根据你的什么牌子phone只能检测通知。

所以 Android 通知的这一部分不是很好,但它在其他方面的可定制性很强,我很喜欢它