gpt4 book ai didi

android - 如何关闭操作点击通知?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:57:12 24 4
gpt4 key购买 nike

我想在点击关闭通知时关闭我的通知。就像我们在滑动时将其关闭一样。

NotificationManager notificationManager = (NotificationManager) ctx
.getSystemService(Context.NOTIFICATION_SERVICE);

Intent intent = new Intent(ctx, MainActivity.class);
intent.putExtra("isFromBadge", false);

Intent resultIntent = new Intent(ctx, ResultActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(ctx);
stackBuilder.addParentStack(ResultActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_ONE_SHOT);

PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, resultIntent, PendingIntent.FLAG_CANCEL_CURRENT);

// PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

Notification notification = new Notification.Builder(ctx)
.setContentTitle(
ctx.getResources().getString(R.string.app_name))
.setContentText(message)
.setWhen(when)
.setStyle(new Notification.BigPictureStyle().bigPicture(result))
.setSmallIcon(R.mipmap.ic_launcher)
.addAction(R.drawable.bell, "Dismiss", resultPendingIntent)
.addAction(R.drawable.bell, "Share", contentIntent)
.setLargeIcon(result)
.setAutoCancel(true)
.build();

最佳答案

这样试试

public void showNotification(Context ctx) {
int noti_id = 100;
NotificationManager notificationManager = (NotificationManager) ctx
.getSystemService(Context.NOTIFICATION_SERVICE);

Intent actionIntent = new Intent(ctx, ActionReceiver.class);
actionIntent.putExtra("noti_id", noti_id);

PendingIntent pendingIntent = PendingIntent.getBroadcast(ctx, noti_id, actionIntent,
PendingIntent.FLAG_CANCEL_CURRENT);

Notification notification = new Notification.Builder(ctx)
.setContentTitle("hello")
.setContentText("Hello")
.addAction(R.drawable.bell, "dismiss", pendingIntent)
.setAutoCancel(false)
.build();


notificationManager.notify(noti_id, notification);
}



public class ActionReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
if (intent != null) {
int noti_id = intent.getIntExtra("noti_id", -1);

if (noti_id > 0) {
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.cancel(noti_id);
}
}
}
}

关于android - 如何关闭操作点击通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44845394/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com