2017-09-21 2 views
0

Je vois beaucoup de messages sur stackoverflow mais je ne trouve pas de solution à mon problème.L'intention en attente sur les vues distantes ne fonctionne pas

J'ai une notification avec une vue de contenu personnalisée avec un bouton lié à un RemoteViews. J'ai suivi ce lien (Adding button action in custom notification) pour attacher une action à mon bouton, mais mon BroadcastReceiver n'est jamais viré. Le code:

private void createNotification(int index){ 
    final int id = index; 
    RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification); 
    contentView.setInt(R.id.container,"setBackgroundColor",ContextCompat.getColor(this,BG_COLORS[index])); 
    contentView.setImageViewResource(R.id.logo,BUTTON_OFF[index]); 
    contentView.setTextViewText(R.id.sound, getString(SOUND_NAME[index])); 

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) 
      .setSmallIcon(R.drawable.ic_launcher) 
      .setContent(contentView); 

    Intent notificationIntent = new Intent(this, MainActivity.class); 
    PendingIntent contentIntent = PendingIntent.getActivity(this, id, notificationIntent, 0); 

    Notification notification = mBuilder.build(); 
    notification.contentIntent = contentIntent; 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 

    Intent closeButton = new Intent(this,StopReceiver.class); 
    closeButton.setAction(StopReceiver); 
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, closeButton,0); 
    contentView.setOnClickPendingIntent(R.id.stop,pendingIntent); 


    mNotificationManager.notify(id, notification); 


} 

    public static class StopReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 
      //Never called 
     Log.d(LOGTAG,"Received Cancelled Event"); 
     ... 
    } 
} 

J'ai également déclaré mon récepteur dans mon AndroidManifest.xml:

<receiver android:name="StopReceiver" android:enabled="true" /> 

Merci.

Répondre

0

Vous définissez setOnClickPendingIntent des RemoteViews après l'avoir fourni en tant que RemoteView personnalisé de la notification. Essayez de définir setOnClickPendingIntent avant d'appeler setContent. E.g .:

RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification); 
contentView.setInt(R.id.container,"setBackgroundColor",ContextCompat.getColor(this,BG_COLORS[index])); 
contentView.setImageViewResource(R.id.logo,BUTTON_OFF[index]); 
contentView.setTextViewText(R.id.sound, getString(SOUND_NAME[index])); 

Intent closeButton = new Intent(this,StopReceiver.class); 
closeButton.setAction(StopReceiver); 
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, closeButton,0); 
contentView.setOnClickPendingIntent(R.id.stop,pendingIntent); 

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) 
    .setSmallIcon(R.drawable.ic_launcher) 
    .setContent(contentView);