Android API31+版本Firebase邮箱登录失效问题求助
Firebase邮箱/密码登录在API 31+设备失效问题解决
问题描述
开发基于Firebase的登录应用,已启用邮箱/密码和Google登录提供商。API 31以下设备上两种登录方式均正常工作;但API 31及以上设备中,Google登录正常,邮箱/密码登录完全无法使用。
已尝试操作
- 将所有依赖更新至最新版本
- 添加依赖
implementation 'androidx.work:work-runtime-ktx:2.7.0' - 尝试了能找到的所有解决方案
相关代码
AuthenticationActivity代码
class AuthenticationActivity : AppCompatActivity() { private lateinit var binding: ActivityAuthenticationBinding private val viewModel by viewModels<LoginViewModel>() companion object { const val TAG = "LoginFragment" const val SIGN_IN_RESULT_CODE = 1001 } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityAuthenticationBinding.inflate(layoutInflater) val view = binding.root setContentView(view) binding.authButton.setOnClickListener { launchSignInFlow() } viewModel.authenticationState.observe(this) { authenticationState -> when (authenticationState) { LoginViewModel.AuthenticationState.AUTHENTICATED -> switchActivities() else -> Log.e( TAG, "Authentication state that doesn't require any UI change $authenticationState" ) } } } private fun launchSignInFlow() { val providers = arrayListOf( EmailBuilder().build(), GoogleBuilder().build() ) startActivityForResult( AuthUI.getInstance().createSignInIntentBuilder().setAvailableProviders(providers) .build(), AuthenticationActivity.SIGN_IN_RESULT_CODE ) } override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) if (requestCode == SIGN_IN_RESULT_CODE) { val response = IdpResponse.fromResultIntent(data) if (resultCode == Activity.RESULT_OK) { startActivity(Intent(this@AuthenticationActivity, MyApp::class.java)) finish() return } else { if (response == null) { Log.e("Login", "Login canceled by User") return } if (response.error!!.errorCode == ErrorCodes.NO_NETWORK) { Log.e("Login", "No Internet Connection") return } if (response.error!!.errorCode == ErrorCodes.UNKNOWN_ERROR) { Log.e("Login", "Unknown Error") return } } Log.e("Login", "Unknown sign in response") } } private fun switchActivities() { val switchActivityIntent = Intent(this, RemindersActivity::class.java) startActivity(switchActivityIntent) } }
通知相关代码
private fun sendNotification(triggeringGeofences: List<Geofence>) { triggeringGeofences.forEach { val requestId = it.requestId val remindersRepository: ReminderDataSource by inject() //Interaction to the repository has to be through a coroutine scope CoroutineScope(coroutineContext).launch(SupervisorJob()) { //get the reminder with the request id val result = remindersRepository.getReminder(requestId) if (result is Result.Success<ReminderDTO>) { val reminderDTO = result.data //send a notification to the user with the reminder details sendNotification( this@GeofenceTransitionsJobIntentService, ReminderDataItem( reminderDTO.title, reminderDTO.description, reminderDTO.location, reminderDTO.latitude, reminderDTO.longitude, reminderDTO.id ) ) } } } }
关键提示
提示:Targeting S+(版本31及以上)要求创建PendingIntent时必须指定FLAG_IMMUTABLE或FLAG_MUTABLE。
解决方案
核心原因
Android 12(API 31)及以上版本强制要求创建PendingIntent时指定FLAG_IMMUTABLE或FLAG_MUTABLE。Firebase Auth UI处理邮箱登录时内部会创建PendingIntent,若未显式设置对应flag,会导致登录流程异常。
修复步骤
- 修改登录流配置
在launchSignInFlow方法中,为AuthUI的IntentBuilder添加setPendingIntentFlags:
private fun launchSignInFlow() { val providers = arrayListOf( EmailBuilder().build(), GoogleBuilder().build() ) startActivityForResult( AuthUI.getInstance().createSignInIntentBuilder() .setAvailableProviders(providers) // 添加PendingIntent flag,适配API 31+ .setPendingIntentFlags(PendingIntent.FLAG_IMMUTABLE) .build(), AuthenticationActivity.SIGN_IN_RESULT_CODE ) }
- 确认依赖版本
确保Firebase Auth UI依赖为最新版本,避免版本兼容问题:
// Module级别build.gradle implementation 'com.firebaseui:firebase-ui-auth:8.0.2' // 替换为最新版本
- 检查其他PendingIntent创建场景
如果代码中还有其他创建PendingIntent的地方(如通知逻辑),同样需要添加对应flag。例如创建通知的PendingIntent时:
val pendingIntent = PendingIntent.getActivity( context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE )
内容的提问来源于stack exchange,提问作者YaSsEr NaBiL
相关产品推荐
相关产品推荐

