Android通知未在指定的时间和日期显示

时间:2019-05-08 08:00:31

标签: android broadcastreceiver alarmmanager

我想在用户选择的日期通知用户,但通知没有在特定的日期和时间显示 我已经阅读了所有相关问题,但是无法完成这项工作。我已经在清单文件中注册了接收者,通知会立即显示

如果我将8000设置为c.getTimeInMillis()。 我对android开发非常陌生,因此不胜感激。

MY notificationhelper

public class NotificationHelper extends ContextWrapper {

    public static final String channel1ID = "channel1ID";
    public static final String channel1Name = "Channel 1";

    public static final String channel2ID = "channel2ID";
    public static final String channel2Name = "Channel 2";

    private NotificationManager mManager;

    public NotificationHelper(Context base){
        super(base);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            createChannels();
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    public void createChannels(){
        NotificationChannel channel1 = new NotificationChannel(channel1ID,channel1Name, NotificationManager.IMPORTANCE_DEFAULT);
        channel1.enableLights(true);
        channel1.enableVibration(true);

        getManager().createNotificationChannel(channel1);

        NotificationChannel channel2 = new NotificationChannel(channel2ID,channel2Name, NotificationManager.IMPORTANCE_DEFAULT);
        channel2.enableLights(true);
        channel2.enableVibration(true);
        getManager().createNotificationChannel(channel2);
    }

    public NotificationManager getManager(){

        if(mManager == null){
            mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        }
        return mManager;
    }

    public NotificationCompat.Builder getChannel1Notification( ){
        return new NotificationCompat.Builder(getApplicationContext(),channel1ID)
                .setSmallIcon(R.drawable.ic_event_vac_notification)
                .setContentTitle(getString(R.string.app_name))
                .setContentText(getString(R.string.vac_notification_message))
                .setPriority(NotificationCompat.PRIORITY_DEFAULT);

    }

    public NotificationCompat.Builder getChannel2Notification(){
        return new NotificationCompat.Builder(getApplicationContext(),channel2ID)
                .setSmallIcon(R.drawable.ic_event_vac_notification)
                //.setContentTitle(title)
                //.setContentText(message)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT);
    }

}

我的广播接收者

public class AlertReciever extends BroadcastReceiver {


    @Override
    public void onReceive(Context context, Intent intent) {


    NotificationHelper notificationHelper = new NotificationHelper(context);
    NotificationCompat.Builder nb = notificationHelper.getChannel1Notification();
    notificationHelper.getManager().notify(1,nb.build());


    }


}

设置了用户选择日期和闹钟的活动

public class Add_Vaccination_Record extends AppCompatActivity {



    private Button vacDueDateBtn;
    private Button saveVacData;
    private CalendarView cal;
    private EditText VacNameField;
    private EditText VacStatusField;
    private String VacDueDateField;
    Calendar c = Calendar.getInstance();


    @Override
    public void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_vaccination_record);
        VacNameField = findViewById(R.id.vaccination_edit);
        VacStatusField = findViewById(R.id.vac_status_edit);

         vacDueDateBtn = findViewById(R.id.vac_due_date_btn);

        vacDueDateBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showCalendarDialog();
            }
        });

        saveVacData = findViewById(R.id.save_vac_btn);

        saveVacData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (VacNameField.getText().toString().equalsIgnoreCase("")) {
                    VacNameField.setError("name required");
                } else if (VacStatusField.getText().toString().equalsIgnoreCase("")) {
                    VacStatusField.setError("Status required");
                } else


                    saveChild();

            }
        });


    }
    private void showCalendarDialog(){

        Dialog dialog = new Dialog(Add_Vaccination_Record.this);


//      setContentView(R.layout.calendar_main);
        dialog.setContentView(getLayoutInflater().inflate(R.layout.activity_calendar_view,null));
        cal = dialog.findViewById(R.id.calendarView1);
        dialog.show();
        cal.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {

            @RequiresApi(api = Build.VERSION_CODES.KITKAT)
            @Override
            public void onSelectedDayChange(CalendarView view, int year, int month,
                                            int dayOfMonth) {


                c.set(Calendar.DAY_OF_MONTH,dayOfMonth);
                c.set(Calendar.MONTH,month);c.set(Calendar.YEAR,year);
                c.set(Calendar.HOUR_OF_DAY,12);
                c.set(Calendar.MINUTE,9);
                c.set(Calendar.SECOND,0);
                startAlarm();
                // TODO Auto-generated method stub

                VacDueDateField = ""+dayOfMonth+""+month+"" +year;

               // Toast.makeText(getBaseContext(),"Selected Date is\n\n"
                              //  +dayOfMonth+" : "+month+" : "+year ,
                     //   Toast.LENGTH_LONG).show();

            }
        });

    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    private void startAlarm(){

        AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(this, AlertReciever.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(Add_Vaccination_Record.this, 1, intent, 0);

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

        alarmManager.setExact(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pendingIntent);
        Toast.makeText(getApplicationContext(),"Single Alarm set for"+ c.getTime(),Toast.LENGTH_LONG).show();
    }

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。 c.getTime()返回用户选择的时间+ GMT 05:00加上5个小时。因此,这就是通知未显示的原因。该通知设置为提前五个小时显示。希望它可以帮助遇到此类问题的任何人,请检查您的时区。