Android 11及以下后台点击FCM通知时Launcher Activity先启动问题
FCM通知点击后Launcher Activity提前启动问题(Android 11及以下)
在onMessageReceived中接收FCM消息并展示通知时,会先启动一个无UI的中间Activity(PushTracker)进行通知预处理,再跳转至目标Activity,默认启动Activity为MainActivity。
问题现象
- 应用处于前台/已杀死状态时,点击FCM通知,
PushTracker优先启动,完成预处理后跳转至目标页面; - 应用处于后台(按下Home键)时,点击FCM通知,会先调用Launcher Activity(MainActivity)的
onStart(),之后才启动PendingIntent配置的PushTracker的onCreate()。
仅Android 11及以下版本出现此问题,Android 12及以上版本
PushTracker按预期优先启动。
临时方案限制
将MainActivity启动模式改为singleInstance可修复问题,但无法进行该修改。
需求
排查Launcher Activity的onStart()被提前调用的原因,当前使用FCM的Data Payload自行处理导航,未使用通知Payload。
附加背景
通过ApplicationLifecycleObserver判断应用前后台状态:
- Android 11及以下版本因Launcher Activity先启动,导致
PushTracker中application_foreground始终为true,不符合预期; - Android 12+版本后台启动时
PushTracker优先启动,application_foreground为false,符合预期。
通知发送代码
private fun sendNotification(remoteMessage: RemoteMessage) { Log.d(TAG, "Message:$remoteMessage") val notification = remoteMessage.notification val requestID = System.currentTimeMillis().toInt() val intent = Intent(this, PushTracker::class.java) intent.putExtra("remoteMessage", Gson().toJson(remoteMessage)) intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK // 注:原代码拼写错误为FLAG_ACTIVTIY_NEW_TASK,此处修正为正确常量名 val pendingIntent = PendingIntent.getActivity(this, requestID, intent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE) val defaultSoundUri: Uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION) val notificationBuilder: NotificationCompat.Builder = NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_1) .setLargeIcon( BitmapFactory.decodeResource( resources, android.R.drawable.ic_lock_idle_alarm ) ) .setSmallIcon(android.R.drawable.ic_lock_lock) .setContentTitle(notification?.title ?: "默认标题") .setContentText(notification?.body ?: "默认内容") .setAutoCancel(true) .setSound(defaultSoundUri) .setContentIntent(pendingIntent) val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as? NotificationManager notificationManager?.notify(0, notificationBuilder.build()) }
MainActivity代码
class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.main) Log.d(TAG, "onStart()"); } override fun onStart() { super.onStart() Log.d(TAG, "onStart()"); } }
MainActivity Manifest配置
<activity android:name=".MainActivity" android:exported="true" android:launchMode="singleTop" android:theme="@style/LaunchTheme"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity>
PushTracker代码
class PushTracker : FragmentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) // 无UI Activity,用于通知预处理及目标页面跳转逻辑 } }
PushTracker Manifest配置
<activity android:name=".PushTracker" android:configChanges="orientation|screenSize" android:noHistory="true" android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" />
内容的提问来源于stack exchange,提问作者Gowtham K K
相关产品推荐
相关产品推荐

