android - 为什么我的通知没有在指定时间出现
问题描述
我想使用警报管理器显示通知,但通知不会在指定时间出现,而是在保存数据时出现通知
这是我的scheduledNotification方法代码
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
我已经搜索并尝试了一个,它仍然不起作用,我需要你的帮助
谢谢
解决方案
定义通知的重要性。参考上面的链接
https://developer.android.com/training/notify-user/build-notification.html#Priority
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
推荐阅读
- python - 如何将 .txt 中的两个值分隔到两个不同的文件中?
- html - F#计算html网站上所有存在的链接
- c# - 使用 C# 写入 Excel。(每次调用时都会在excel中输入一个值的函数)
- ruby-on-rails - Rails:仅更新实体的某些属性
- list - 无法在新 GCE 虚拟机上列出服务帐户
- c# - 如何通过将 HttpClient 请求转换为 RestSharp 请求来上传文档?
- c - 我将如何阅读并将此文本文件的信息分成数组?
- c++ - 重载矩阵类的加法和乘法运算符
- javascript - 如何在javascript中获取URL中使用where条件?
- python - 为什么 TypeError 即使它需要 2 个参数