简体   繁体   English

安排多个未来通知

[英]Scheduling multiple future notifications

I am currently debugging an issue with notifications inside my application. 我目前正在调试应用程序内的通知问题。 For some context, what I'd like to do is schedule notifications that should popup whenever a rocket launch is occurring. 对于某些情况,我想做的是安排在火箭发射时应该弹出的通知。 What I was doing was, after getting a list of scheduled launches from an API, I would take the launch date (in milliseconds since Jan 1 1970) and subtract the System.currentTimeMillis() from it. 我正在做的是,在从API获取预定启动列表后,我将采用启动日期(自1970年1月1日以来的毫秒数)并从中减去System.currentTimeMillis() I would then use the resulting time to schedule the notification in the future, represented as System.currentTimeMillis() + timeDifference . 然后,我将使用生成的时间来安排将来的通知,表示为System.currentTimeMillis() + timeDifference I noticed that for whatever reason, only 1 notification is ever displayed. 我注意到无论出于何种原因,只会显示1个通知。

I've tried debugging by scheduling notifications at 2, 4, and 6 minutes in the future, however a notification is only displayed at the 6 minute mark. 我已经尝试通过在将来的2,4和6分钟安排通知进行调试,但通知仅显示在6分钟标记处。

Some relevant code is below: 一些相关代码如下:

public void scheduleNotifications(List<Launch> launches) {

        for(int i = 0; i < launches.size(); i++) {

            SimpleDateFormat format = new SimpleDateFormat("MMMM dd, yyyy HH:mm:ss z");
            Date date = null;
            try {
                date = format.parse(launches.get(i).getWindowstart());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            long timeBetween = date.getTime() - System.currentTimeMillis();

            Integer id = Long.valueOf(date.getTime()).intValue();

            Intent notificationIntent = new Intent(this, NotificationPublisher.class);
            notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, id);
            notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, getNotification(launches.get(i).getRocket().getName(), launches.get(i).getLocation().getPads().get(0).getName()));
            PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, 0);
            AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            //Debug. Schedule at 2, 4, 6 minutes.
            if (i == 0) {
                alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 120000, pendingIntent);
            }
            if (i == 1) {
                alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 240000, pendingIntent);

            }
            if (i == 2) {
                alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 360000, pendingIntent);

            }
        }
    }

private Notification getNotification(String rocketName, String padName) {
        Notification.Builder builder = new Notification.Builder(this);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);
        builder.setContentIntent(pendingIntent);
        builder.setContentTitle("Upcoming Launch");
        builder.setContentText("A launch of a " + rocketName + " is about to occur at " + padName + ". Click for more info.");
        builder.setSmallIcon(R.drawable.rocket_icon);
        return builder.build();
}

Broadcast Receiver: 广播接收器:

public class NotificationPublisher extends BroadcastReceiver {

    public static String NOTIFICATION_ID = "notification_id";
    public static String NOTIFICATION = "notification";

    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);

    }

}

I'd like to know why only a single notification is ever presented, as well as what I need to add to achieve the previously stated goal. 我想知道为什么只提供一个通知,以及为实现上述目标我需要添加的通知。

When you set an alarm using AlarmManager , it automatically cancels any existing alarm that has a matching PendingIntent . 使用AlarmManager设置闹钟时,它会自动取消任何具有匹配PendingIntent现有闹钟。 Since all your PendingIntent s contain the same components, every time you set an alarm, the previously set ones are automatically cancelled. 由于所有PendingIntent都包含相同的组件,因此每次设置警报时,先前设置的警报将自动取消。

If you want to set multiple alarms, you must make sure that each of the PendingIntent s is unique. 如果要设置多个警报,则必须确保每个PendingIntent都是唯一的。 You can do this in one of the following ways: 您可以通过以下方式之一执行此操作:

  • Use a different requestCode (second parameter to PendingIntent.getBroadcast() ) for each PendingIntent 对每个PendingIntent使用不同的requestCodePendingIntent.getBroadcast()第二个参数)
  • Use a different ACTION in the Intent you pass to PendingIntent.getBroadcast() for each PendingIntent 在为每个PendingIntent传递给PendingIntent.getBroadcast()Intent使用不同的ACTION

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM