Spi*_*uce 10 firebase reactjs react-native
这里首次使用Firebase和RN用户,试图弄清楚如何传输应用用户的设备ID /令牌.
我正在使用像这样基本的东西来立即将信息传输到我的firebase数据库(使用firebase.database().ref()
).
updateDB = (timestamp_key, item_name, item_url) => {
firebase.database().ref(timestamp_key).set({
item_name: item_name,
item_url: item_url
})
}
Run Code Online (Sandbox Code Playgroud)
我还没有找到一个简单直截了当的答案,如何获取一个设备令牌并传输它(所以我知道谁的行动是后者).
有一个(我认为应该是)一个相对简单的方法吗?谢谢!
PS除了初始化连接的配置之外,我在脚本中调用了更高的位置 - 也许它在这里的某处我需要抓住设备令牌?
// Initialize Firebase
const firebaseConfig = {
apiKey: "blah",
authDomain: "blah",
databaseURL: "blah",
storageBucket: "blah",
messagingSenderId: "blah"
};
firebase.initializeApp(firebaseConfig);
Run Code Online (Sandbox Code Playgroud)
所以我也将 Expo 与 React Native 结合使用。但是,我使用的是 Google 的 Cloud Firestore,而不是实时数据库。
我不确定我完全理解你的问题,但这就是我的做法:
const firebase = require('firebase');
import { Constants } from 'expo';
// Required for side-effects
require('firebase/firestore');
import { firebaseConfig } from './../keys';
class Fire {
constructor() {
this.init();
this.observeAuth();
}
init = () => firebase.initializeApp(firebaseConfig);
observeAuth = () => firebase.auth().onAuthStateChanged(this.onAuthStateChanged);
onAuthStateChanged = user => {
if (!user) {
try {
firebase.auth().signInAnonymously();
} catch ({ message }) {
console.warn(message);
}
} else {
this.saveUser();
}
};
saveUserInfo = () => {
const { uid } = this;
if (!uid) {
return;
}
// You can add any information here
const info = {
deviceId: Constants.deviceId,
};
const ref = this.db.collection('users').doc(uid);
const setWithMerge = ref.set({ uid, ...info }, { merge: true });
};
get db() {
return firebase.firestore();
}
get uid() {
return (firebase.auth().currentUser || {}).uid;
}
}
Fire.shared = new Fire();
export default Fire;
Run Code Online (Sandbox Code Playgroud)
我从 Evan Bacon 的示例中获取了大部分代码 - https://github.com/EvanBacon/Expo-Pillar-Valley。
归档时间: |
|
查看次数: |
1235 次 |
最近记录: |