如何阻止通知重新启动活动

时间:2014-01-09 14:19:54

标签: android notifications

我有以下代码:

public void notify(int notificationId, int iconId, String message, Class<?> returnTo, boolean onGoing, boolean showTime, boolean autoCancel)
{
    Intent intent = new Intent(mContext, returnTo);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(mContext);
    stackBuilder.addParentStack(returnTo);
    stackBuilder.addNextIntent(intent);

    PendingIntent pending = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    mNotificationBuilder.setContentTitle("Notification")
                        .setContentText(message)
                        .setSmallIcon(iconId)
                        .setContentIntent(pending)
                        .setAutoCancel(autoCancel);

    if (!showTime)
    {
        mNotificationBuilder.setWhen(0);
    }

    Notification notification = mNotificationBuilder.build();

    if (onGoing)
    {
        notification.flags |= Notification.FLAG_ONGOING_EVENT;
    }

    mNotificationManager.notify(NOTIFICATION_TAG, notificationId, notification);
}

完全可以创建通知。

然而,调用此函数的Activity(即Class<?> returnTo参数)第二次启动,而不是在按下通知时返回初始实例。

为了避免这种情况,我添加了标志Intent.FLAG_ACTIVITY_CLEAR_TOP&amp;我在函数顶部创建的Intent Intent.FLAG_ACTIVITY_SINGLE_TOP。可悲的是,这没用,所以我尝试了别的东西。

我将android:launchMode="singleTop"添加到调用此代码的Activity中,看看是否有帮助。但遗憾的是,这也没有用。

我已经尝试在谷歌上找到任何东西,但所有想到的都是旗帜(如上所述,我已经尝试过)和launchMode(如上所述,也尝试过)。

那么,我可以尝试其他任何标志,设置吗?

编辑:

Android开发者网站link上的android:launchMode="singleTop"上似乎有一些文字。

这表明:

In other circumstances — for example, if an existing instance of the "singleTop" activity is in the target task, but not at the top of the stack, or if it's at the top of a stack, but not in the target task — a new instance would be created and pushed on the stack.

使用TaskStackBuilder来获取关于PendingIntent的{​​{1}}是否有问题?

2 个答案:

答案 0 :(得分:0)

也许尝试这样的事情:

@SuppressLint("NewApi")
public static void showNotification(Context context, String message) {
    Intent intent = new Intent(context, MainActivity.class);
    // Ensures navigating back to activity, then to Home screen.
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    // Add the activity parent chain to the stack builder.
    stackBuilder.addParentStack(MainActivity.class);
    // Add a new Intent to the task stack.
    stackBuilder.addNextIntent(intent);     
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    // Create notification.
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) { // API 16 onwards
        Notification.Builder builder = new Notification.Builder(context)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent)
            .setContentTitle(context.getString(R.string.app_name))
            .setSmallIcon(R.drawable.ic_notifier)
            .setTicker(message)
            .setContentText(message)
            .setPriority(Notification.PRIORITY_HIGH)
            .setStyle(new Notification.BigTextStyle().bigText(message)) 
            .setWhen(System.currentTimeMillis());
            mNotificationManager.notify(NOTIFICATION_DEFAULT, builder.build());         
    } else {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent)
            .setContentTitle(context.getString(R.string.app_name))
            .setSmallIcon(R.drawable.ic_notifier)
            .setTicker(message)
            .setContentText(message)
            .setPriority(Notification.PRIORITY_HIGH)
            .setWhen(System.currentTimeMillis());
        mNotificationManager.notify(NOTIFICATION_DEFAULT, builder.build());         
    }
}

答案 1 :(得分:0)

正如我在编辑中所怀疑的那样,TaskStackBuilder是罪魁祸首。

我改变了我的代码:

TaskStackBuilder stackBuilder = TaskStackBuilder.create(mContext);
stackBuilder.addParentStack(returnTo);
stackBuilder.addNextIntent(intent);

为:

PendingIntent pending = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

在此之后,通知按照我希望的方式运行。