[react-native] [android]应用未收到通知

时间:2018-01-19 09:35:43

标签: firebase react-native firebase-cloud-messaging react-native-fcm

这可能就像一个愚蠢的问题。

我尝试使用react-native-fcm使用Firebase通知为Android做出反应原生演示应用。

完成所有设置后,Firebase注册了一个应用程序连接,我尝试推送通知测试,应用程序执行两项操作:

  1. 它未注册通知事件
  2. 在firebase发送通知后10-20秒关闭。 (可能这是我的应用程序中的一些错误,我将检查后者。)
  3. 这是我的应用程序通知监听器:

    Listeners.js

    
    
    import {
      Platform,
      AsyncStorage
    } from 'react-native';
    
    import FCM, {
      FCMEvent,
      RemoteNotificationResult,
      WillPresentNotificationResult,
      NotificationType
    } from "react-native-fcm";
    
    AsyncStorage.getItem('lastNotification').then(data => {
      if (data) {
        // if notification arrives when app is killed, it should still be logged here
        console.log('last notification', JSON.parse(data));
        AsyncStorage.removeItem('lastNotification');
      }
    })
    
    export function registerKilledListener() {
      // these callback will be triggered even when app is killed
      FCM.on(FCMEvent.Notification, notif => {
        AsyncStorage.setItem('lastNotification', JSON.stringify(notif));
      });
    }
    
    // these callback will be triggered only when app is foreground or background
    export function registerAppListener() {
      FCM.on(FCMEvent.Notification, notif => {
        console.log("Notification", notif);
        if (notif.local_notification) {
          return;
        }
        if (notif.opened_from_tray) {
          return;
        }
    
        if (Platform.OS === 'ios') {
          //optional
          //iOS requires developers to call completionHandler to end notification process. If you do not call it your background remote notifications could be throttled, to read more about it see the above documentation link.
          //This library handles it for you automatically with default behavior (for remote notification, finish with NoData; for WillPresent, finish depend on "show_in_foreground"). However if you want to return different result, follow the following code to override
          //notif._notificationType is available for iOS platfrom
          switch (notif._notificationType) {
            case NotificationType.Remote:
              notif.finish(RemoteNotificationResult.NewData) //other types available: RemoteNotificationResult.NewData, RemoteNotificationResult.ResultFailed
              break;
            case NotificationType.NotificationResponse:
              notif.finish();
              break;
            case NotificationType.WillPresent:
              notif.finish(WillPresentNotificationResult.All) //other types available: WillPresentNotificationResult.None
              break;
          }
        }
      });
    
      FCM.on(FCMEvent.RefreshToken, token => {
        console.log("TOKEN (refreshUnsubscribe)", token);
        this.props.onChangeToken(token);
      });
    
      FCM.enableDirectChannel();
      FCM.on(FCMEvent.DirectChannelConnectionChanged, (data) => {
        console.log('direct channel connected' + data);
      });
      setTimeout(function() {
        FCM.isDirectChannelEstablished().then(d => console.log(d));
      }, 1000);
    }
    
    
    

    此处是管理通知的位置:

    Notifications.js

    
    
    import React, {
      Component
    } from 'react'
    import {
      Platform,
      View,
      TouchableOpacity,
      Text
    } from 'react-native';
    import FCM, {
      FCMEvent,
      RemoteNotificationResult,
      WillPresentNotificationResult,
      NotificationType
    } from 'react-native-fcm';
    import {
      registerKilledListener,
      registerAppListener
    } from "./Listeners";
    
    registerKilledListener();
    
    export default class Notifications extends Component {
    
      constructor(props) {
        super(props);
    
        this.state = {
          token: "",
          tokenCopyFeedback: ""
        }
      }
    
      async componentDidMount() {
        registerAppListener();
        FCM.getInitialNotification().then(notif => {
          console.log(notif)
          this.setState({
            initNotif: notif
          })
        });
    
        try {
          let result = await FCM.requestPermissions({
            badge: false,
            sound: true,
            alert: true
          });
        } catch (e) {
          console.error(e);
        }
    
        FCM.getFCMToken().then(token => {
          console.log("TOKEN (getFCMToken)", token);
          this.setState({
            token: token || ""
          })
        });
    
        if (Platform.OS === 'ios') {
          FCM.getAPNSToken().then(token => {
            console.log("APNS TOKEN (getFCMToken)", token);
          });
        }
      }
    
      componentWillUnmount() {
    
      }
    
      render() {
        return null
      }
    }
    
    
    

    我完全迷失了,任何帮助都会受到影响。非常感谢,对不起我的拼写错误。

0 个答案:

没有答案