我正在尝试使用react-native fire-base启用google firebase推送通知。当应用程序在后台运行但前台通知无法正常工作时,我可以接收推送通知。另外,如何使推送通知像其他通知一样显示,例如Whatsapp通知,向用户显示下拉警报。
我尝试使用此Push notification not showing in Android foreground,但仍然无法在前台设置通知。
MainFest.xml
package="com.pragyaam.grid.mobile">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
android:name=".MainApplication"
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:allowBackup="false"
android:theme="@style/AppTheme">
<activity
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>
用于通知的App.js代码
this.notificationListener = firebase.notifications().onNotification((notification) => {
let notificationMessage = notification._android._notification._data.action;
let recordId = notification._android._notification._data.recordID;
const channelId = new firebase.notifications.Android.Channel(
'Default',
'Default',
firebase.notifications.Android.Importance.High
);
firebase.notifications().android.createChannel(channelId);
let notification_to_be_displayed = new firebase.notifications.Notification({
data: notification._android._notification._data,
sound: 'default',
show_in_foreground: true,
title: notification.title,
body: notification.body,
});
if (Platform.OS == 'android') {
notification_to_be_displayed.android
.setPriority(firebase.notifications.Android.Priority.High)
.android.setChannelId('Default')
.android.setVibrate(1000);
}
console.log('FOREGROUND NOTIFICATION LISTENER: \n', notification_to_be_displayed);
firebase.notifications().displayNotification(notification_to_be_displayed);
const { title, body } = notification;
this.showAlert(title, body,);
});
const notificationOpen =await firebase.notifications().getInitialNotification();
if (notificationOpen) {
const { title, body } = notificationOpen.notification;
this.showAlert(title, body);
}
this.notificationOpenedListener = firebase.notifications().onNotificationOpened((notificationOpen) => {
const { title, body } = notificationOpen.notification;
this.showAlert(title, body);
});
this.messageListener = firebase.messaging().onMessage((message) => {
//process data message
});
this.handleConnectivityChange = isConnected => {
this.setState({ isConnected });
}
}```