可能未处理的承诺拒绝ID:无效的FCM事件订阅

时间:2017-10-26 08:13:22

标签: javascript android react-native push-notification react-native-fcm

Index.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 event error

1 个答案:

答案 0 :(得分:2)

我明白这已经太晚了,但我遇到了同样的问题,我通过导入

修复了它
import FCM, {FCMEvent} from "react-native-fcm";

然后使用FCMEvent订阅事件:

this.notificationSubscribe = FCM.on(FCMEvent.Notification, notif => {
   this.showNotification(notif);
});