Реагирует-нативный-firebase. getInitialNotification получает значение NULL
При нажатии на уведомление из трея срабатывает getInitialNotification, но параметр NotificationOpen получает нулевое значение.
Я отправляю уведомление через консоль Firebase. Если мое приложение находится на переднем плане, я получаю данные уведомления, которые я отправляю. Но если мое приложение находится в фоновом режиме или приложение убито, я получаю уведомление, но когда я нажимаю на уведомление, значение openNotification равно нулю.
Это то, что я делаю.
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;
}
})
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>
Вы должны реализовать getInitialNotification
а также notificationOpenedListener
в вашем приложении, в зависимости от состояния приложения, любой из них вызывается при нажатии на уведомление, но вы можете быть уверены, что будет вызван только 1 из них.
firebase
.notifications()
.getInitialNotification()
.then(notificationOpen => {
if (notificationOpen) {
}
});
this.notificationOpenedListener = firebase
.notifications()
.onNotificationOpened(() => {
});