当应用程序不在前台时,访问React Native Firebase远程输入通知Open.results



在使用react native firebase构建通知并添加远程输入和操作后,当应用程序没有前景或在屏幕上时,用户输入不会控制台日志。此时,notificationOpen.results默认为undefined,否则会在用户单击send后填充用户响应。下面是添加远程输入和监听响应的代码。

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();
};

我已经尝试过以下操作,并且它有效。但你需要请确保只发送数据负载。

  1. 将其添加到您的android清单xml中

<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"/>

  1. 将此文件添加到名为backgroundMessaging的src文件夹中

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. 将其从根项目添加到index.js

AppRegistry.registerHeadlessTask('RNFirebaseBackgroundMessage', () => backgroundMessageListener)
AppRegistry.registerHeadlessTask('RNFirebaseBackgroundNotificationAction', () => backgroundActionHandler);

  1. 并将此发送到管理脚本以发送仅限数据的有效负载

async function sendPushNotification(message) {
try {
await admin.messaging().sendToDevice(
['eTlozrPo49U:APA91bEPJhpZPN4rVUjJL93y9PxN5DFKH198RJOSPZ9qHgBenmtcuMIPOmUTZGuFEGc07OxGkgh4OpSyggOr13ud3W9Ipokp61VayZ29sucTuyjHCjDGlNCfTbliwKiN0uDBjpklpOR0'],
{
data: {
title: 'a',
body: 'a',
navigation: 'CreateOrderLandingScreen'
},
},
{
// Required for background/quit data-only messages on iOS
contentAvailable: true,
// Required for background/quit data-only messages on Android
priority: 'high',
},
);
console.log('A');
}catch(e) {
console.log('Gagal mengirim pesan');
}
}

相关内容

  • 没有找到相关文章

最新更新