7

Notifica in Android che ha lo stesso intento di fare clic. Sto inviando notifiche dopo aver installato il tema. Considera che installo 4 temi e 4 notifiche compaiono nella finestra di notifica, ma quando faccio clic su ciascuna notifica verrà avviata l'attività perticulari ma l'intento è avere gli stessi dati per ogni intenzione.Notifica multipla Android che invia gli stessi dati facendo clic su

mio codice va in questo modo

@SuppressWarnings("deprecation") 
void sendInstalledNotification(String fileName, String packageName) { 
    NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE); 

    String name = ""; 
    try { 
     name += fileName.substring(fileName.lastIndexOf(".") + 1); 
    } catch (Exception e) { 
     Log.e("NewThemeChooser", "Invalid Package name"); 
     e.printStackTrace(); 
    } 
    name += " Installed"; 
    Notification notification = new Notification(R.drawable.ic_launcher_9, name , System.currentTimeMillis()); 

    Intent intent = new Intent(mContext , ThemeInfo.class); 
    Bundle bundle = new Bundle(); 
    bundle.putString("apkid", packageName); 
    bundle.putBoolean("isApplied", false); 
    intent.putExtra("bundle", bundle); 
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0); 
    notification.setLatestEventInfo(mContext, name, "Click to Apply Theme", pendingIntent); 
    notification.flags = Notification.FLAG_AUTO_CANCEL; 
    Log.d("NewThemeChooser__:ThemeChangeReceiver" , "hascode : " + packageName.hashCode() + " installed " + packageName); 
    notificationManager.notify(packageName.hashCode(), notification); 

} 

e sto stampando i dati intenti onCreate di ThemeInfo attività

Bundle bundle = getIntent().getBundleExtra("bundle"); 
    apkid = bundle.getString("apkid"); 
    isApplied = bundle.getBoolean("isApplied", false); 

    System.out.println("NewThemeChooser__:bundle apkid " + apkid); 

Il risultato che sto ottenendo in ceppi è

D/NewThemeChooser__:ThemeChangeReceiver(4423): hascode : -186637114 installed com.test.theme.MiCrease 
D/NewThemeChooser__:ThemeChangeReceiver(4423): hascode : 2106806482 installed com.test.theme.iPhone 
D/NewThemeChooser__:ThemeChangeReceiver(4423): hascode : -1413669305 installed com.test.theme.Simpsons 
D/NewThemeChooser__:ThemeChangeReceiver(4423): hascode : -2146296452 installed com.test.theme.AnnaTheme 
I/System.out(4423): NewThemeChooser__:bundle apkid com.test.theme.MiCrease 
I/System.out(4423): NewThemeChooser__:bundle apkid com.test.theme.MiCrease 
I/System.out(4423): NewThemeChooser__:bundle apkid com.test.theme.MiCrease 
I/System.out(4423): NewThemeChooser__:bundle apkid com.test.theme.MiCrease 

risposta

18

Ho avuto lo stesso problema, e il problema è che Android è un po 'troppo intelligente e ti dà lo stesso PendingIntent s anziché nuovi. Dal docs:

Un errore comune le persone fanno è quello di creare più PendingIntent oggetti con Intent s che variano solo nei loro contenuti "extra", aspettandosi di ottenere un diverso PendingIntent ogni volta. Questo non succede Le parti di Intent utilizzate per la corrispondenza sono le stesse definite da Intent.filterEquals. Se si utilizzano due oggetti Intent equivalenti come da Intent.filterEquals, si otterrà lo stesso PendingIntent per entrambi.

modificare il codice come segue per la fornitura di un unico requestCode:

// ... 
PendingIntent pendingIntent = PendingIntent.getActivity(mContext, packageName.hashCode(), intent, 0); 
// ... 

Questo farà sì che un unico PendingIntent viene utilizzato, in contrasto con la stessa.

Nota che hashCode() potrebbe non essere univoco, quindi se possibile utilizzare un altro numero univoco come requestCode.

+1

In realtà avevo già avuto questa correzione in precedenza ma non si aggiornava. grazie @Oleg –

+0

Wow. bella cattura. + 1 upvote. grazie per la condivisione –

Problemi correlati