如何获取设备令牌in native native

时间:2017-11-09 05:29:26

标签: react-native push-notification react-native-ios

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

1 个答案:

答案 0 :(得分:6)

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

Here您将找到有关通知的所有信息。

此外,您还可以使用Firebase附带的其他库,例如Google Analytics和崩溃报告

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

// 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() {
    ...
  }
}

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