0

I am new to android dev. I am trying to setup a notification, but it is not working, and I am not sure why.


public class MainActivity extends AppCompatActivity {

    public static final String SHARED_PREF="SHARED_PREFS";
    public static final String KEY="KEY_TEXT";

    private EditText editText;

    private Button button;
    private Button button2;
    private TextView final_text;
    private TextView editText3;
    private int val_two;
    private String food_list;
    private String id;
    private NotificationChannel channel;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.O){
            channel = new NotificationChannel(NotificationChannel.DEFAULT_CHANNEL_ID, "notifChan2", NotificationManager.IMPORTANCE_HIGH);
            channel.setDescription("A calorie notification");
        }


        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String val = editText.getText().toString();
                String food = editText3.getText().toString();
                food_list.concat(food);

                val_two = val_two + Integer.parseInt(val);
                SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREF,0);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putInt(KEY, val_two);

                editor.apply();

                String finalText = "You current calories " + val_two ;

                if(val_two>1000){
                    DisplayNotification();
                }
                final_text.setText(finalText);
callupdatewidget() function this will print the current calories into the wdiget
                callUpdateWidget(MainActivity.this,finalText);
            }
        });


    }




     private void DisplayNotification() {
        NotificationCompat.Builder notify = new NotificationCompat.Builder(this, NotificationChannel.DEFAULT_CHANNEL_ID)
                .setSmallIcon(android.R.drawable.ic_menu_agenda)
                .setContentTitle("Congrats!")
                .setContentText("You have reached your calorie goal of 1000!");

        NotificationManagerCompat notificationMgr = NotificationManagerCompat.from((this));
        notificationMgr.notify(1, notify.build());
    }

}

I have tried googling many things, as well as many other stack overflow answers, but have not been able to come up with anything.

Thanks, and any help is greatly appreciated.

wmoskal
  • 195
  • 2
  • 13

0 Answers0