代码之家  ›  专栏  ›  技术社区  ›  Donut

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

  •  0
  • Donut  · 技术社区  · 7 年前

    enter text directly into a notification in order to respond to it ,而不打开应用程序。我用的是 react-native-firebase

    根据文档,似乎支持此功能 --具体来说, AndroidNotification.addAction AndroidAction.addRemoteInput 似乎表明这是可能的。

    react-native-firebase

    1 回复  |  直到 7 年前
        1
  •  3
  •   Donut    7 年前

    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"/>
      
    2. 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();
      };
      
    3. 更新 index.js 具体如下:

      import { backgroundMessageListener, backgroundActionHandler } from './backgroundMessaging'
      
      AppRegistry.registerHeadlessTask('RNFirebaseBackgroundMessage', () => backgroundMessageListener)
      AppRegistry.registerHeadlessTask('RNFirebaseBackgroundNotificationAction', () => backgroundActionHandler);
      

    笔记:
    本例假设您已经配置了 react-native-firebase 并遵循安装指南 here . 这个 backgroundMessageListener 当你的应用程序不在前台并且收到“数据”通知时,函数将被调用。这个 Receiving Notifications

        2
  •  2
  •   Rohit Hazra    6 年前

    再加上@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。

    推荐文章