首页 > 解决方案 > 警报在到期时正确收到,但在之后打开应用程序时第二次不正确

问题描述

我可以通过使用日历类 java(例如上午 9:15)在未来的特定时间获得通知。只要时间是上午 9:15,但当我在上午 9:15 之后(例如上午 9:20)打开应用程序时,警报管理器就会将广播发送到广播接收器。我收到同样的通知。任何人都可以帮助我解决这个问题。

我的清单

<receiver android:name=".others.NotificationReceiver"/>

通知接收器.java

public class NotificationReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        createNotification(context);
    }

    private void createNotification(Context context) {
        NotificationManager notificationManager = context.getSystemService(NotificationManager.class);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
        {
            NotificationChannel linkChannel = new NotificationChannel(link_channel_id, "Link Channel", NotificationManager.IMPORTANCE_HIGH);
            linkChannel.setDescription("This channel is used to show Zoom Links");
            notificationManager.createNotificationChannel(linkChannel);
        }

        Notification notification = new NotificationCompat.Builder(context, link_channel_id)
                .setSmallIcon(R.mipmap.ic_launcher_foreground)
                .setContentTitle("Test Notification")
                .setContentText("This is a test notification")
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .setCategory(NotificationCompat.CATEGORY_MESSAGE)
                .build();

        notificationManager.notify(1, notification);
    }
}

我的警报管理器

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        Calendar calendar = Calendar.getInstance();

        calendar.set(Calendar.HOUR_OF_DAY, 9);
        calendar.set(Calendar.MINUTE, 10);
        calendar.set(Calendar.SECOND, 0);
        Intent myIntent1 = new Intent(this, NotificationReceiver.class);
        myIntent1.setAction(ACTION_ONE);
        PendingIntent pendingIntent1 = PendingIntent.getBroadcast(this, 1, myIntent1, PendingIntent.FLAG_UPDATE_CURRENT);
        alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent1);

标签: javaandroidnotificationsbroadcastreceiveralarmmanager

解决方案


推荐阅读