加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

react-native – 如何使设备令牌以本机方式响应

发布时间:2020-12-15 16:21:16 所属栏目:百科 来源:网络整理
导读:我正在使用react-native 0.49.3版本,我的问题是如何获取 IOS和 Android的本机设备令牌我尝试使用此 link,但它不适用于我,现在我在IOS中尝试过.如何解决它可以告诉我如何配置? 解决方法 我尝试了不同的解决方案,我决定使用 React Native Firebase. Here您将
我正在使用react-native 0.49.3版本,我的问题是如何获取 IOS和 Android的本机设备令牌我尝试使用此 link,但它不适用于我,现在我在IOS中尝试过.如何解决它可以告诉我如何配置?

解决方法

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

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

此外,您还可以使用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() {
    ...
  }
}

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读