【问题标题】:can't detect notification when app closed应用关闭时无法检测到通知
【发布时间】:2018-09-17 16:01:11
【问题描述】:

所以我一直在尝试做这个应用程序,但由于某种原因,当应用程序关闭时我似乎无法检测到来自 firebase 控制台的通知(不是在后台或前台工作正常) .

所以这里有一些相关的文件。

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();

}

【问题讨论】:

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


    【解决方案1】:

    TL/dr 是,根据您手机的品牌,只能检测到通知。

    所以 Android 在这部分通知方面并不是那么好,但它可以在我喜欢的其他事情上进行自定义

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多