没有消息显示-通知

时间:2019-02-02 20:19:02

标签: java android notifications

我想设定一个闹钟。用户选择与所述时间选择器的时刻,并且当时间到来时通知一个显示出来。这很完美。但是问题是,用户输入中没有显示用户编写的警报的名称。我没有收到错误消息!
https://prnt.sc/mfs5hx
https://prnt.sc/mfs5w2

NotificationHelper.java:

    public NotificationCompat.Builder getChannelNotification() {
    LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View frAl = inflater.inflate(R.layout.fragment_alarm, null);
    titelAlarm = frAl.findViewById(R.id.eingUserAlarm);
    String nameAlarm = titelAlarm.getText().toString();
    return new NotificationCompat.Builder(getApplicationContext(), channelID)
            .setContentTitle("Erinnerung")
            .setContentText(nameAlarm)
            .setSmallIcon(R.drawable.ic_one);
}

用户可以在其中命名警报的EditText位于片段中,可以通过导航抽屉对其进行访问。

   <EditText
    android:id="@+id/eingUserAlarm"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="180dp"
    android:hint="Was wirst du machen?" />

同样对于报警代码是我Fragment.java内部。
Fragment.java:

Button buttonTimePicker = fraAla.findViewById(R.id.button_timepicker);
    buttonTimePicker.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DialogFragment timePicker = new AlarmTimePicker();
            timePicker.show(getChildFragmentManager(), "time picker");
        }
    });

    Button buttonCancelAlarm = fraAla.findViewById(R.id.button_cancel);
    buttonCancelAlarm.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            cancelAlarm();
        }
    });
    return fraAla;
}

@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    Calendar c = Calendar.getInstance();
    c.set(Calendar.HOUR_OF_DAY, hourOfDay);
    c.set(Calendar.MINUTE, minute);
    c.set(Calendar.SECOND, 0);

    updateTimeText(c);
    startAlarm(c);
}

private void updateTimeText(Calendar c) {
    String timeText = "Wir erinnern dich um ";
    timeText += DateFormat.getTimeInstance(DateFormat.SHORT).format(c.getTime());

    mTextView.setText(timeText);
}

private void startAlarm(Calendar c) {
    AlarmManager alarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(getActivity(), AlertReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 1, intent, 0);

    if (c.before(Calendar.getInstance())) {
        c.add(Calendar.DATE, 1);
    }

    alarmManager.setExact(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent);
}

private void cancelAlarm() {
    AlarmManager alarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(getActivity(), AlertReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 1, intent, 0);

    alarmManager.cancel(pendingIntent);
    mTextView.setText("Wir werden nicht stören!");
}

0 个答案:

没有答案