簡體   English   中英

按通知會兩次打開相同的活動

[英]pressing on notification opens the same activity twice

如果我有一個由用戶關閉的活動(用戶在家中按下以便應用程序仍處於應用程序堆棧中)然后他收到通知,當他按下它時我開始活動,現在相同的活動打開兩次。 我怎樣才能防止這種情況發生?

我的代碼:

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, 
        new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);

NotificationManager mNotificationManager = (NotificationManager)
        this.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.logo)
            .setContentTitle("Title")
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
            .setAutoCancel(true)
            .setLights(GCMSIntentService.PURPLE, 500, 500)
            .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
            .setContentText(msg);

mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(1, mBuilder.build());
android:launchMode="singleTask"

在清單中或者,將其用作您意圖的標志。

通過android:launchMode="singleTask" ,如果活動的實例已存在於單獨的任務中,系統會通過調用其onNewIntent()方法將意圖路由到現有實例,而不是創建新實例並添加標志意圖

FLAG_ACTIVITY_NEW_TASK,FLAG_ACTIVITY_CLEAR_TOP,FLAG_ACTIVITY_SINGLE_TOP

引用任務和后台堆棧

您需要添加FLAG_ACTIVITY_CLEAR_TOPFLAG_ACTIVITY_SINGLE_TOPFLAG_ACTIVITY_NEW_TASK標志,以您的通知intent.So變化

 PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);

Intent notificationIntent = new Intent(this, MainActivity.class);

notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);


PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent , PendingIntent.FLAG_UPDATE_CURRENT);

這是錯誤。

每次都必須更改通知函數id。 你每次都給它1。 那就是錯誤。

mNotificationManager.notify(1,mBuilder.build());

每次給一個不同的id。 它會像魅力一樣工作。

例如,使用sharedPrefences每次都提供不同的id。

檢查一下,

     PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);

            NotificationManager mNotificationManager = (NotificationManager)
                    this.getSystemService(Context.NOTIFICATION_SERVICE);
            NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(this)
                            .setSmallIcon(R.drawable.logo)
                            .setContentTitle("Title")
                            .setStyle(new NotificationCompat.BigTextStyle()
                                    .bigText(msg))
                            .setAutoCancel(true)
                            .setLights(GCMSIntentService.PURPLE, 500, 500)
                            .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                            .setContentText(msg);

            mBuilder.setContentIntent(contentIntent);

 SharedPreferences sp = context.getSharedPreferences("randomidfornotification", Activity.MODE_PRIVATE);
        myIntValue = sp.getInt("notificationid", 0);

mNotificationManager.notify(1, mBuilder.build());

SharedPreferences sp1 = context.getSharedPreferences("randomidfornotification", Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = sp1.edit();
        editor.putInt("notificationid", myIntValue+1);
        editor.apply();

暫無
暫無

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

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