2012-09-23 10 views
8

Ciao Voglio mostrare tutte le notifiche in un'unica vista .. e voglio aggiornare il numero di notifiche nella barra di stato ... il suo aggiornamento di tutte le informazioni ma mostra il numero sempre 1 .. per favore dimmi come per risolverlo ...Come aggiornare il numero di notifica

@Override 
public void onReceive(Context context, Intent intent) 
{ 
    //Random randGen = new Random(); 
    //int notify_id = randGen.nextInt(); 
    NotificationManager notificationManager = (NotificationManager) 
     context.getSystemService(Activity.NOTIFICATION_SERVICE); 
    String title = intent.getStringExtra(TableUtils.KEY_TITLE); 
    String occasion = intent.getStringExtra(TableUtils.KEY_OCCASION); 
    Notification notification = 
     new Notification(R.drawable.icon, "Love Cardz" , 
         System.currentTimeMillis()); 
    // notification.vibrate = new long[]{100,250,300,330,390,420,500}; 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 
    notification.number+=1; 
    Intent intent1 = new Intent(context, ThemesBrowserActivity.class); 
    PendingIntent activity = 
     PendingIntent.getActivity(context, 1 , intent1, 
            PendingIntent.FLAG_UPDATE_CURRENT); 
    notification.setLatestEventInfo(context, occasion, title, activity); 
    notificationManager.notify(1, notification); 
} 

risposta

18

Devi tenere traccia del conteggio. Si potrebbe estendere la classe di applicazione:

public class AppName extends Application { 
    private static int pendingNotificationsCount = 0; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
    } 

    public static int getPendingNotificationsCount() { 
     return pendingNotificationsCount; 
    } 

    public static void setPendingNotificationsCount(int pendingNotifications) { 
     pendingNotificationsCount = pendingNotifications; 
    } 
} 

E si dovrebbe modificare l'OnReceive:

@Override 
public void onReceive(Context context, Intent intent) { 
    ... 
    int pendingNotificationsCount = AppName.getPendingNotificationsCount() + 1; 
    AppName.setPendingNotificationsCount(pendingNotificationsCount); 
    notification.number = pendingNotificationsCount; 
    ... 
} 

E si potrebbe azzerare il conteggio quando l'utente apre la notifica:

AppName.setPendingNotificationsCount(0); 
+12

abbastanza ridicolo come il framework non ha una semplice chiamata 'getNotifications (int id)' per controllare semplicemente questo ... – clu

+5

Sfortunatamente se l'applicazione è stata uccisa, il contatore si resetta ... Probabilmente dovrebbe salvare in SharedPreference per la persistenza – xnagyg

+1

Come puoi sapere quando è stata aperta la notifica per resettare il contatore ...? – Micro

4

Questo è il mio codice e funziona. Ho provato solo su vecchie versioni di Android. Sospetto che sulle versioni più recenti il ​​badge "numero" sia stato reso invisibile, ma non ho avuto la possibilità di testarlo.

void notifyMsgReceived(String senderName, int count) { 
    int titleResId; 
    String expandedText, sender; 

    // Get the sender for the ticker text 
    // i.e. "Message from <sender name>" 
    if (senderName != null && TextUtils.isGraphic(senderName)) { 
     sender = senderName; 
    } 
    else { 
     // Use "unknown" if the sender is unidentifiable. 
     sender = getString(R.string.unknown); 
    } 

    // Display the first line of the notification: 
    // 1 new message: call name 
    // more than 1 new message: <number of messages> + " new messages" 
    if (count == 1) { 
     titleResId = R.string.notif_oneMsgReceivedTitle; 
     expandedText = sender; 
    } 
    else { 
     titleResId = R.string.notif_missedCallsTitle; 
     expandedText = getString(R.string.notif_messagesReceivedTitle, count); 
    } 

    // Create the target intent 
    final Intent intent = new Intent(this, TargetActivity.class); 
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    final PendingIntent pendingIntent = 
     PendingIntent.getActivity(this, REQUEST_CODE_MSG_RECEIVED, intent, PendingIntent.FLAG_UPDATE_CURRENT); 

    // Build the notification 
    Notification notif = new Notification(
     R.drawable.ic_notif_msg_received, // icon 
     getString(R.string.notif_msgReceivedTicker, sender), // tickerText 
     System.currentTimeMillis()); // when 
     notif.setLatestEventInfo(this, getText(titleResId), expandedText, pendingIntent); 
    notif.number = count; 
    notif.flags = Notification.FLAG_AUTO_CANCEL; 

    // Show the notification 
    mNotificationMgr.notify(NOTIF_MSG_RECEIVED, notif); 
} 

È anche facile aggiornare la notifica in seguito: è sufficiente chiamare nuovamente il metodo con nuovi valori. Il numero verrà visualizzato nel badge dell'icona di notifica se e solo se è stato maggiore di zero al momento della creazione della notifica.

Analogamente, il badge del numero non verrà nascosto (il numero lo sarà, se si imposta il numero su un numero inferiore a 1. Forse cancellando la notifica prima di ri-mostrarlo potrebbe risolverlo.

0

Devi tenere traccia del conteggio. L'incremento che si sta tentando di eseguire su notif.number non funziona, poiché tale stato non è disponibile (ad esempio, se notif.number è sempre 0, quindi lo si incrementa a 1). Tenere traccia del numero da qualche parte nella tua app (preferenze condivise, forse), e di incremento e conservarla lì, poi quando si genera la notifica, impostare

notif.number = myStoredValueForWhatShouldShowInNumber; 

Dare che una prova.

Problemi correlati