的PendingIntent不工作在AndroidØ的PendingIntent不工作在Androi

2019-05-12 08:48发布

我在我的应用程序下载通知。 我已经添加了“取消”按钮NotificationCompat.Builder通过调用addAction()方法。 但是按键不工作在Android O设备。 当我按下“取消”按钮,没有任何反应。 但是按钮在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>

Answer 1:

切勿使用隐式Intent时,明确的Intent就行了。 安卓Ø有助于通过禁止在收到隐含的强制实施此Intent从清单注册的接收器广播。

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

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



Answer 2:

非常感谢您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*/}


文章来源: PendingIntent is not working on Android O