我想使用警报管理器显示通知,但是该通知在指定时间没有出现,而是在保存数据时显示该通知
这是我的scheduleNotification方法代码
private void scheduleNotification(Notification notification, Calendar calendar) {
Intent notifIntent = new Intent(this, NotificationPublish.class);
notifIntent.putExtra(NotificationPublish.NOTIFICATION_ID, 0);
notifIntent.putExtra(NotificationPublish.NOTIFICATION, notification);
PendingIntent pendingIntent = PendingIntent.getBroadcast(AddTaskActivity.this, 0, notifIntent, PendingIntent.FLAG_CANCEL_CURRENT);
// long futureInMilis = SystemClock.elapsedRealtime() + time;
AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
是getNotification方法
private Notification getNotification(String task) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle(task);
builder.setContentText("Go Start");
builder.setSmallIcon(R.drawable.ic_launcher_background);
return builder.build();
}
这是日历集
public void onDateSelected(Date date) {
String date3 = sdf.format(date);
// time = date.getTime();
calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, date.getYear());
calendar.set(Calendar.MONTH, date.getMonth());
calendar.set(Calendar.DAY_OF_MONTH, date.getDay());
calendar.set(Calendar.HOUR_OF_DAY, date.getHours());
calendar.set(Calendar.MINUTE, date.getMinutes());
edt_datepicker.setText(date3);
}
}).display();
和此onClick来保存数据
public void onClick(View view) {
if (view == btn_add_task){
addTask();
scheduleNotification(getNotification(task),calendar);
onBackPressed();
finish();
}
这是BroadCastReceiver类
public class NotificationPublish extends BroadcastReceiver {
public static String NOTIFICATION_ID = "notification_id";
public static String NOTIFICATION = "notification";
@Override
public void onReceive(final Context context, Intent intent) {
Log.d("ONRECEIVE", "CALLED");
NotificationManager manager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = intent.getParcelableExtra(NOTIFICATION);
int id = intent.getIntExtra(NOTIFICATION_ID, 0);
manager.notify(id, notification);
}
}
日历导入为java.util.Calendar
我已经搜索并尝试过一个,但仍然无法正常工作,我需要您的帮助
谢谢
答案 0 :(得分:0)
定义通知的重要性。请参阅上面的链接
https://developer.android.com/training/notify-user/build-notification.html#Priority
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);