如何通过单击通知操作按钮删除通知?

时间:2019-04-11 11:49:39

标签: java android broadcastreceiver android-notifications

我当前正在开发一个Android应用程序,该应用程序会接收推送通知。现在,我显示的通知通常具有一个操作按钮,该按钮应执行以下代码片段所示的操作:

public class PushReceiver extends BroadcastReceiver implements RequestCallbacks {

    private RequestHandler requestHandlerInstance;
    private SessionHandler sessionHandlerInstance;
    private Context context;

    @Override
    public void onReceive(Context context, Intent intent) {
        this.context = context.getApplicationContext();
        requestHandlerInstance = RequestHandler.getInstance(context);
        sessionHandlerInstance = SessionHandler.getInstance(context);

        String id = intent.getStringExtra("error_id");
        requestHandlerInstance.startRequest(new RequestOperation(RequestOperation.Type.ERROR_TAKE_OVER, sessionHandlerInstance.getDeviceHash(), id), this);
    }

    #these are callbacks that will be executed 
    #when starRequest() returns a response

    @Override
    public void onSuccess(JSONObject json, String parsingKey) {
        #request was successful
    }

    @Override
    public void onError(VolleyError error, String parsingKey) {
        #request failed, activating panic mode
    }

    @Override
    public void onFinished(String parsingKey) {
        #here i plan to dismiss the notification box, 
        #but this doesn't seem to be the correct approach 
        #as the box is still there
        Intent intent = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
        context.sendBroadcast(intent);
    }
}

有人有合适的解决方案如何关闭通知框吗?当前不需要onClick事件的UI响应。还是我必须从MessagingService发送一个新的Intent,这样会覆盖以前的通知?

为了更加清楚,这是我创建通知的方式:

Notification notification;

        Intent activityIntent = new Intent(this.getApplicationContext(), MainActivity.class);
        activityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent contentIntent = PendingIntent.getActivity(this.getApplicationContext(), REQUEST_CODE, activityIntent, PendingIntent.FLAG_ONE_SHOT);

        Intent broadcastIntent = new Intent(this, PushReceiver.class);
        broadcastIntent.putExtra("error_id", remoteMessage.getData().get("id"));
        PendingIntent actionIntent = PendingIntent.getBroadcast(this, REQUEST_CODE, broadcastIntent, PendingIntent.FLAG_ONE_SHOT);

        notification = new NotificationCompat.Builder(this, getString(R.string.default_notification_channel_id))
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(remoteMessage.getData().get("notification_title"))
                .setContentText(remoteMessage.getData().get("notification_text"))
                .setSound(settingsHandlerInstance.getRingtoneUri())
                .setVibrate(settingsHandlerInstance.shouldVibrateOnPush() ? new long[] {0, 500, 200, 500, 0} : new long[] {0, 0, 0, 0, 0 })
                .setPriority(NotificationCompat.PRIORITY_HIGH)
                .setCategory(NotificationCompat.CATEGORY_MESSAGE)
                .setContentIntent(contentIntent)
                .addAction(R.mipmap.ic_launcher, "Übernehmen", actionIntent)
                .setAutoCancel(true)
                .build();

1 个答案:

答案 0 :(得分:0)

您在哪里显示通知详细信息。

int notifId = new Random().nextInt();
PendingIntent dismissIntent = NotificationActivity.getDismissIntent(notifId, context);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setPriority(NotificationCompat.PRIORITY_MAX)
        .setDefaults(Notification.DEFAULT_ALL)
        .setSmallIcon(R.drawable.app_icon)
        .setContentTitle("Title")
        .setContentText("Content Text")
        .setAutoCancel(true)
        //You use addAction to include the dismiss button. The click action is to remove the notification. You can handle any action here as desired to function with the dismiss button.
        .addAction(R.drawable.ic_cancel, "Dismiss", dismissIntent);

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

notifManager.notify(notificationId, builder.build());