无法使用react-native-firebase从Firebase控制台发送推送通知

时间:2017-06-21 02:18:19

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

我正在尝试通过令牌ID从Firebase控制台向我的ios设备发送推送通知。我使用react-native-firebase允许应用程序根据通知事件执行操作。我按照说明集成了SDK并设置了APNS证书等:

http://invertase.io/react-native-firebase/#/installation-ios

我的firebase.js配置文件如下所示:

import RNFirebase from 'react-native-firebase';

const configurationOptions = {
  debug: true
};

const firebase = RNFirebase.initializeApp(configurationOptions);

export default firebase;

我的主要React组件如下所示:

import React, { Component } from 'react'
import {
  Text,
  View,
  Alert,
  Platform
} from 'react-native'
import firebase from './firebase'

export default class extends Component {

  constructor(props) {
     super(props)
     this.state = {
        token: ''
     }
  }

  componentDidMount() {
     firebase.messaging().getToken()
        .then((token) => {
           this.setState({ token })
           console.log('token: ', token)
        })

     firebase.messaging().getInitialNotification()
        .then((notification) => {
           console.log('Notification which opened the app: ', notification)
        })

     firebase.messaging().onMessage((message) => {
        console.log('messaging', message)
     })

     firebase.messaging().onTokenRefresh((token) => {
        console.log('Refreshed FCM token: ', token)
     })
  }

  render() {
     return (
        <View style={{ marginTop: 22 }}>
           <Text>{this.state.token}</Text>
        </View>
     )
  }

}

我在组件安装时成功获取令牌,然后在Firebase控制台中使用该令牌发送通知,但未收到通知。我使用的是真正的设备,而不是iPhone。我正在使用启用了推送通知的开发配置文件,并且已成功启用删除通知后台授权,以及已上载到firebase控制台的相应开发APNs证书。

为什么我没有在设备上收到通知?

2 个答案:

答案 0 :(得分:1)

好吧,看起来它只是忽略远程通知,因为我没有请求用户的权限。只需要通过SDK执行此操作:

componentDidMount() {
    firebase.messaging().requestPermissions()
    firebase.messageing().getToken().then...

答案 1 :(得分:0)

好吧,看来它只是忽略了远程通知,因为我没有从用户那里请求权限。只需通过SDK即可做到: