我向用户发送推送通知,点击它时会打开应用。
我的问题是,当应用程序已经打开时,点击通知会再次启动应用程序。
我只希望它启动应用程序,如果它尚未运行。
我在通知中使用Pending Intent:
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, Splash.class), 0);
我看到帖子说使用:
<activity
android:name=".Splash"
android:launchMode="singleTask"
但问题是我的正在运行的应用程序正在运行其他活动,然后在应用程序启动后7秒内完成启动,因此当应用程序运行时,Splash不是当前活动
答案 0 :(得分:26)
为您的应用使用“启动Intent
”,如下所示:
PackageManager pm = getPackageManager();
Intent launchIntent = pm.getLaunchIntentForPackage("your.package.name");
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, launchIntent, 0);
将“your.package.name”替换为Android清单中包的名称。
此外,您应该从清单中删除特殊launchMode="singleTask"
。标准的Android行为会做你想要的。
答案 1 :(得分:3)
String appPackageName = "";
private void isApplicationInForeground() throws Exception {
ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
final List<ActivityManager.RunningAppProcessInfo> processInfos = am
.getRunningAppProcesses();
ActivityManager.RunningAppProcessInfo processInfo = processInfos
.get(0);
// for (ActivityManager.RunningAppProcessInfo processInfo : processInfos) {
if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
// getting process at 0th index means our application is on top on all apps or currently open
appPackageName = (Arrays.asList(processInfo.pkgList).get(0));
}
// }
}
else {
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = null;
componentInfo = taskInfo.get(0).topActivity;
appPackageName = componentInfo.getPackageName();
}
}
private void notifyMessage(String text) {
if (appPackageName.contains("com.example.test")) {
// do not notify
}
else {
// create notification and notify user
}
}
答案 2 :(得分:3)
对于那些使用 Xamarin.Android 的人。 Xamarin版 David Wasser 的答案如下:
//Create notification
var notificationManager = GetSystemService(Context.NotificationService) as NotificationManager;
Intent uiIntent = PackageManager.GetLaunchIntentForPackage("com.company.app");
//Create the notification
var notification = new Notification(Android.Resource.Drawable.SymActionEmail, title);
//Auto-cancel will remove the notification once the user touches it
notification.Flags = NotificationFlags.AutoCancel;
//Set the notification info
//we use the pending intent, passing our ui intent over, which will get called
//when the notification is tapped.
notification.SetLatestEventInfo(this, title, desc, PendingIntent.GetActivity(this, 0, uiIntent, PendingIntentFlags.OneShot));
//Show the notification
notificationManager.Notify(0, notification);
答案 3 :(得分:1)
不要在通知点击时显示Splash活动,而是显示您的MainActivity,因为您的启动活动将在一段时间后关闭,但MainActivity将保持打开状态
<activity
android:name=".MainActivity"
android:launchMode="singleTask"
答案 4 :(得分:1)
使用Splash作为片段而不是Activity。保持Splash片段(7秒),将其替换为所需的片段(着陆页)。
将launchMode =“singleTask”添加到清单。
正如Rahul已经说明的那样,如果应用程序已在运行onNewIntent()
onCreate()
@Override
protected void onNewIntent(Intent intent)
{
super.onNewIntent(intent);
}
OR
使用David的答案,似乎很有希望。
答案 5 :(得分:0)
当点击通知并且您的代码重定向到您想要的屏幕时,只需通过调用此方法替换该代码并重定向到&#34; true / false&#34;结果基础。
private boolean isAppOnForeground(Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
if (appProcesses == null) {
return false;
}
final String packageName = context.getPackageName();
for (RunningAppProcessInfo appProcess : appProcesses) {
if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName)) {
return true;
}
}
return false;
}
答案 6 :(得分:0)
Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT
也许不要启动Splash Activity并重新打开(带到前面)MainActivity并用一个监听器更新UI,告诉你,你有一个新的通知(带有一个标志 - 布尔值或带有一个接口来制作听众)。
答案 7 :(得分:0)
您可以使用有序广播来完成此任务。
1)将PendingIntent
更改为开始BroadcastReceiver
,这将决定是开始活动还是不做任何事情:
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(this, DecisionReceiver.class), 0);
2)创建决策BroadcastReceiver
:
public class DecisionReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
context.sendOrderedBroadcast(new Intent(MainActivity.NOTIFICATION_ACTION), null, new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (getResultCode() == MainActivity.IS_ALIVE) {
// Activity is in the foreground
}
else {
// Activity is not in the foreground
}
}
}, null, 0, null, null);
}
}
3)在你的活动中创建一个BroadcastReceiver
,表示它还活着:
public static final String NOTIFICATION_ACTION = "com.mypackage.myapplication.NOTIFICATION";
public static final int IS_ALIVE = 1;
private BroadcastReceiver mAliveReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
setResultCode(IS_ALIVE);
}
};
// Register onResume, unregister onPause
// Essentially receiver only responds if the activity is the foreground activity
@Override
protected void onResume() {
super.onResume();
registerReceiver(mAliveReceiver, new IntentFilter(NOTIFICATION_ACTION));
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(mAliveReceiver);
}
答案 8 :(得分:0)
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
[RequiredIf]
答案 9 :(得分:-1)
尝试将此添加到您的意图,如果它在后台运行,则将活动置于前面
Intent intent = new Intent(this, Splash.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
答案 10 :(得分:-2)
首先在清单文件的android:taskAffinity="com.example.testp.yourPreferredName"
元素中设置默认任务Application
。在android:launchMode="singleTask"
上维护SplashActivity
。现在,由于您的SplashActivity是您的主要条目,因此请将此代码添加到onResume()
,onNewIntent()
和onCreate()
(在第二个想法onResume()不建议的情况下) - 按照代码中的注释
//Note these following lines of code will work like magic only if its UPVOTED.
//so upvote before you try it.-or it will crash with SecurityException
ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(1000);
for(int i =0; i< taskInfo.size(); i++){
String PackageName = taskInfo.get(i).baseActivity.getPackageName();
if(PackageName.equals("packagename.appname")){// suppose stackoverflow.answerer.Elltz
//if the current runing actiivity is not the splash activity. it will be 1
//only if this is the first time your <taskAffinity> is be called as a task
if(taskInfo.get(i).numActivities >1){
//other activities are running, so kill this splash dead!! reload!!
finish();
// i am dying in onCreate..(the user didnt see nothing, that's the good part)
//about this code. its a silent assassin
}
//Operation kill the Splash is done so retreat to base.
break;
}
}
此代码不适用于api 21+
;要使其工作,您需要使用AppTask,这将为您节省额外的代码行,因为您不会在循环中找到Task
。
希望有所帮助