Android Уведомление не отображается - PullRequest
0 голосов
/ 21 февраля 2020

Я новичок в android dev. Я пытаюсь настроить уведомление, но оно не работает, и я не уверен, почему.


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());
    }

}

Я пробовал гуглить многие вещи, а также многие другие ответы о переполнении стека, но не был в состоянии придумать что угодно.

Спасибо, и любая помощь очень ценится.

...