【问题标题】:How to delete a Notification by clicking on a notification action button?如何通过单击通知操作按钮删除通知?
【发布时间】:2019-04-11 11:49:38
【问题描述】:

我目前正在开发一个接收推送通知的 Android 应用程序。现在我显示的通知通常有一个操作按钮,它应该执行如下代码 sn-p 所示的操作:

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

【问题讨论】:

  • 您可以在 NotificationManager 对象上调用“cancel(int id)”。但是,您必须跟踪通知 ID。
  • 可以这么说,我可以将 notification_id 作为额外的意图传递并在 pushReceiver 中调用 cancel() 吗?
  • 是的,试试看它是否适合你。确保您可以从 pushReceiver 访问 NotificationManager 对象
  • 这可能是一个警告,通知管理器是我的 MessagingService 的一个属性。它必须是同一个对象吗?
  • 有什么方法可以通过意图将 NotificationManagerCompat 传递给我的 BroadcastReceiver?

标签: java android broadcastreceiver android-notifications


【解决方案1】:

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

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

【讨论】:

  • 你想用这个随机码 sn-p 说什么?
  • 对我来说似乎可行,但我希望在单击操作按钮后关闭通知,这会有所不同。我将在稍后添加创建通知的代码
  • 这就是 .addAction 的作用。单击关闭后它将关闭通知。
  • 但我想通过按下按钮执行另一个操作,例如发送请求。并在响应返回以关闭通知时作为回调。
  • 哦,好吧。您可以分享创建通知的位置。 @TimCastelijns我可能不明白“随机代码sn-p”是什么意思。您有一个更好的解决方案,我分享的内容对我有用?
猜你喜欢
  • 2016-10-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-11
相关资源
最近更新 更多