0

J'ai une notification de téléchargement dans mon application. J'ai ajouté le bouton "Annuler" à NotificationCompat.Builder en appelant la méthode addAction(). Mais le bouton ne fonctionne pas sur le périphérique Android O. Lorsque j'appuie sur le bouton "Annuler", rien ne se passe. Mais bouton de travail sur Android < O.PendingIntent ne fonctionne pas sur Android O

Example Screenshot


Mon Notification:

NotificationCompat.Builder notification = new NotificationCompat.Builder(context, channelId) 
      .setContentTitle(title) 
      .setSmallIcon(R.drawable.network_download) 
      .setContentText(contentText) 
      .setOngoing(true) 
      .setContentIntent(null) 
      .addExtras(idBundle) 
      .addAction(R.drawable.cancel, context.getString(R.string.cancel), getCancelPendingIntent(context, id)) 
      .setProgress(100, 30, true); 

Mon PendingIntent:

private PendingIntent getCancelPendingIntent(Context context, int id){ 
    return PendingIntent.getBroadcast(
      context, id, new Intent("CANCEL_DOWNLOAD").putExtra("id", id), PendingIntent.FLAG_UPDATE_CURRENT); 
} 

Aussi je NotificationReceiver:

public static class NotificationReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     String action = intent.getAction(); 
     if ("CANCEL_DOWNLOAD".equals(action) && context != null){ 
      int id = intent.getIntExtra("id", -1); 
      NotificationManager mgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
      if (mgr != null) 
       mgr.cancel(id); 
      FtpManager.getInstance(new AppExecutors(), CredentialsManager.getInstance().getCredentials(context)) 
        .cancelDownloading(); 
     } 
    } 
} 

Dans Manifest fichier J'ai:

<receiver 
     android:name="eu.warble.pjapp.util.NotificationsManager$NotificationReceiver" 
     android:exported="false"> 
     <intent-filter> 
      <action android:name="CANCEL_DOWNLOAD" /> 
     </intent-filter> 
</receiver> 

Répondre

3

Ne jamais utiliser unIntent implicite lorsqu'un Intent fera explicite. Android O contribue à renforcer cela en interdisant la réception d'émissions Intent implicites à partir de destinataires enregistrés.

Étape # 1: Retirez le <intent-filter> de votre <receiver> (ce qui signifie que vous pouvez vous débarrasser de android:exported="false", comme cela est maintenant la valeur par défaut)

Étape # 2: Remplacer new Intent("CANCEL_DOWNLOAD").putExtra("id", id) avec new Intent(context, NotificationReceiver.class).putExtra("id", id)