[英]Launch activity using pending intent when app running in background
I have some problem how to launch DetailActivity using pending intent on a notification received when the app is running in the background, it's always launching a MainActivity ( but when the app is running in the foreground its work fine ) 我有一些问题,如何在应用程序在后台运行时如何使用收到的通知的待定意图来启动DetailActivity ,它始终会启动MainActivity (但是当应用程序在前台运行时其工作正常)
Everybody can help me please and sorry for my bad English 每个人都可以帮助我,对我英语不好的人感到抱歉
notificationIntent
notificationIntent
val intent = Intent(this, DetailActivity::class.java)
intent.action = Intent.ACTION_MAIN
intent.addCategory(Intent.CATEGORY_LAUNCHER)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
PendingIntent
的PendingIntent
val pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT)
manifest.xml
的manifest.xml
<application
android:name=".helper.App"
android:icon="@drawable/ic_main_app"
android:label="@string/app_name"
android:roundIcon="@drawable/ic_main_app"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true"
tools:ignore="GoogleAppIndexingWarning"
tools:targetApi="m">
<activity
ndroid:screenOrientation="portrait"
android:theme="@style/Theme.MaterialComponents.Light.NoActionBar"
android:name=".ui.activity.aduan.DetailActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="kominfo.com.umat.ui.activity.main.MainActivity"/>
</activity>
<activity
android:screenOrientation="portrait"
android:name=".ui.activity.main.MainActivity"
android:launchMode="singleTop"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
you have set to your DetailActivity
in Manifest
您已在
Manifest
设置为DetailActivity
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="kominfo.com.umat.ui.activity.main.MainActivity"/>
This is the reason why your DetailActivity
needs MainActivity
这就是您的
DetailActivity
需要MainActivity
的原因
Also try to add following Flags to your Intent 还要尝试将以下标志添加到您的意图中
Intent.FLAG_ACTIVITY_CLEAR_TOP
Intent.FLAG_ACTIVITY_SINGLE_TOP
and for Notification
和
Notification
notification.flags |= Notification.FLAG_AUTO_CANCEL
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.