从 onprogressupdate 方法只显示一次通知
displaying notification only once from onprogressupdate method
我正在尝试从正在进行的更新方法中仅显示一次通知。存在允许显示通知的条件。问题是它在第一个通知之后继续显示通知,并且 phone 一直在响铃和振动。我只想收到一个关于某事的通知并停止。有可能吗?有人可以帮忙并建议其他方法吗?谢谢
这是我的 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());
}
使用布尔值来记住您已经显示了通知,并使用它来不显示另一个通知,如下所示:
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;
}
您还可以在通知生成器上使用 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();
我正在尝试从正在进行的更新方法中仅显示一次通知。存在允许显示通知的条件。问题是它在第一个通知之后继续显示通知,并且 phone 一直在响铃和振动。我只想收到一个关于某事的通知并停止。有可能吗?有人可以帮忙并建议其他方法吗?谢谢
这是我的 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());
}
使用布尔值来记住您已经显示了通知,并使用它来不显示另一个通知,如下所示:
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;
}
您还可以在通知生成器上使用 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();