【发布时间】:2021-06-24 03:13:04
【问题描述】:
我已经使用@react-native-firebase/messaging 启动了我的应用程序,现在我遇到了一些棘手的情况。
我的应用程序将 fcm 令牌保存在数据库中以针对特定用户并向他们发送通知。当用户打开应用程序时,它会将令牌保存到数据库中,以确保每个用户都有自己的令牌(为网络错误等任何问题做好准备)。
奇怪的是,大约有 11,000 名用户在我的应用程序中注册,但大约 3,000 名用户没有他们的令牌。没有数据库问题,即使在完全相同的设备上也会随机发生(无论是 iOS 还是 Android)。我很确定不会有多达 3000 人拒绝许可或删除我的应用。
不知道有没有人和我一样的问题。
下面是我使用messaging().getToken() 的简化代码。我的代码有问题吗?
(+) 我使用的是^11.5.0 版本,上周我将其更新为^12.0.0。
"@react-native-firebase/analytics": "^11.5.0",
"@react-native-firebase/app": "^11.5.0",
"@react-native-firebase/messaging": "^11.5.0",
"@react-native-firebase/remote-config": "^11.5.0",
(++) 在其他成功将其令牌保存在数据库中的设备上,一切正常。
App.tsx
const App = () => {
return (
<View style={{ flex: 1 }}>
<PushHandler />
<Navigation />
</View>
)
}
export default App
PushHandler.tsx
export const PushHandler: React.FC = () => {
const appState = useRef(AppState.currentState)
const [hasPermission, setHasPermission] = useState<FirebaseMessagingTypes.AuthorizationStatus>()
useEffect(() => {
// Check AppState, and when app has come to the foreground, check permission
const handleAppStateChange = (nextAppState: AppStateStatus) => {
if (appState.current.match(/inactive|background/) && nextAppState === 'active') {
messaging()
.hasPermission()
.then(authStatus => setHasPermission(authStatus))
}
appState.current = nextAppState
}
AppState.addEventListener('change', handleAppStateChange)
return AppState.removeEventListener('change', handleAppStateChange)
}, [])
useEffect(() => {
handleTokenPermission()
}, [])
useEffect(() => {
if (hasPermission === messaging.AuthorizationStatus.AUTHORIZED || hasPermission === messaging.AuthorizationStatus.PROVISIONAL) {
saveDeviceToken()
}
}, [hasPermission])
const handleTokenPermission = async () => {
try {
// Check whether permission allowed
const authStatus = await messaging().hasPermission()
const enabled = authStatus === messaging.AuthorizationStatus.AUTHORIZED || authStatus === messaging.AuthorizationStatus.PROVISIONAL
if (enabled) {
setHasPermission(authStatus)
} else {
// if permission already has been denied, move to settings
if (authStatus === messaging.AuthorizationStatus.DENIED) {
Alert.alert('check permission', undefined, [
{ text: 'cancel' },
{ text: 'settings', onPress: openSettings },
])
}
// else, request permission
else {
const authorized = await messaging().requestPermission()
setHasPermission(authorized)
}
}
} catch (error) {
console.error(error)
}
}
const saveDeviceToken = async () => {
try {
messaging()
.getToken()
.then(async fcmToken => {
console.log('PUSH TOKEN: ', fcmToken)
await saveTokenToDatabase({ token: fcmToken }) // This is the function I save fcm token into my database
})
return messaging().onTokenRefresh(async token => {
await saveTokenToDatabase({ token })
})
} catch (error) {
console.error(error)
}
}
return null
}
【问题讨论】:
-
很难知道什么是错误的代码似乎很好,但为什么你必须检查应用程序是否在前台?
-
当打开设置并允许权限的用户返回我的应用程序时立即获得权限和令牌。 (从这里
Alert.alert('check permission', undefined, [ { text: 'cancel' }, { text: 'settings', onPress: openSettings }]))
标签: react-native react-native-firebase