使用RemoteView自动取消自定义通知

时间:2014-04-22 05:42:30

标签: android android-notifications

我使用remoteview创建了自定义布局通知。问题我面临的问题是,如何在用户触摸通知时自动取消通知。 我确实尝试过很少的东西,但没有一个能给出理想的结果。

查找以下代码:

RemoteViews remoteView = new RemoteViews(this.getPackageName(), R.layout.notification_layout);      
    NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this);
    builder.setSmallIcon(R.drawable.ic_launcher);

    Intent intent = new Intent(MainActivity.this, SecondActivity.class);

    PendingIntent pIntent = PendingIntent.getActivity(MainActivity.this, RQST_CODE, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    remoteView.setOnClickPendingIntent(R.id.btnNotification, pIntent);

    builder.setAutoCancel(true);
    builder.setContent(remoteView); 
    Notification notify = builder.build();
    notify.flags |= Notification.FLAG_AUTO_CANCEL;
    NotificationManager notiManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notiManager.notify(NOTY_ID, notify);

2 个答案:

答案 0 :(得分:3)

我实现此目标的方法是创建BroadcastReceiver来控制来自Notification的按钮点击。创建这样的东西:

public class NotificationButtonListener extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        int action = intent.getIntExtra("mode", -1);
        switch (action) {
            // do some things depending on action if there are more stuff to do
        }
    }
}

不要忘记将BroadcastListener添加到您的清单文件中:

<receiver android:name=".NotificationButtonListener">

您可以创建一个帮助类来创建和取消通知:

public class NotificationHelper {

    private static final int NOTIFICATION_ID = 5;

    private static NotificationManager mNotificationManager = null;

    public static void showNotification(Context context) {

        mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        RemoteViews contentView = new RemoteViews(context.getPackageName(), R.layout.custom_notification_layout);
        contentView.setOnClickPendingIntent(R.id.btn_close, closePendingIntent);

        Intent mMainIntent = new Intent(context, MainActivity.class);
        mMainIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent mMainPendingIntent = PendingIntent.getActivity(context, 555, mMainIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                .setTicker("Playing")
                .setContent(contentView)
                .setAutoCancel(false)
                .setOngoing(true)
                .setSmallIcon(R.drawable.ic_av_play)
                .setContentIntent(mMainPendingIntent);

        Notification notification = mBuilder.build();

        mNotificationManager.notify(NOTIFICATION_ID, notification);

    }

    public static void cancelNotification() {
        if (mNotificationManager != null) {
            mNotificationManager.cancelAll();
        }
    }
}

NotificationButtonListener中使用该课程,您可以拨打NotificationHelper.cancelNotification();

希望这能帮到你!

答案 1 :(得分:2)

对于拥有自定义布局并希望自动取消工作的其他人, 如果点击通知中的任何地方都可以,则不要使用 remoteView.setOnClickPendingIntent 。因为任何带有 OnClick 侦听器的视图都会覆盖通知主 OnClick 侦听器。

改为使用 setContentIntent

NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setContentTitle("title);
builder.setContentText("contect);
builder.setSmallIcon(@smallicon);
builder.setWhen(@when);
builder.setContent(remoteViews);
builder.setContentIntent(onClickPendingIntent); // use this
builder.setAutoCancel(true);