android - Ionic 3 Firebase Google Auth 在 Android 设备上停止工作

标签 android firebase ionic3 angularfire2 ionic-native

我的应用程序运行完美,直到我尝试实现通知,在 Android 设备上运行时遇到 firebase 拒绝连接和 404 错误,但一切在浏览器上运行完美。然后重新安装所有插件和模块,解决了 firebase 连接问题,但 Google native 登录方法根本不起作用,甚至没有抛出任何错误

signInWithGoogle() { 
    if (this.platform.is('cordova')) {
        this.nativeGoogleLogin();
    }else{
        this.webGoogleLogin().then(()=>{});
    }
}
async nativeGoogleLogin(): Promise<void>{
    try {
        this.loading.present();
        const gplusUser = await this.gplus.login({
            'webClientId':'******',
            'offline':true,
            'scopes':'profile email'
        }) 
        console.log('NativeLogin');
        return await this.afAuth.auth.signInWithCredential(
            firebase.auth.GoogleAuthProvider.credential(gplusUser.idToken))

        // return await this.afAuth.auth.signInAndRetrieveDataWithCredential(
        //     firebase.auth.GoogleAuthProvider.credential(gplusUser.idToken))
        .then(data =>{
            this.loading.dismiss();
            console.log(data);
            this.updateUserData(data.user);
        }).catch(err =>{
            this.loading.dismiss();
            console.log('NativeLoginError',err);
        })
    } catch (error) {
        this.loading.dismiss();
        console.error('NativeLoginError',error); 
    }
}

异步方法甚至没有控制台记录文本。

console.log('NativeLogin');

我仔细检查了 webClientId。正在努力解决这个问题

最佳答案

所以您在安装 Firebase Notifications 时开始看到错误?流量计?我会先卸载它,然后看看问题是否仍然存在。有时插件有相互冲突的依赖关系,并且会相互混淆。

您是否在应用程序运行时安装了 FCM?根据我的经验,它会搞乱热重载,并且它不再知道应该将东西正确放置在哪里,所以如果是这种情况,简单的停止和启动应该可以解决这个问题。

尝试运行 ionic doctor check 这将解决 ionic 项目在 CLI 方面遇到的任何问题。

此外,稍微清理一下您的代码,使其更易于理解。

signInWithGoogle() { 
    if (this.platform.is('cordova')) {
        this.nativeGoogleLogin().then(data => {
    this.loading.dismiss();
    this.updateUserData(data.user);
}).catch(err => { console.log('Native login error: ', JSON.stringify(err); });
    } else {
        this.webGoogleLogin();
    }
}

nativeGoogleLogin(): Promise<void> { // Personally I would take this out of the try catch because you already are catching the error with the .catch of your promise.

this.loading.present();
const gplusUser = await this.gplus.login({
            'webClientId':'******',
            'offline':true,
            'scopes':'profile email'
        });
return await this.afAuth.auth.signInWithCredential(
            firebase.auth.GoogleAuthProvider.credential(gplusUser.idToken));
}

要检查的另一件事是查看您是否真的在调用 signInWithGoogle() 方法,因为这是我刚开始时常犯的错误。

也尝试在任何地方输入 console.log 消息,我个人最喜欢做的事情如下,尤其是在开发过程中。

console.log('File-name.ts: What-Im-Attempting-to-do: status(error, success or JSON.stringify(data);');

这样可以更轻松地追踪您的错误,而不必记住每个错误应该属于什么。

如果所有这些都不起作用,请尝试完全删除您的平台,然后重新添加。

ionic cordova platform rm ios/android 
ionic cordova platform add ios/android

希望这能有所帮助!

关于android - Ionic 3 Firebase Google Auth 在 Android 设备上停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50898959/

相关文章:

android - 在 Android 中从 Facebook 注销的代码

android - 布局表达式中可绘制对象的三元运算符

ios - 检索要标记的 Firebase 数据

java - 创建实现 Serialized/Parcelable 的 Firebase POJO 类

firebase - 在 flutter 中验证电子邮件时不会调用 onAuthStateChanged

android - 如何将 OpenCV 与 Ionic 3 集成?

java - android 定时器任务替代长时间运行的BackgroundService

Android 如何从 AsyncTask 内部处理网络异常

css - 在 ion-searchbar 上实现 float 标签和跨设备(ios/android)不一致的颜色

javascript - 如何在 Angular 中使用 WebWorker?