React native 如何触发;“通知”;收到通知后的功能?

React native 如何触发;“通知”;收到通知后的功能?,react-native,firebase-cloud-messaging,react-native-firebase,react-native-push-notification,React Native,Firebase Cloud Messaging,React Native Firebase,React Native Push Notification,我正在开发react本机应用程序和集成推送通知,其中我使用react本机firebase版本6和软件包来处理通知。 但我的“onNotification”功能在获取远程通知时不会触发。 在登录到应用程序内部后,我如何调用“onNotification”,以及每次发生远程通知时它将如何触发 我创建了“NotificationHandler”类,在其中调用了“configure”函数,并创建了NotificationService,在构造函数中调用了NotificationHandler 通知服务:

我正在开发react本机应用程序和集成推送通知,其中我使用react本机firebase版本6和软件包来处理通知。 但我的“onNotification”功能在获取远程通知时不会触发。 在登录到应用程序内部后,我如何调用“onNotification”,以及每次发生远程通知时它将如何触发

我创建了“NotificationHandler”类,在其中调用了“configure”函数,并创建了NotificationService,在构造函数中调用了NotificationHandler

通知服务::

import PushNotification from 'react-native-push-notification';
import NotificationHandler from './NotificationHandler';

export default class NotificationService{
  constructor(onRegister, onNotification) {
    this.lastId = 0;
    this.lastChannelCounter = 0;

    this.createDefaultChannels();

    NotificationHandler.attachRegister(onRegister);
    NotificationHandler.attachNotification(onNotification);

    // Clear badge number at start
    PushNotification.getApplicationIconBadgeNumber(function (number) {
      if (number > 0) {
        PushNotification.setApplicationIconBadgeNumber(0);
      }
    });
    
    PushNotification.getChannels(function(channels) {
      console.log(channels);
    });
  }
}
NotificationHandler::

class NotificationHandler {
  onNotification(notification) {
    console.log('NotificationHandler:', notification);

    if (typeof this._onNotification === 'function') {
      this._onNotification(notification);
    }
  }

  onRegister(token) {
    console.log('NotificationHandler:', token);

    if (typeof this._onRegister === 'function') {
      this._onRegister(token);
    }
  }

  onAction(notification) {
    console.log ('Notification action received:');
    console.log(notification.action);
    console.log(notification);

    if(notification.action === 'Yes') {
      PushNotification.invokeApp(notification);
    }
  }

  // (optional) Called when the user fails to register for remote notifications. Typically occurs when APNS is having issues, or the device is a simulator. (iOS)
  onRegistrationError(err) {
    console.log(err);
  }
  
  attachRegister(handler) {
    this._onRegister = handler;
  }

  attachNotification(handler) {
    this._onNotification = handler;
  }
}

const handler = new NotificationHandler();

PushNotification.configure({
  // (optional) Called when Token is generated (iOS and Android)
  onRegister: handler.onRegister.bind(handler),

  // (required) Called when a remote or local notification is opened or received
  onNotification: handler.onNotification.bind(handler),

  // (optional) Called when Action is pressed (Android)
  onAction: handler.onAction.bind(handler),

  // (optional) Called when the user fails to register for remote notifications. Typically occurs when APNS is having issues, or the device is a simulator. (iOS)
  onRegistrationError: handler.onRegistrationError.bind(handler),

  // IOS ONLY (optional): default: all - Permissions to register.
  permissions: {
    alert: true,
    badge: true,
    sound: true,
  },

  // Should the initial notification be popped automatically
  // default: true
  popInitialNotification: true,

  /**
   * (optional) default: true
   * - Specified if permissions (ios) and token (android and ios) will requested or not,
   * - if not, you must call PushNotificationsHandler.requestPermissions() later
   */
  requestPermissions: true,
});
导出默认处理程序; 在App.js构造函数中,我创建了一个“NotificationService”的对象,它成功地创建了令牌,然后我登录到App内部并与用户会话保存了令牌。现在,在用户登录后,我想在主屏幕内处理通知,我通过创建一个“NotificationService”对象调用configure()。我试图使用fcm的令牌发送测试消息,但应用程序未触发“onNotification”功能。 我们如何在收到通知时触发主配置“onNotification”功能?

试试这个:试试这个: