为什么我的通知没有在指定的时间出现

问题描述 投票:0回答:1

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

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

我已经搜索并尝试过一个,仍然无法使用,需要您的帮助

感谢

android push-notification alarmmanager
1个回答
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);
© www.soinside.com 2019 - 2024. All rights reserved.