J'ai inclus un identifiant unique pour créer PendingIntent ainsi que dans la méthode mNM.notify(). Lorsque je mets deux notifications à afficher en même temps, elles ne s'affichent pas simultanément. La première notification est affichée avec l'heure donnée pour la deuxième notification. Beaucoup de gens ont eu ce problème et la seule suggestion était de donner des identifiants uniques. Mais ça ne marche pas! S'il vous plaît aidez. Voici ma méthode showNotification().Pourquoi plusieurs notifications ne sont-elles pas affichées simultanément?
private void showNotification() {
/*create intent for show notification details when user clicks notification*/
Intent intent =new Intent(getApplicationContext(), MainActivity.class);
Random random = new Random();
int id = random.nextInt();
intent.setData((Uri.parse("custom://"+System.currentTimeMillis())));
// This is the 'title' of the notification
CharSequence title = "Reminder!" + id;
// This is the icon to use on the notification
int icon = R.drawable.ic_dialog_alert;
// This is the scrolling text of the notification
CharSequence text = task;
// What time to show on the notification
long time = System.currentTimeMillis();
Notification notification = new Notification(icon, text, time);
// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this,id, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
// Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(this, title, text, contentIntent);
// Clear the notification when it is pressed
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Send the notification to the system.
mNM.notify((int)System.currentTimeMillis(), notification);
// Stop the service when we are finished
stopSelf();
}
Vous n'avez pas besoin de 'getApplicationContext()'; utilisez 'this'. Je n'ai aucune idée de ce que 'custom: //' est supposé faire. Vous devriez vraiment envisager d'utiliser 'Notification.Builder' ou' NotificationCompat.Builder' plutôt que de lancer 'Notification' manuellement. 'Intent.FLAG_ACTIVITY_NEW_TASK' n'est pas un indicateur valide pour' getActivity() 'sur PendingIntent'. Et montrer deux 'Notifications 'n'est généralement pas une bonne idée en premier lieu - montrer un' Notification' avec des détails sur les multiples événements. – CommonsWare
Je suis d'accord que vous devriez utiliser le constructeur et deux notifications est un anti-modèle. Plus précisément, une application devrait avoir une notification qui indique combien de notifications sont en attente Mais il n'y a rien de mal à être un individu lorsque vous développez alors continuez. Je prévois en fait une notification de couleur qui est également une violation directe de '[motif de conception pour les notifications.] (Http://developer.android.com/design/patterns/notifications.html) – danny117