@ react-native-firebase /消息未在真实的ios设备中获取设备令牌

问题描述

我正在使用react-native-firebase v8和react-native v0.61.2。我最近将我的本机版本从0.61升级到0.61.2。在android中,一切正常,但在ios 真实设备中,messagement.getToken()在catch块中抛出错误,而没有获得任何令牌。

错误

enter image description here

我的FCM服务文件

class FcmService {

register = (onRegister,onNotification,onopenNotification) =>{
    this.checkPermission(onRegister)
    // when register function call that time we create notification listener
    this.createNoitificationListeners(onRegister,onopenNotification)
}

registerappWithFCM = async () => {
    if(Platform.OS === 'ios') {
        await messaging().registerDeviceForRemoteMessages();
        await messaging().setAutoInitEnabled(true);
    }
}

checkPermission = (onRegister) => {
    messaging().hasPermission()
        .then(enabled  => {
            if (enabled) {
                Alert.alert('enabled')
                //user has permission
                this.getToken(onRegister)
            } else {
                //user don't have permission
                this.requestPermission(onRegister)
            }
        }).catch(error => {
            let err = `check permission error${error}`
            Alert.alert(err)
        // console.log("[FCMService] Permission rejected",error)
    })
}

getToken = async (onRegister) => {
    let fcmToken = await AsyncStorage.getItem(Constants.FCM_TOKEN);
    if(!fcmToken) {
        messaging().getToken()
        .then(fcmToken => {
            Alert.alert(fcmToken)
            if (fcmToken) {
                onRegister(fcmToken)
            } else {
                // console.log("[FCMService] User does not have a device token")
            }
        }).catch(error => {
            let err = `FCm token get error${error}`
            Alert.alert(err)
        // console.log("[FCMService] getToken rejected ",error)
    })
    }
    else {

    }
}

requestPermission = (onRegister) => {
    messaging().requestPermission().then(() => {
        this.getToken(onRegister)
    }).catch(error => {
        // console.log("[FCMService] Requested persmission rejected ",error)
    })
}

deletedToken = async () => {
    await messaging().deletetoken()
        .catch(error => {
            // console.log("Delected token error ",error)
        })
}

createNoitificationListeners = (onRegister,onopenNotification) => {

    messaging().onNotificationopenedApp(remoteMessage => {
        Alert.alert(remoteMessage)
        // console.log("[FCMService] onNotificationopenedApp Notification caused app to open from background state:",remoteMessage);
        if(remoteMessage) {
            onopenNotification(remoteMessage)
        }
    });

    // when the application is opened form a quit state
    messaging()
        .getinitialNotification()
        .then(remoteMessage => {
            Alert.alert(remoteMessage)
            // console.log('[FCMService] getinitialNotification Notification caused app to open from quit state:',remoteMessage);
            if (remoteMessage) {
                onopenNotification(remoteMessage)
            }
        }
    );

    // Foreground state messages
    this.messageListener = messaging().onMessage(async remoteMessage => {
        Alert.alert(remoteMessage)
         //console.log("[FCMService] A new FCM message arrived",remoteMessage);
        if(remoteMessage) {
            let notification = null;
            if(Platform.OS === 'ios') {
                notification = remoteMessage
            } else {
                notification = remoteMessage
            }
            onNotification(notification);
        }
    });

    // Triggered when have new token
    messaging().onTokenRefresh(fcmToken => {
        // console.log("New token refresh: ",fcmToken)
        onRegister(fcmToken)
    })

}

unRegister = () => {
    this.messageListener()
}

}

App.js文件中,我调用了该方法

fcmService.registerappWithFCM();
fcmService.register(this.onRegister,this.onNotification,this.onopenNotification);

解决方法

您必须使用社区/推送通知ios依赖关系并寻求许可。 使用它我可以解决这个问题