react-native-firebase。 getInitialNotification获取空

时间:2018-07-06 10:10:15

标签: react-native react-native-firebase

当点击托盘中的提名时,会触发getInitialNotification,但notificationOpen参数为空值。

我正在通过Firebase控制台发送通知。如果我的应用程序位于前台,则我将收到发送的通知数据。但是,如果我的应用程序在后台运行或被杀死,我会收到通知,但是当我点击通知时,openNotification的值为null。

这就是我在做什么。

firebase.notifications().getInitialNotification()
      .then((notificationOpen: NotificationOpen) => {
        console.log('Notification closed')
        console.log(notificationOpen)
        if (notificationOpen) {
          // App was opened by a notification
          // Get the action triggered by the notification being opened
          const action = notificationOpen.action;
          // Get information about the notification that was opened
          const notification: Notification = notificationOpen.notification;  
        }
      })

2 个答案:

答案 0 :(得分:0)

您应该在应用程序中实现getInitialNotificationnotificationOpenedListener,具体取决于应用程序的状态,当您点击通知时会调用其中一个,但是您可以确定只有一个他们将被呼叫。

firebase
  .notifications()
  .getInitialNotification()
  .then(notificationOpen => {
    if (notificationOpen) {

    }
  });



this.notificationOpenedListener = firebase
  .notifications()
  .onNotificationOpened(() => {

  });

答案 1 :(得分:0)

创建 SplashActivity.java 及以下代码

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class SplashActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = new Intent(this, MainActivity.class);

        // this line is necessary to open notification when app is closed
        intent.putExtras(this.getIntent());
        startActivity(intent);
        finish();
    }
}

将启动活动添加到 AndroidManifest.xml

...
<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>
...

将启动资源添加到 Styles.xml

...
<style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
       <item name="android:windowBackground">@drawable/background_splash</item>
       <item name="android:statusBarColor">@color/darkblue</item>
</style>
...

创建 background_splash.xml 并创建在应用启动时显示的资源

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:drawable="@color/blue"/>

    <item android:gravity="center">
        <bitmap
            android:tileMode="disabled"
            android:src="@drawable/ic_launcher"
            android:gravity="center" />
    </item>

</layer-list>