首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RNFirebase v6推送通知不会同时带来iOS和Android

RNFirebase v6推送通知不会同时带来iOS和Android
EN

Stack Overflow用户
提问于 2020-03-04 19:39:05
回答 1查看 2.7K关注 0票数 3

我正在尝试将通知从firebase控制台发送到我的react原生应用程序。

据我所知,我在这里跟踪了糟糕的文档:https://invertase.io/oss/react-native-firebase/v6/messaging/quick-start

我安装了@react本机-firebase/app和/messaging,下面是我在组件中的代码:

代码语言:javascript
复制
  componentDidMount() {

    this.reqNotifications()
    this.checkNotificationPermission()

  }


  reqNotifications() {

    requestNotifications(['alert', 'badge', 'sound']).then(({status, settings}) => {

      console.log('NOTIFICATION STATUS' + status)

    });

  }

  async checkNotificationPermission() {
    const enabled =  await messaging().hasPermission();
    if (enabled) {
      console.log('APPROVED');
      await messaging().registerForRemoteNotifications()
      messaging().getToken().then(token => console.log('token: >> ' + token))



    } else {
      console.log('NOT APPROVED');
    }
  }

我通过working.

  • My、Apple是苹果的OK和Firebase控制台

  • 请求许可,并在代码succesfully.

上通过getToken()方法获得令牌

但是我不能把任何东西从火力基地发送到设备上,在前景和背景上都没有发生任何事情。我试过-象征性测试,也尝试了正常,但没有,什么都没有发生。

我将此代码添加到componentDidMount中:

代码语言:javascript
复制
messaging().onMessage(async remoteMessage => {
  console.log('FCM Message Data:', remoteMessage.data);
});

正如我所理解的那样,它订阅了云消息,并且当我从firebase控制台发送一些云消息通知时,我应该得到控制台输出;,但是什么都没有发生。

我不知道我错过了什么,但我认为这个包有一个很大的更新,大部分文档都是以前版本的,我真的很留恋这里,谢谢你的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-20 08:06:53

代码语言:javascript
复制
for rnfirebase.io V6
 componentDidMount = async () => {
    this.checkNotificationPermission();
    await messaging().requestPermission({provisional: true});
    await messaging().registerDeviceForRemoteMessages();

    await this.getFCMToken();
    if (Platform.OS === 'android') {
      this.createAndroidNotificationChannel();
    }

    this.backgroundState();
    this.foregroundState();
  };   
checkNotificationPermission = () => {
    firebase
      .messaging()
      .hasPermission()
      .then(enabled => {
        if (!enabled) {
          this.promptForNotificationPermission();
        }
      });
  };

  promptForNotificationPermission = () => {
    firebase
      .messaging()
      .requestPermission({provisional: true})
      .then(() => {
        console.log('Permission granted.');
      })
      .catch(() => {
        console.log('Permission rejected.');
      });
  };

  createAndroidNotificationChannel() {
    const channel = new firebase.notifications.Android.Channel(
      'channelId',
      'Push Notification',
      firebase.notifications.Android.Importance.Max,
    ).setDescription('Turn on to receive push notification');

    firebase.notifications().android.createChannel(channel);
  }
 foregroundState = () => {
    const unsubscribe = messaging().onMessage(async notification => {
      console.log('Message handled in the foregroundState!', notification);
    });

    return unsubscribe;
  };

  // Register background handler
  backgroundState = () => {
    messaging().setBackgroundMessageHandler(async notification => {
      console.log('Message handled in the background!', notification);
    });
  };
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60533547

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档