1

索引.js

import FCM from "react-native-fcm";

class Register extends Component {
constructor(props) {
        super(props);
componentDidMount () {
   // this method generate fcm token.
    FCM.requestPermissions();
    FCM.getFCMToken().then(token => {
      console.log("TOKEN (getFCMToken)", token);
    });

    // This method get all notification from server side.
    FCM.getInitialNotification().then(notif => {
      console.log("INITIAL NOTIFICATION", notif)
    });

    // This method give received notifications to mobile to display.
    this.notificationUnsubscribe = FCM.on("notification", notif => {
      console.log("a", notif);
      if (notif && notif.local_notification) {
        return;
      }
      this.sendRemote(notif);
    });

    // this method call when FCM token is update(FCM token update any time so will get updated token from this method)
     FCM.on("refreshToken", token => {
      console.log("TOKEN (refreshUnsubscribe)", token);
      this.props.onChangeToken(token);
    });
     }
     sendRemote(notif) {
    console.log('send');
    FCM.presentLocalNotification({
      title: notif.title,
      message: notif.message,
      priority: "high",
      click_action: notif.click_action,
      show_in_foreground: true,
      local: true
    });
  }
componentWillUnmount() {
    this.refreshUnsubscribe();
    this.notificationUnsubscribe();
  }

这是我用于生成令牌和处理来自服务器的通知的代码的一部分。我在做什么错?想不通,请帮忙。提前致谢。下面是我得到的警告错误的截图。

fcm 事件错误

4

1 回答 1

2

我知道这为时已晚,但遇到了同样的问题,我通过导入修复了它

import FCM, {FCMEvent} from "react-native-fcm";

然后使用 FCMEvent 订阅事件:

this.notificationSubscribe = FCM.on(FCMEvent.Notification, notif => {
   this.showNotification(notif);
});
于 2018-04-10T06:39:16.903 回答