我正在 Android 中显示一条通知。这工作正常。现在如果用户单击通知,我希望聚焦 MyApp。我知道我可以将 Intent 传递给通知,这样我就可以启动任何我想要的 Activity 。但我不习惯启动特定 Activity ,我只想返回应用程序。如果应用程序没有运行,那么我想正常启动应用程序,而不是特定的 Activity 。
这三种情况:
A: MyApp 正在后台运行,但没有焦点 -> 聚焦 MyApp
B:MyApp 未运行 ->正常启动MyApp
C:MyApp 正在运行并且也有焦点 -> 不执行任何操作
我的通知代码:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, channelId)
.setContentTitle(title)
.setContentIntent(intent) // -> here I could parse an Intent, I know...
// But that is not exactly what I want
我在 Google 上找不到任何相关内容。这可能吗?
最佳答案
尝试将此作为待处理 Intent :
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
这将:
- 如果应用未运行,请启动该应用。
- 如果在前台,则不会执行任何操作,再次将注意力集中到应用程序。
- 如果在后台,(据我所知,最有可能)将集中应用程序,并且如果已经创建了 Activity ,则不会创建 Activity 。
关于java - 如何在通知点击时返回应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57843074/