我试图仅从onprogress更新方法显示一次通知。有条件允许显示通知。问题是它在第一个之后继续显示通知,并且手机继续振铃和振动。我只想收到一条通知,告知某事并停止。有可能吗?有人可以帮忙,并建议其他方法。感谢。
这是我的onPorgressUpdate方法的代码:
protected void onProgressUpdate(byte[]... values) {
super.onProgressUpdate(values);
String command=new String(values[0]);//get the String from the recieved bytes
String[] parts= command.split(",");
String part1=parts[0];
String part2=parts[1];
temp.setText(part1);
humi.setText(part2);
if(Integer.parseInt(part2)>70)
{
NotificationCompat.Builder builder=new NotificationCompat.Builder(this.context);
builder.setContentTitle("AM Home Automation");
builder.setContentText("humi");
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setTicker("alert");
builder.setDefaults(Notification.DEFAULT_ALL);
notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
}
答案 0 :(得分:0)
使用布尔值记住您已经显示通知并使用它不显示另一个如下所示:
private boolean alreadyDisplayedNotification = false;
protected void onProgressUpdate(byte[]... values) {
super.onProgressUpdate(values);
String command=new String(values[0]);//get the String from the recieved bytes
String[] parts= command.split(",");
String part1=parts[0];
String part2=parts[1];
temp.setText(part1);
humi.setText(part2);
if(Integer.parseInt(part2)>70 && !alreadyDisplayedNotification) {
NotificationCompat.Builder builder=new NotificationCompat.Builder(this.context);
builder.setContentTitle("AM Home Automation");
builder.setContentText("humi");
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setTicker("alert");
builder.setDefaults(Notification.DEFAULT_ALL);
notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
alreadyDisplayedNotification=true;
}
答案 1 :(得分:0)
您还可以在通知构建器上使用setOnlyAlertOnce(true),这只会显示抬头通知一次
Notification notification = new NotificationCompat.Builder(getActivity().getApplicationContext(),CHANNEL_ID)sensitive content.
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("title")
.setAutoCancel(false)
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(notiPriority)
.setOnlyAlertOnce(true) // set this to show and vibrate only once
.build();