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

时间:2018-07-26 18:48:28

标签: react-native firebase-cloud-messaging android-notifications react-native-android

在构建通知并使用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();
};

1 个答案:

答案 0 :(得分:0)

我已经尝试了以下操作及其工作。但是你需要 确保只发送数据有效载荷。

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

var nano = require('nano')('http://localhost:5984');
var db = nano.use('address');
var app = express();

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

 <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. 将其从您的根项目添加到index.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. 并将其发送到管理脚本以仅发送数据有效负载

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