2017-08-31 3 views
1

J'affiche une notification à un moment donné qui ouvrira une activité (activité A) en appuyant sur. J'ai essayé d'enregistrer un récepteur avec un IntentFilter pour écouter l'utilisateur en tapant sur la notification. Ce que je veux, c'est éviter que le système relance l'activité lorsque l'activité A est déjà présente. Malheureusement, le destinataire n'est jamais appelé.RegisterReceiver pour PendingIntent à partir de la notification

C'est le code lancement de la notification:

//Create the BackStack for the back navigation when opening from intent 
TaskStackBuilder builder = TaskStackBuilder.create(this); 

//Create the intent to open this activity, allowing the implementing class add some parameters 
Intent thisClassIntent = new Intent(this, ActivityA.class); 
onPrepareIntent(thisClassIntent); 
thisClassIntent.setAction(ACTION_OPEN_CHAT); 

builder.addParentStack(this); 
builder.addNextIntent(thisClassIntent); 

PendingIntent pendingIntent = builder.getPendingIntent(0, 
PendingIntent.FLAG_UPDATE_CURRENT); 

Notification.Builder builder = new Notification.Builder(context).setContentTitle("some title") 
       .setContentText("some text") 
       .setSmallIcon(R.drawable.icon) 
       .setPriority(Notification.PRIORITY_MAX) 
       .setDefaults(Notification.DEFAULT_ALL) 
       .setAutoCancel(true) 
       .setContentIntent(pendingIntent); 

Notification notification = builder.build(); 
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
     manager.notify(0, notification); 

Dans cette activité, j'ai essayé d'écouter l'action définie dans l'intention:

public class ActivityA extends AppCompatActivity { 

private BroadcastReceiver mReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     //This should be called when the user taps in the notification 
    } 
}; 

@Override 
protected void onStart() { 
    super.onStart(); 
    registerReceiver(mReceiver, new IntentFilter(ACTION_OPEN_CHAT)); 
} 

... 
} 

Si vous avez la moindre idée . Merci!

+0

êtes-vous en train de déclarer l'intention qui activera votre récepteur de diffusion dans votre manifeste android? – Rafa

Répondre

0

Le PendingIntent créé par TaskStackBuilder est destiné aux cibles Activity, et non aux cibles BroadcastReceiver. Si vous souhaitez recevoir votre notification, envoyez une diffusion spécialisée, vous devrez créer votre propre PendingIntent pour un BroadcastReceiver et demander à votre Activity d'enregistrer un récepteur.