【问题标题】:Scheduling multiple future notifications安排多个未来通知
【发布时间】:2017-01-26 18:13:29
【问题描述】:

我目前正在调试我的应用程序中的通知问题。在某些情况下,我想做的是安排在火箭发射时应该弹出的通知。我所做的是,在从 API 获取计划启动列表后,我将获取启动日期(自 1970 年 1 月 1 日以来的毫秒数)并从中减去 System.currentTimeMillis()。然后,我将使用生成的时间来安排将来的通知,表示为System.currentTimeMillis() + timeDifference。我注意到无论出于何种原因,只显示 1 条通知。

我已尝试通过在未来 2、4 和 6 分钟安排通知来进行调试,但通知仅在 6 分钟标记处显示。

部分相关代码如下:

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

广播接收器:

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

    }

}

我想知道为什么只显示一个通知,以及我需要添加什么来实现之前声明的目标。

【问题讨论】:

    标签: android broadcastreceiver alarmmanager android-notifications android-pendingintent


    【解决方案1】:

    当您使用AlarmManager 设置警报时,它会自动取消与PendingIntent 匹配的任何现有警报。由于您所有的PendingIntents 都包含相同的组件,因此每次设置警报时,之前设置的警报都会自动取消。

    如果要设置多个警报,则必须确保每个PendingIntents 都是唯一的。您可以通过以下方式之一执行此操作:

    • 为每个PendingIntent 使用不同的requestCodePendingIntent.getBroadcast() 的第二个参数)
    • 在传递给PendingIntent.getBroadcast()Intent 中为每个PendingIntent 使用不同的操作

    【讨论】:

    • 太好了,谢谢。我已将 requestCode 参数替换为唯一整数(在本例中为 i 的值),现在收到多个通知。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-28
    • 1970-01-01
    • 2022-06-26
    • 1970-01-01
    相关资源
    最近更新 更多