在构建通知并使用react native firebase添加远程输入和操作之后,当应用程序未显示在前台或屏幕上时,用户输入不会控制台日志。此时,notificationOpen.results默认为undefined,否则用户单击“发送”后将在其中填充用户响应。下面是添加远程输入并侦听响应的代码。
export const backgroundMessageListener = async (message) => {
console.log(message);
const { data } = message;
const notification = new firebase.notifications.Notification()
.setTitle(data.title)
.setBody(data.body)
.setSound('default')
.android.setSmallIcon('notification')
.android.setColor(colors.primary.normal)
.android.setAutoCancel(true)
.android.setLargeIcon(data.icon)
.android.setChannelId(data.channel)
.android.setPriority(firebase.notifications.Android.Priority.Max);
if (!notification.android.channelId) {
notification.android.setChannelId('misc_channel');
} else {
// Build an action
const action = new firebase.notifications.Android.Action('reply', 'check-mark', `Reply to ${notification.title}`);
action.setShowUserInterface(false);
// Build a remote input
const remoteInput = new firebase.notifications.Android.RemoteInput('input')
.setLabel('Reply')
.setAllowFreeFormInput(true);
// Add the remote input to the action
action.addRemoteInput(remoteInput);
// Add the action to the notification
notification.android.addAction(action);
}
// display the notification
firebase.notifications().displayNotification(notification);
return Promise.resolve();
};
export const backgroundActionHandler = async (notificationOpen) => {
if (notificationOpen.action === 'reply') {
console.log(notificationOpen);
}
return Promise.resolve();
};
答案 0 :(得分:0)
我已经尝试了以下操作及其工作。但是你需要 确保只发送数据有效载荷。
var nano = require('nano')('http://localhost:5984');
var db = nano.use('address');
var app = express();
<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"/>
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
}
};
AppRegistry.registerHeadlessTask('RNFirebaseBackgroundMessage', () => backgroundMessageListener)
AppRegistry.registerHeadlessTask('RNFirebaseBackgroundNotificationAction', () => backgroundActionHandler);