PendingIntent 不适用于 Android O

PendingIntent is not working on Android O

我的应用程序中有下载通知。我通过调用 addAction() 方法将 "Cancel" 按钮添加到 NotificationCompat.Builder。但是按钮在 Android O 设备上不起作用。当我按下 "Cancel" 按钮时,没有任何反应。但是按钮在 Android < O.

上工作


我的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);

我的 PendingIntent :

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

我还有 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();
        }
    }
}

Manifest 文件中我有:

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

从不 使用隐式 Intent 而显式 Intent 可以。 Android O 通过禁止接收来自清单注册接收器的隐式 Intent 广播来帮助实施这一点。

第 1 步:从 <receiver> 中删除 <intent-filter>(这也意味着您可以删除 android:exported="false",因为这是现在的默认值)

第 2 步:将 new Intent("CANCEL_DOWNLOAD").putExtra("id", id) 替换为 new Intent(context, NotificationReceiver.class).putExtra("id", id)

非常感谢 CommonsWare。这个对我有用。这是我的代码

    var notifyIntent = Intent(context, NotificationBroadcastReceiver::class.java)
                notifyIntent.action = "ACTION_UPDATE_BADGE"//hardcode in manifest

                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                    // only for oreo and newer versions
                    notifyIntent = Intent(context, NotificationBroadcastReceiverAndroidO::class.java)
                }
val pendingIntent = PendingIntent.getBroadcast(context, it, notifyIntent, PendingIntent.FLAG_ONE_SHOT)

清单中

<receiver android:name=".firebase.NotificationBroadcastReceiver">
            <intent-filter>
                <action android:name="ACTION_UPDATE_BADGE" />
            </intent-filter>
        </receiver>

        <receiver android:name=".firebase.NotificationBroadcastReceiverAndroidO">
            <!--Android O not working when having this code
            <intent-filter>
                <action android:name="ACTION_UPDATE_BADGE" />
            </intent-filter>-->
        </receiver>

创建 2 类:

open class NotificationBroadcastReceiver : BroadcastReceiver() {...}
open class NotificationBroadcastReceiverAndroidO : NotificationBroadcastReceiver() {/*do nothing*/}