Java 在特定时间推送多个通知(AlarmManager、NotificationManager)-Android

Java 在特定时间推送多个通知(AlarmManager、NotificationManager)-Android,java,android,notifications,Java,Android,Notifications,我正在尝试制作离线推送通知,该通知将在特定时间推送一次。 以下是我的日历和AlarmManager设置: Intent intent = new Intent(getApplicationContext(), Notification_receiver.class); AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); PendingIntent pendingIntent1

我正在尝试制作离线推送通知,该通知将在特定时间推送一次。 以下是我的日历和AlarmManager设置:

Intent intent = new Intent(getApplicationContext(), Notification_receiver.class);

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

    PendingIntent pendingIntent1 = PendingIntent.getBroadcast(getApplicationContext(), 101, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    PendingIntent pendingIntent2 = PendingIntent.getBroadcast(getApplicationContext(), 102, intent, PendingIntent.FLAG_UPDATE_CURRENT);


    Calendar push1 = Calendar.getInstance();
    Calendar push2 = Calendar.getInstance();

    push1.set(2017, Calendar.JULY, 1);
    push1.set(Calendar.HOUR_OF_DAY, 20);
    push1.set(Calendar.MINUTE, 22);
    push1.set(Calendar.SECOND, 30);

    push2.set(2017, Calendar.JULY, 1);
    push2.set(Calendar.HOUR_OF_DAY, 20);
    push2.set(Calendar.MINUTE, 28);
    push2.set(Calendar.SECOND, 30);


    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, push1.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent1);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, push2.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent2);
和我的通知类:

public class Notification_receiver extends BroadcastReceiver{


@Override
public void onReceive(Context context, Intent intent) {

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    Intent repeating_intent = new Intent(context, RepeatingActivity.class);
    repeating_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent1 = PendingIntent.getActivity(context, 101, repeating_intent, PendingIntent.FLAG_UPDATE_CURRENT);
    PendingIntent pendingIntent2 = PendingIntent.getActivity(context, 102, repeating_intent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder1 = new NotificationCompat.Builder(context).setContentIntent(pendingIntent1)
            .setSmallIcon(R.drawable.onusicijadi_icon)
            .setContentTitle("FIRST")
            .setContentText("FIRST NOTIFICATION")
            .setDefaults(Notification.DEFAULT_ALL)
            .setAutoCancel(true);

    notificationManager.notify(103, builder1.build());

    NotificationCompat.Builder builder2 = new NotificationCompat.Builder(context).setContentIntent(pendingIntent2)
            .setSmallIcon(R.drawable.onusicijadi_icon)
            .setContentTitle("SECOND")
            .setContentText("SECOND NOTIFICATION")
            .setDefaults(Notification.DEFAULT_ALL)
            .setAutoCancel(true);

    notificationManager.notify(104, builder2.build());

}}
RepeatingActivity.class只是:

public class RepeatingActivity extends AppCompatActivity{


@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

     setContentView(R.layout.activity_program);

}
这段代码在20:22和20:28发送两个通知,显然,我希望它是分开的。例如,一个通知在20:22发出,另一个在20:28发出。
在为期3天的音乐节期间,我需要大约30个活动通知。

您使用的是哪种api

注:自API 19起,所有重复报警均不准确。如果你的 应用程序需要精确的交付时间,然后必须使用一次性交付 准确的警报,按上述每次重新安排。遗产 targetSdkVersion早于API 19的应用程序将 继续保持所有警报,包括重复警报, 被视为精确的

您可以在上找到更多信息,如果仍然使用AlarmManager,解决方案将是使用
setExact
setExactAndAllowHileId


使用这些文件时要小心,如果在短时间内有多个通知,可以考虑使用服务。

您使用哪种API?

注:自API 19起,所有重复报警均不准确。如果你的 应用程序需要精确的交付时间,然后必须使用一次性交付 准确的警报,按上述每次重新安排。遗产 targetSdkVersion早于API 19的应用程序将 继续保持所有警报,包括重复警报, 被视为精确的

您可以在上找到更多信息,如果仍然使用AlarmManager,解决方案将是使用
setExact
setExactAndAllowHileId


使用这些文件时要小心,如果在短时间内有多个通知,则可以考虑使用服务。

在使用<代码> SETTACTION/COD>时,也一样。两个通知同时到达,稍后,它们都再次到达。我想知道我怎样才能一次发送第一个,然后只发送第二个?为什么这两个实例(push1和push2)都在只有push1应该到达的时间到达呢?@ogicekic甚至setExact都有它的问题,因为它在空闲时不运行。要使它真正匹配您指定的时间,需要
setExactAndAllowHileId
,但这是API 23及更高版本。在这种情况下,您可以考虑通过作业调度程序或其他方法启动的服务,该方法可以持续生存一段时间,以便在准确的时间发出通知。当使用<代码> SETTACTION/COD>时,也会相同,两个通知同时到达,稍后,它们都再次到达。我想知道我怎样才能一次发送第一个,然后只发送第二个?为什么这两个实例(push1和push2)都在只有push1应该到达的时间到达呢?@ogicekic甚至setExact都有它的问题,因为它在空闲时不运行。要使它真正匹配您指定的时间,需要
setExactAndAllowHileId
,但这是API 23及更高版本。在这种情况下,您可以考虑通过作业调度程序或其他可以持续生存一段时间的方法启动的服务,以便它可以在准确的时间发出通知。