【问题标题】:react-native-firebase. getInitialNotification gets null反应原生火力。 getInitialNotification 为空
【发布时间】:2018-07-06 10:10:15
【问题描述】:

当点击托盘中的通知时,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;  
        }
      })

【问题讨论】:

  • 欢迎来到 Stack Overflow!您能否提供更多详细信息以帮助我们回答您的问题?
  • 好的。如果您点击“编辑”链接,您可以编辑您的问题并包含这些详细信息。

标签: react-native react-native-firebase


【解决方案1】:

创建 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>

【讨论】:

【解决方案2】:

您应该在您的应用程序中实现getInitialNotificationnotificationOpenedListener,这取决于应用程序的状态,当您点击通知时会调用其中的一个,但您可以确定只有其中一个会被调用.

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

    }
  });



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

  });

【讨论】:

    猜你喜欢
    • 2016-12-21
    • 2020-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多