我正在开发一个使用FCM在后台接收通知的android库。点击通知后,应将用户重定向到上次打开的活动 - 而不是启动活动。我使用标志FLAG_ACTIVITY_NEW_TASK
来完成这个,并且应用程序在两种情况下按预期工作:1。如果我在android studio中直接通过USB构建应用程序,并且2.如果我下载并安装应用程序的.apk,则杀死应用程序,然后重新启动它。
但是,如果我将应用程序安装为.apk,我会收到以下行为:
1.应用程序在启动活动A上打开。
2.我导航到活动B并将应用程序放在后台
3.我收到通知并点按它。应用程序按活动A打开,而不是按预期打开活动B.
我不明白将应用程序安装为apk并通过usb安装它之间的区别。活动中是否存在一些差异,使FLAG_ACTIVITY_NEW_TASK
将其视为一项单独的活动?这是我的代码:
Intent packageIntent = applicationContext.getPackageManager().getLaunchIntentForPackage(packageName);
String mainClassName = packageIntent.getComponent().getClassName();
Intent intent = new Intent(applicationContext, NotificationTapService.class);
(请注意,我设置启动服务以重新打开上一个活动的中间意图的原因是该服务还调用了一些不相关的分析功能)
PendingIntent pendingIntent = PendingIntent.getService(applicationContext, requestCode, intent, PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(applicationContext, applicationContext.getString(R.string.channel_id))
.setSmallIcon(notificationResourceId)
.setContentTitle(messageTitle)
.setContentText(messageBody)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(messageBody))
.setSound(defaultSoundUri)
.setVibrate(new long[] {1000, 1000, 1000, 1000, 1000})
.setLights(Color.BLUE, 3000, 3000)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_MAX);
NotificationManager notificationManager = (NotificationManager) applicationContext.getSystemService(applicationContext.NOTIFICATION_SERVICE);
notificationManager.notify(messageId, notificationBuilder.build());
(设置重新打开后台堆栈顶部活动的意图)(来自NotificationTapService)
Intent activityIntent = new Intent(applicationContext, Class.forName(intent.getExtras().getString(CLASS_NAME)));
activityIntent.setAction(Intent.ACTION_MAIN);
activityIntent.addCategory(Intent.CATEGORY_LAUNCHER);
activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(activityIntent);
你试试这个链接[Resume application and stack from notification与属性android:launchMode="singleTask"