如何在收到通知时触发“ onNotification”功能?

时间:2020-10-08 14:52:25

标签: react-native firebase-cloud-messaging react-native-firebase react-native-push-notification

我正在使用React native-firebase版本6和react-native-push-notification包来处理react本机应用程序和集成的推送通知。 但是获取远程通知时不会触发我的“ onNotification”功能。 在应用程序内部登录后,如何调用“ onNotification”?每次发生远程通知时,它将如何触发?

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

NotificationService ::

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”对象,它已成功创建令牌,然后我在应用程序内部登录并通过用户会话保存了令牌。现在,用户登录后,我想在主屏幕中处理通知,我通过创建“ NotificationService”对象调用了configure()。我正在尝试使用来自fcm的令牌发送测试消息,但应用程序未触发“ onNotification”功能。 收到通知时如何触发主要配置的“ onNotification”功能?

0 个答案:

没有答案