Javascript 使用react native firebase时添加对Android通知的回复

Javascript 使用react native firebase时添加对Android通知的回复,javascript,android,react-native,firebase-cloud-messaging,react-native-firebase,Javascript,Android,React Native,Firebase Cloud Messaging,React Native Firebase,安卓7.0为用户引入了无需打开应用程序就可以访问的功能。我使用该项目是为了在我的React本机应用程序中接收推送通知 根据文档,似乎支持此功能 --具体来说,这似乎表明这是可能的 但是,我找不到任何关于如何正确实现此功能的示例。使用React Native firebase的React Native项目是否支持此功能?是的,这是可能的: 更新您的AndroidManifest.xml文件以包括以下内容: <receiver android:name="io.invertase.fireba

安卓7.0为用户引入了无需打开应用程序就可以访问的功能。我使用该项目是为了在我的React本机应用程序中接收推送通知

根据文档,似乎支持此功能 --具体来说,这似乎表明这是可能的

但是,我找不到任何关于如何正确实现此功能的示例。使用React Native firebase的React Native项目是否支持此功能?

是的,这是可能的:

  • 更新您的
    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"/>
    
    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
    ,并遵循了安装指南。当您的应用程序不在前台并且收到“数据”通知时,将调用
    backgroundMessageListener
    函数。提供了有关如何执行其他设置(例如请求接收通知的权限)的示例

    是的,这是可能的:

  • 更新您的
    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"/>
    
    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
    ,并遵循了安装指南。当您的应用程序不在前台并且收到“数据”通知时,将调用
    backgroundMessageListener
    函数。提供了有关如何执行其他设置(例如请求接收通知的权限)的示例

    再加上@Donut的回答,现在情况有了一些变化

    AndroidManifest.xml(根据@Donut保持不变)

    注意事项: 1.通过FCM发送的通知应仅为数据通知。 2.通知优先级应为“高”。 3.通知操作的时间不得超过60秒。 4.ChannelID是必需的。
    5.通知声音应该是默认的。

    为了补充@Donut所回答的内容,现在情况有了一些变化

    AndroidManifest.xml(根据@Donut保持不变)

    注意事项: 1.通过FCM发送的通知应仅为数据通知。 2.通知优先级应为“高”。 3.通知操作的时间不得超过60秒。 4.ChannelID是必需的。
    5.通知声音应为默认声音。

    当应用程序处于后台或应用程序关闭时,android中不显示操作。您能否确切地告诉我如何从react native firebase实现相同的效果。提前感谢我已经完成了这项工作,它工作得非常完美,除非我在它永远加载的通知中提交输入,即使我调用
    Promise.resolve()
    @palkerecseni我看到了我的答案。当应用程序在后台或应用程序关闭时,在android中不显示操作。您能确切地告诉我如何从react native firebase实现相同的操作吗。提前感谢我已经完成了这项工作,它工作得非常完美,除非我在它永远加载的通知中提交输入,即使我调用
    Promise.resolve()
    @palkerecseni查看我的答案。