英文:
Start Activity is not working in Android 13
问题
Description
我正在使用Firebase消息服务来处理推送通知,在通知被点击时,Intent的OnHandleIntent方法会被调用。我尝试启动闪屏界面的活动,这在Android 10上有效,但在Android 13设备上无效。
不论应用程序处于前台、后台还是被杀死模式,我都能接收到通知。
但是,当应用程序处于后台或被杀死模式时,我点击通知时什么都没有发生。我在后台模式下进行了调试,在点击通知时,它会到达OnHandleIntent方法,在那里我创建了一个闪屏界面的新意图并启动了活动,但活动没有运行,应用程序没有进入前台或调用闪屏活动的代码。
重现步骤
- 创建一个通知意图
- 在通知被点击时,在OnHandleIntent中尝试启动任何活动,无论是闪屏活动还是主活动,它在Android 13设备上都不会运行。
基本信息
- Android: <!-- Android 13.0 API level 33 -->
- 受影响的设备:Android 13设备
代码示例
通知活动
public class NotificationIntentService : IntentService
{
public NotificationIntentService() : base("NotificationIntentService")
{
}
protected override async void OnHandleIntent(Intent intent)
{
try
{
var notificationManager = (NotificationManager)GetSystemService(Context.NotificationService);
notificationManager.Cancel(0);
var action = intent.Extras.GetString("action");
if (string.IsNullOrWhiteSpace(action))
{
action = "default";
}
var newIntent = new Intent(this, typeof(SplashActivity));
if (intent?.Extras != null)
{
newIntent.PutExtras(intent.Extras);
}
newIntent.AddFlags(ActivityFlags.NewTask);
StartActivity(newIntent);
}
catch (System.Exception ex)
{
}
}
}
闪屏活动
[Activity(Theme = "@style/MyTheme.Splash", MainLauncher = true, NoHistory = true)]
public class SplashActivity : AppCompatActivity
{
static readonly string TAG = "X:" + typeof(SplashActivity).Name;
public override void OnCreate(Bundle savedInstanceState, PersistableBundle persistentState)
{
base.OnCreate(savedInstanceState, persistentState);
}
protected override void OnResume()
{
base.OnResume();
var mainActivity = new Android.Content.Intent(Application.Context, typeof(MainActivity));
if (Intent?.Extras != null)
{
mainActivity.PutExtras(Intent.Extras);
}
StartActivity(mainActivity);
}
public override void OnBackPressed() { }
}
期望行为
它应该运行闪屏活动,然后运行主活动,相同的代码在Android 10设备上运行正常,但在Android 13上不行。
实际行为
OnHandleIntent启动活动对于Android 13设备不会运行任何活动。
代码示例
这里是重现问题的示例
https://github.com/aakashsolangi/SampleNotification
英文:
Description
I am using the Firebase Messaging service for handling Push notifications On notification tapped Intent OnHandleIntent method calls I tried to start the activity of Splash screen it works for Android 10 but not for Android 13 devices.
I do receive the notification in all modes whether the app is in foreground, background or killed mode.
But when the app is in the background or killed mode and I tapped the notification it did not do anything. I debug it into background mode and on the Tapped notification it reached onHandleIntent method where I create a new intent of splash screen and start the activity but the activity did not run app did not come into the foreground or call the splash activity code
Steps to Reproduce
- Create a Notification Intent
- On Notification Tapped OnHandleIntent try to start any activity whether the splash activity or Main activity It did not run for Android 13 devices
Basic Information
- Android: <!-- Android 13.0 API level 33 -->
- Affected Devices: Android 13 devices
Code Sample
Notification Activity
public class NotificationIntentService : IntentService
{
public NotificationIntentService() : base("NotificationIntentService")
{
}
protected override async void OnHandleIntent(Intent intent)
{
try
{
var notificationManager = (NotificationManager)GetSystemService(Context.NotificationService);
notificationManager.Cancel(0);
var action = intent.Extras.GetString("action");
if (string.IsNullOrWhiteSpace(action))
{
action = "default";
}
var newIntent = new Intent(this, typeof(SplashActivity));
if (intent?.Extras != null)
{
newIntent.PutExtras(intent.Extras);
}
newIntent.AddFlags(ActivityFlags.NewTask);
StartActivity(newIntent);
}
catch (System.Exception ex)
{
}
}
}
Splash Activity
[Activity(Theme = "@style/MyTheme.Splash", MainLauncher = true, NoHistory = true)]
public class SplashActivity : AppCompatActivity
{
static readonly string TAG = "X:" + typeof(SplashActivity).Name;
public override void OnCreate(Bundle savedInstanceState, PersistableBundle persistentState)
{
base.OnCreate(savedInstanceState, persistentState);
}
protected override void OnResume()
{
base.OnResume();
var mainActivity = new Android.Content.Intent(Application.Context, typeof(MainActivity));
if (Intent?.Extras != null)
{
mainActivity.PutExtras(Intent.Extras);
}
StartActivity(mainActivity);
}
public override void OnBackPressed() { }
}
Expected Behavior
It should run the splash activity which will run the main activity, Same code worked fine for Android 10 devices but not on Android 13
Actual Behavior
OnHandleIntent Start Activity did not run any activity for Android 13 devices
Code Sample
here is the sample to recreate the issue
答案1
得分: 1
你尝试使用针对Android 13设计的新闪屏API了吗?我认为这可能与您的问题有关,或者您需要在捕获处理程序中捕获并记录错误,以查看为什么失败。
我有一个显示如何使用新闪屏API的答案这里。
英文:
Have you tried to use the new splash screen API intended for Android 13? I think that may be related to your problem, or you need to catch and log the error in your catch handler to see why it's failing.
I've got an answer showing how to use the new splash screen API here.
答案2
得分: 1
如果您仔细检查logcat中的错误,您会看到"从YOUR_PACKAGE阻止的间接通知活动启动(蹦床)"。
也许您错过了此更新:
https://developer.android.com/about/versions/12/behavior-changes-12#notification-trampolines
英文:
If you carefully check the error from logcat, you will see "indirect notification activity start (trampoline) from YOUR_PACKAGE blocked"
Maybe you miss this update:
https://developer.android.com/about/versions/12/behavior-changes-12#notification-trampolines
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论