如何获取设备令牌in native native

ach*_*chu 5 push-notification react-native react-native-ios

我正在使用react-native 0.49.3版本,我的问题是如何获取IOS和Android本地的设备令牌我尝试使用此链接,但它不适用于我,现在我在IOS中尝试过.如何解决它可以告诉我如何配置?

小智 6

我尝试了不同的解决方案,我决定使用React Native Firebase.

在这里,您将找到有关通知的所有信息.

此外,您还可以使用Firebase附带的其他库,例如Google Analytics和Crash Reporting

设置库后,您可以执行以下操作:

// utils/firebase.js
import RNFirebase from 'react-native-firebase';

const configurationOptions = {
  debug: true,
  promptOnMissingPlayServices: true
}

const firebase = RNFirebase.initializeApp(configurationOptions)

export default firebase


// App.js
import React, { Component } from 'react';
import { Platform, View, AsyncStorage } from 'react-native';

// I am using Device info
import DeviceInfo from 'react-native-device-info';

import firebase  from './utils/firebase';

class App extends Component {

 componentDidMount = () => {
    var language = DeviceInfo.getDeviceLocale();

    firebase.messaging().getToken().then((token) => {
       this._onChangeToken(token, language)
    });

    firebase.messaging().onTokenRefresh((token) => {
        this._onChangeToken(token, language)
    });
  }

  _onChangeToken = (token, language) => {
    var data = {
      'device_token': token,
      'device_type': Platform.OS,
      'device_language': language
    };

    this._loadDeviceInfo(data).done();
  }

  _loadDeviceInfo = async (deviceData) => {
    // load the data in 'local storage'.
    // this value will be used by login and register components.
    var value = JSON.stringify(deviceData);
    try {
      await AsyncStorage.setItem(config.DEVICE_STORAGE_KEY, value);
    } catch (error) {
      console.log(error);
    }
  };  

  render() {
    ...
  }
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用令牌和所需的所有信息调用服务器.

  • 此方法获取FCM设备令牌,而不获取APNs令牌。APNs令牌对于故障排除很有用。 (2认同)