0

I am trying to get data from a server and post it on screen and push with notification. The data is a random number and it's changing second by second. On opening, the server is working on background and the data is receiving but data is posting on screen one time, it isn't changing and I can't get notification. With button, the data is perfectly posting on screen and it's changing second by second but I can't get notification again.

import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import android.os.Bundle;
import com.star_zero.sse.EventHandler;
import com.star_zero.sse.EventSource;
import com.star_zero.sse.MessageEvent;
//STAR-ZERO/sse-android
public class MainActivity extends AppCompatActivity {
    static TextView s;
    String a =null;
    private final String CHANNEL_ID = "server_notifications";
    private final int NOTIFICATIONS_ID = 1;
    public EventSource eventSource;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        s=findViewById(R.id.random);
        final NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
        final NotificationCompat.Builder builder = new  NotificationCompat.Builder(this, CHANNEL_ID);
        eventSource = new EventSource("path", new EventHandler() {
            @Override
            public void onOpen() {
            }
            @Override
            public void onMessage(MessageEvent messageEvent) {
                a=messageEvent.getData();
                try {

                    builder.setPriority(NotificationCompat.PRIORITY_MAX)
                            .setAutoCancel(true)
                            .setContentText(a)
                            .setContentTitle("ALERT!!!")
                            .setSmallIcon(R.drawable.noti_icon);
                    notificationManager.notify (NOTIFICATIONS_ID,builder.build());
                    s.setText(a);
                }
                catch (Exception e){
                    e.printStackTrace();
                }
            }
            @Override
            public void onError(Exception e) {
                try {
                    s.setText("error");
                }
                catch (Exception d) {
                    d.printStackTrace();
                }
            }
        });
        eventSource.connect();
        findViewById(R.id.button_open).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                eventSource.connect();
            }
        });
        findViewById(R.id.button_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                eventSource.close();
            }
        });
    }
}
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847

1 Answers1

0

You should not build a new notification on each onMessage instead, maintain a global notification reference and update the content. See this response for more details: Update text of notification, not entire notification

elcuco
  • 8,948
  • 9
  • 47
  • 69