【发布时间】:2019-08-18 12:20:05
【问题描述】:
我正在编写一个原生 Android 代码来在按下通知时打开我的应用程序。如果应用程序已经打开(无论是在前台运行还是在后台运行),我想点击通知将应用程序带到最前面,而不是重新启动它,以便保留它的状态。
我尝试了以下代码(仅显示相关代码):
///////// Create an activity on tap (intent)
const Intent = android.content.Intent;
const PendingIntent = android.app.PendingIntent;
// Create an explicit intent for an Activity in your app
const intent = new Intent(context, com.tns.NativeScriptActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_NEW_TASK);
const pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
///////// Creating a notification
var NotificationCompat = android.support.v4.app.NotificationCompat;
const builder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(android.R.drawable.btn_star_big_on)
.setContentTitle(title)
.setContentText(message)
.setStyle(
new NotificationCompat.BigTextStyle()
.bigText("By default, the notification's text content is truncated to fit one line.")
)
.setPriority(NotificationCompat.PRIORITY_HIGH)
// Set the intent that will fire when the user taps the notification
.setContentIntent(pendingIntent)
.setAutoCancel(true);
///////// Show the notification
notificationManager.notify(NOTIFICATION_ID, builder.build());
但这会打开应用程序而不保留其状态。
按照here 的建议,我还尝试模拟按下启动器中的应用程序图标 - 以便将应用程序带到前台并且不会重新创建 Nativescript 活动。
const packageName = context.getPackageName();
console.log('Package name: ',packageName);
const emulateLaunchByAppIconIntent = context.getPackageManager()
.getLaunchIntentForPackage(packageName)
.setPackage(null)
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
const pendingIntent_emulated = PendingIntent.getActivity(context, 0, emulateLaunchByAppIconIntent, 0);
///////// Creating a notification
var NotificationCompat = android.support.v4.app.NotificationCompat;
const builder = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(android.R.drawable.btn_star_big_on)
.setContentTitle(title)
.setContentText(message)
.setStyle(
new NotificationCompat.BigTextStyle()
.bigText("By default, the notification's text content is truncated to fit one line.")
)
.setPriority(NotificationCompat.PRIORITY_HIGH)
// Set the intent that will fire when the user taps the notification
.setContentIntent(pendingIntent_emulated)
.setAutoCancel(true);
///////// Show the notification
notificationManager.notify(NOTIFICATION_ID, builder.build());
这确实使应用程序出现在前面,但没有保留其状态(即使应用程序已经在前台 - 它重新加载了应用程序)。
然后我尝试按下 Nativescript 应用程序图标(手动),当应用程序刚刚被发送到后台时 - 我发现它会重新启动应用程序,而不仅仅是将其带到前台。
我的问题是 - 为什么 Nativescript 应用程序会这样? 如何让 Android 仅将应用程序置于前台而不重新构建新的 nativescript 活动?
【问题讨论】:
-
您是否尝试删除
FLAG_ACTIVITY_NEW_TASK,只保留FLAG_ACTIVITY_RESET_TASK_IF_NEEDED。 -
谢谢,我试试这个
标签: nativescript