Android 7.0引入了用户enter text directly into a notification in order to respond to it的功能,而无需打开该应用程序。我正在使用react-native-firebase项目,以便在我的React Native应用程序中接收推送通知。
根据文档,似乎支持此功能 -特别是AndroidNotification.addAction和AndroidAction.addRemoteInput似乎表明这是可能的。
但是,我找不到有关如何正确实现此功能的任何示例。使用react-native-firebase
的React Native项目是否支持此功能?
答案 0 :(得分:2)
要添加到@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(即app-local-notifications)
<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。
5.通知声音应为默认声音。
答案 1 :(得分:1)
是的,这是可能的:
更新您的AndroidManifest.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"/>
添加包含以下内容的.js文件(例如backgroundMessaging.js
)
import firebase from 'react-native-firebase'
export const backgroundMessageListener = async (message) => {
const notification = new firebase.notifications.Notification()
// TODO: Configure your notification here...
// https://rnfirebase.io/docs/v4.3.x/notifications/reference/AndroidAction
const action = new firebase.notifications.Android.Action('reply', 'ic_launcher', 'Reply')
action.setShowUserInterface(false)
// https://rnfirebase.io/docs/v4.0.x/notifications/reference/AndroidRemoteInput
const remoteInput = new firebase.notifications.Android.RemoteInput("input")
remoteInput.setLabel('Reply')
action.addRemoteInput(remoteInput)
notification.android.addAction(action)
firebase.notifications().displayNotification(notification)
return Promise.resolve()
}
export const backgroundActionHandler = async (notificationOpen) => {
if (notificationOpen.action === 'reply') {
// TODO: Handle the input entered by the user here...
console.log(notificationOpen);
}
return Promise.resolve();
};
更新index.js
如下:
import { backgroundMessageListener, backgroundActionHandler } from './backgroundMessaging'
AppRegistry.registerHeadlessTask('RNFirebaseBackgroundMessage', () => backgroundMessageListener)
AppRegistry.registerHeadlessTask('RNFirebaseBackgroundNotificationAction', () => backgroundActionHandler);
注意:
本示例假定您已经配置了react-native-firebase
,并遵循了安装指南here。当您的应用程序不在前台并且收到“数据”通知时,将调用backgroundMessageListener
函数。 Receiving Notifications包含有关如何执行其他设置(例如,请求许可以接收通知)的示例。