Je travaille sur une application GPS. Mon application va déclencher une notification par NotificationManager si l'utilisateur va à l'intérieur ou à l'extérieur d'une zone prédéfinie. Mon application peut déclencher une notification dans les deux cas. De onResume(), j'obtiens toujours la dernière valeur de intent.setExtra() à la place de la valeur intent.setExtra de la notification cliquée.Comment détecter l'expéditeur d'une notification envoyée par NotificationManager/détecter quelle notification a été cliquée sur la barre de notification?
Le problème est de savoir comment détecter l'utilisateur clique sur la notification pour la zone à l'intérieur ou à l'extérieur zone? (Je veux montrer la vue différente sur le cas différent)
Est-il possible d'ajouter un écouteur pour la notification cliquée?
Voici mon code Epinette:
private void displayNotificationMessage(String message, boolean vibrate, boolean playSound, Intent contentIntent, String notificationTag)
{
Notification notification = new Notification(R.drawable.buslogo, message, System.currentTimeMillis());
PendingIntent myPendingIntent = PendingIntent.getActivity(this.getBaseContext(),0, contentIntent,PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(this, "Friendly Reminder", message, myPendingIntent);
contentIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
if (vibrate)
notification.vibrate=new long[] {100L, 100L, 200L, 500L};
if (playSound)
notification.defaults |= Notification.DEFAULT_SOUND;
notification.number = ++notificationCounter;
notificationMgr.notify(notificationTag, notificationCounter, notification);
}
@Override
protected void onNewIntent(Intent intent) {
Log.i(TAG, "*********** onNewIntent(), intent = " + intent);
if (intent.getExtras() != null)
{
Log.i(TAG, "in onNewIntent = " + intent.getExtras().getString("test"));
}
super.onNewIntent(intent);
setIntent(intent);
}
@Override
public void onResume() {
super.onResume();
Log.i(TAG, "*********** Main - onResume()");
Intent intent = this.getIntent();
if (intent.getExtras() != null)
{
Log.i(TAG, "test = " + intent.getExtras().getString("test"));
}
}
public void createNotification(String msnContent)
{
Intent intent = new Intent();
intent.setClass(this, Main.class);
Bundle bundle = new Bundle();
bundle.putString("test", msnContent);
intent.putExtras(bundle);
displayNotificationMessage(msnContent, true, true, intent, "test");
}
Merci, j'ai modifié mon code. Je passe un nouvel objet PendingIntent à notification.setLatestEventInfo() chaque fois avant d'envoyer la notification par notificationMgr.notify(). Mais il ne peut toujours pas distinguer quelle notification à la barre de notification a été cliquée. Puisque onResume() retourne toujours la dernière valeur de setExtra(). –
Est-ce un moyen de passer valeur/objet de l'activité d'origine à l'intention appelée par Notification? –
@ user767987: "Depuis onResume() renvoie toujours la dernière valeur setExtra()." Voir http://stackoverflow.com/questions/6089698/get-the-intent-that-woke-up-my-activity/6089739#6089739 – CommonsWare