0

Ich habe Download-Benachrichtigung in meiner Anwendung. Ich habe die Schaltfläche "Abbrechen" zu NotificationCompat.Builder durch Aufruf addAction() Methode hinzugefügt. Aber Taste funktioniert nicht auf Android O-Gerät. Wenn ich die Taste "Abbrechen" drücke, passiert nichts. Aber Taste funktioniert auf Android < O.PendingIntent funktioniert nicht auf Android O

Example Screenshot


Mein 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); 

Mein PendingIntent:

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

Auch ich habe 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(); 
     } 
    } 
} 

In Manifest Datei ich habe:

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

Antwort

3

Nie Verwendung eine implizite Intent wenn eine explizite Intent tun. Android O unterstützt dies, indem es den Empfang impliziter Intent Broadcasts von Manifest-registrierten Empfängern verbietet.

Schritt 1: Entfernen Sie die <intent-filter> von Ihrem <receiver> (was auch bedeutet, dass Sie von android:exported="false" loswerden konnte, wie das jetzt der Standardwert ist)

Schritt # 2: Ersetzen new Intent("CANCEL_DOWNLOAD").putExtra("id", id) mit new Intent(context, NotificationReceiver.class).putExtra("id", id)

Verwandte Themen