目标:我的目标是在用户点击通知时将Android活动启动到前台。如果task of the app已打开,则点击通知会关闭通知抽屉,并且活动会按预期在前台打开。
问题:但是,如果未打开应用程序的任何任务,则在点击通知时,通知抽屉将保留在前台,而活动仅在后台打开。
问题:无论应用程序是否已打开任务,如何告诉Android关闭通知抽屉并将打开的活动置于前台?
代码段:
val intent = Intent(context, SplashActivity::class.java)
intent.addFlags(
Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_SINGLE_TOP
)
val pendingIntent = PendingIntent.getActivity(context, 0, intent, 0)
NotificationCompat.Builder(context, channelId)
.build(title, text, icon, pendingIntent)
您可以使用PendingIntent
进行此操作。
请参阅Start an Activity from a Notification的操作方法。希望这会有所帮助。
如果我没错,您想在活动打开时关闭通知抽屉。因此,在通过通知打开活动时,请在代码中添加这两行。
Intent x = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
sendBroadcast(x);