簡體   English   中英

如何防止通知重新啟動活動

[英]How to prevent a notification to re-launch an Activity

我有以下代碼:

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_TOPIntent.FLAG_ACTIVITY_SINGLE_TOP添加到了在函數頂部創建的Intent中。 可悲的是,這沒有用,所以我嘗試了其他方法。

我在調用此代碼的Activity中添加了android:launchMode="singleTop" ,以查看是否有幫助。 但可悲的是,這也不起作用。

我試圖在Google上找到任何東西,但所附帶的只是標志(如前所述,我已經嘗試過)和launchMode(如前所述,也曾嘗試過)。

因此,我可以嘗試其他標志,設置等嗎?

編輯:

在Android開發者網站上的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來獲取我的PendingIntentor if it's at the top of a stack, but not in the target task是否可能存在問題?

也許嘗試這樣的事情:

@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());         
    }
}

正如我在Edit中所懷疑的TaskStackBuilder ,罪魁禍首是TaskStackBuilder

我從以下位置更改了代碼:

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

至:

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM