Search code examples
androidandroid-asynctasknotificationsasync-onprogressupdate

Notifications from asynctack OnProgressUpdate method


I am currently working on an application which will continuously get sensors data from a web server and display it in text Views. I am using an Asynctask to get the data and the OnprogressUpdate method continuously fetch new data and display it. Whenever the sensor data is greater than 70, a notification is sent.

The problem I am having is that only one notification is sent when the data is greater than 70 and when it decreases and re-increases no notification is sent.

When I remove the boolean value, my phone keep on sending notifications and this is annoying. Is there a way when the data is >70 , it send a notification and when it decreases and again increases, another notification is sent?

Note that the phone will be receiving 2 sensors data and it will continuously display the data in text Views.

Below is the code for the OnProgressMethod. Hope that you guys can help me pls. Thanks.

OnProgressUpdate method:

 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(Float.parseFloat(part2)>70.00 && !alreadyDisplayedNotification)
         {
        	NotificationCompat.Builder builder=new NotificationCompat.Builder(this.context);
 			builder.setContentTitle("AM Home Automation");
 			builder.setContentText("humidity is high");
 			builder.setSmallIcon(R.drawable.ic_launcher);
 			builder.setTicker("alert");
 			builder.setDefaults(Notification.DEFAULT_ALL);
 			//builder.setSound(Uri.parse("android.resource://"+getPackageName()+"/"+R));
 			
 			notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
 			notificationManager.notify(1, builder.build());
 			//notificationID--;
 			alreadyDisplayedNotification=true;
 			
         }
        
         
      
     }


Solution

  • I think:

    if(Float.parseFloat(part2)<=70.00){
        alreadyDisplayedNotification=false;
    }