Android通知会立即触发,而不是在设定的日期和时间触发

时间:2018-08-17 01:49:29

标签: android notifications android-service android-notifications

几天来我一直遇到问题。我正在制作一个程序,在其中创建待办事项列表,并允许用户设置应用程序的日期/时间以通知他们有关该程序的信息。有一个复选框“通知我” ,可以安排通知,但每当我在列表中保存一个项目时,通知就会立即触发。

这是我的代码:

    public class NotificationService extends BroadcastReceiver {

    public String NOTIFICATION_ID = "notification-id";
    public String NOTIFICATION = "notification";

    public String getNOTIFICATION() {
        return NOTIFICATION;
    }

    public String getNOTIFICATION_ID() {
        return NOTIFICATION_ID;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

        Notification notification = intent.getParcelableExtra(NOTIFICATION);
        int id = intent.getIntExtra(NOTIFICATION_ID, 0);
        notificationManager.notify(id, notification);
    }
}

在列表中保存项目的功能:

private void save(int journalNo, int journalNo, int agentNo, String agentId, Date journalDateCreated,
                  Date journalDateNotify, String journal, boolean notify, boolean deleted) {

    Log.i("INFO", "Saving journal...");
    {
        JournalModel journalModel = new JournalModel(0, 0, RootUser.user.registeredAgentNo, "",
                journalDateCreated,
                journalDateNotify,
                journal,
                notifyOn,
                false);

        if (createJournalFunction.insertJournal(realm, journalModel)) {
            long timeInMillis = journalModel.getJournalDateNotify().getTime() - System.currentTimeMillis();
            Log.e("ERROR", String.valueOf(System.currentTimeMillis()));
            Log.e("ERROR", String.valueOf(timeInMillis));

            if (timeInMillis > 0 && notifyOn==true) {
                Log.e("INFO", String.valueOf(notifyOn));
                scheduledNotification(setNotification(journalModel.getJournal().toString()), timeInMillis);
            }
        }
            Log.e("INFO", journal);
            Log.e("INFO", "journal saved");
        }

        loadjournals();

    }

通知构建器:

public void scheduledNotification(Notification notification, long journaldatenotify) {
    NotificationService notificationService = new NotificationService();
    SecureRandom random = new SecureRandom();
    int notificationID = random.nextInt(9999 - 1000) + 1000;
    Intent notificationIntent = new Intent(this, NotificationService.class);
    notificationIntent.putExtra(notificationService.getNOTIFICATION_ID(), notificationID);
    notificationIntent.putExtra(notificationService.getNOTIFICATION(), notification);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    long milli = System.currentTimeMillis()+journaldatenotify;
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
    alarmManager.setExact(AlarmManager.RTC_WAKEUP, milli, pendingIntent);

    Log.e("TIME UNTIL NOTIF", String.valueOf(milli));
    Log.e("NOTIF ID", String.valueOf(notificationID));
}

public Notification setNotification(String message) {
    Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    Notification.Builder notificationBuilder = new Notification.Builder(this);
    notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
    notificationBuilder.setContentTitle("My Journal");
    notificationBuilder.setContentText(message);
    notificationBuilder.setAutoCancel(true);
    notificationBuilder.setTicker(message);
    notificationBuilder.setSound(sound);
    notificationBuilder.setStyle(new Notification.BigTextStyle().bigText(message));

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, notificationBuilder.build());
    return notificationBuilder.build();

}

1 个答案:

答案 0 :(得分:-1)

您可以使用以下代码设置警报:

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, second);
long time = calendar.getTimeInMillis();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    alarmManager.setAlarmClock(new AlarmManager.AlarmClockInfo(time, pendingIntent), pendingIntent);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    alarmManager.setExact(AlarmManager.RTC_WAKEUP, time, pendingIntent);
} else {
    alarmManager.set(AlarmManager.RTC_WAKEUP, time, pendingIntent);
}

如果您需要使用其他ID来设置通知,则可以使用代码:

long id = System.currentTimeMillis();