首页 > 解决方案 > 为什么我的通知没有在指定时间出现

问题描述

我想使用警报管理器显示通知,但通知不会在指定时间出现,而是在保存数据时出现通知

这是我的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

我已经搜索并尝试了一个,它仍然不起作用,我需要你的帮助

谢谢

标签: androidpush-notificationalarmmanager

解决方案


定义通知的重要性。参考上面的链接

https://developer.android.com/training/notify-user/build-notification.html#Priority

int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);

推荐阅读