无法读取React Native Android应用中收到的firebase消息

时间:2018-03-17 12:42:22

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

我是React Native开发的新手。我已经通过引用https://github.com/evollu/react-native-fcm将firebase与我的React Native应用程序集成在一起。当我从Firebase控制台发送推送通知时,我可以在设备中看到通知。但我想使用AsyncStorage将其保存在我的React Native应用程序中。所以我改变了代码,如

    componentDidMount() {
        // iOS: show permission prompt for the first call. later just check permission in user settings
        // Android: check permission in user settings

        FCM.requestPermissions()
       .then(()=>console.log('granted'))
       .catch(()=>console.log('notification permission rejected'));

       FCM.getFCMToken().then(token => {
         console.log(token)
         // store fcm token in your server
       });

      this.notificationListener = FCM.on(FCMEvent.Notification, async (notif) => {
         // optional, do some component related stuff
       });

        // initial notification contains the notification that launchs the app. If user launchs app by clicking banner, the banner notification info will be here rather than through FCM.on event
        // sometimes Android kills activity when app goes to background, and when resume it broadcasts notification before JS is run. You can use FCM.getInitialNotification() to capture those missed events.
        // initial notification will be triggered all the time even when open app by icon so send some action identifier when you send notification

      FCM.getInitialNotification().then(notif => {
   console.log(notif)
          AsyncStorage.setItem('lastNotification', JSON.stringify(notif));
         alert('hello'+JSON.stringify(notif)+' Title='+notif.title+' Body='+notif.body);
     });
   }

在FCM.getInitialNotification()方法中,我试图存储收到的消息。但是从同一方法的警报中我才知道收到的消息格式与我预期的不同。提醒内容如下所示enter image description here

请指导我做正确的事。任何建议表示赞赏。

0 个答案:

没有答案