再加上@Donut的回答,现在情况有了一些变化。
AndroidManifest.xml(根据@Donut保持不变)
<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 android:name="io.invertase.firebase.notifications.RNFirebaseBackgroundNotificationActionsService"/>
建议您在此处添加频道id(即应用程序本地通知)
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="@string/default_notification_channel_id"/>
你的backgroundMessaging.js现在应该
import firebase from 'react-native-firebase'
const channelId = 'app-local-notifications'
export const backgroundMessageListener = async (message) => {
const channel = new firebase.notifications.Android.Channel(
channelId,
'Local Interactive Notifications',
firebase.notifications.Android.Importance.Max
).setDescription('Local Interactive Notifications');
firebase.notifications().android.createChannel(channel);
const localNotification = new firebase.notifications.Notification({
sound: 'default' //important
})
.setNotificationId(message.messageId)
.setTitle(message.data.title)
.setBody(message.data.body)
.android.setChannelId(channelId) //important
.android.setSmallIcon('ic_launcher')
.android.setPriority(firebase.notifications.Android.Priority.High); //important
const action = new firebase.notifications.Android.Action('reply', 'ic_launcher', 'Reply')
action.setShowUserInterface(false)
const remoteInput = new firebase.notifications.Android.RemoteInput("inputText")
remoteInput.setLabel('Message')
action.addRemoteInput(remoteInput)
localNotification.android.addAction(action)
firebase.notifications().displayNotification(localNotification).catch(err => console.log(err)); //important
}
export const backgroundActionHandler = async (notificationOpen) => {
if (notificationOpen && notificationOpen.notification) {
const action = notificationOpen.action;
const notificationId = notificationOpen.notification.notificationId;
if (action === "reply") {
console.log(notificationOpen)
} else {
console.log("unsupported action", action);
}
// hide the notification instead of Promise.resolve()
firebase.notifications().removeDeliveredNotification(notificationId); //important
}
};
注意事项:
1.通过FCM发送的通知应
data-only
通知。
2.通知优先级应为“高”。
3.通知行动不得超过60秒。
4.需要ChannelID。