android - Android 通知未在指定时间和日期显示
问题描述
我想在用户选择的日期通知用户,但通知未在特定日期和时间显示我已阅读所有相关问题但无法完成此工作。我已经在清单文件中注册了接收器,并且通知立即显示
如果我设置 8000 而不是 c.getTimeInMillis()。我对 android 开发很陌生,因此感谢您的帮助。
我的通知助手类
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();
}
解决方案
我解决了这个问题。c.getTime() 返回用户选择的时间 + 格林威治标准时间 05:00 加上 5 小时。所以,这就是为什么没有显示通知的原因。通知设置为提前五个小时显示。希望它可以帮助任何遇到此类问题的人,请检查您的时区。
推荐阅读
- javascript - 匹配符号的正则表达式以符号开头
- r - R使用“[[”将函数应用于嵌套列表元素
- c++ - FFTW 在 Windows 机器上与 g++ 错误链接
- excel - 如何找到连续出现的字符串的第一次和最后一次出现?
- python - turtle.onclick 滞后 1 个周期
- c# - Visual Studio 2019 无法构建 Tizen 项目
- sql-server - 在 SQL Server 重放跟踪结果中捕获读取、写入和 CPU
- python-3.x - 功能不工作,语法错误等等
- orm - 我怎样才能“作曲家要求”只是 Doctrine ORM EntityManagerInterface
- wso2 - WSO2 API Manager - 向第三方公开服务