how to create multi notification based on a list of localtime

爷,独闯天下 提交于 2019-12-13 04:34:39

问题


I want to create several notification based on a local time list. so first I loop throw the list then I change them to millisecond and send them to the notification. based on the current code it only notify me based on the last time and it only loop throw the rest.

public static void main(String[] args) {
String q = "01:00:00";
String w = "02:00:00";
String e = "03:00:00";
String r = "04:00:00";

LocalTime qq = LocalTime.parse(q);
LocalTime ww = LocalTime.parse(w);
LocalTime ee = LocalTime.parse(e);
LocalTime rr = LocalTime.parse(r);

List<LocalTime> listTimes = new ArrayList<>();
listTimes.add(qq);
listTimes.add(ww);
listTimes.add(ee);
listTimes.add(rr);

// find out what day today is and do that outside the loop
LocalDate today = LocalDate.now();

// iterate/stream all the parsed times
listTimes.forEach(time -> {
    // combine the time with today
    LocalDateTime dateTime = LocalDateTime.of(today, time);
    // then convert it to an Instant (choose the offset you need) and get the millis
    long timeInMillis = dateTime.atZone(ZoneId.systemDefault())
                            .toInstant()
                            .toEpochMilli();

    Intent intent = new Intent(getApplicationContext(), Notification_reciever.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
                                                            100,
                                                            intent,
                                                            PendingIntent.FLAG_UPDATE_CURRENT);

    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
                                // use the millis here then
                                timeInMillis,
                                AlarmManager.INTERVAL_DAY,
                                pendingIntent);
});
}

and this is the notification class where the onReceive method on

public void onReceive(Context context, Intent intent) {

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


    int notificationId = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);
    String channelId="";

    int importance = NotificationManager.IMPORTANCE_HIGH;

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {

               channelId = "channel-"+notificationId;
        String channelName = "Channel-"+notificationId;

        NotificationChannel mChannel = new NotificationChannel(
                channelId, channelName, importance);
        notificationManager.createNotificationChannel(mChannel);
    }


    Intent repeating_intent = new Intent(context,ReaptingActivity.class);

  //  repeating_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
   //         | Intent.FLAG_ACTIVITY_SINGLE_TOP);


    repeating_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(context,0,repeating_intent,PendingIntent.FLAG_UPDATE_CURRENT);

  //  PendingIntent pendingIntent = PendingIntent.getActivity(context,0,repeating_intent,0);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context, channelId).setContentIntent(pendingIntent).setSmallIcon(R.drawable.ic_free_breakfast_black_24dp)
            .setContentTitle("Water Time").setContentText("Drink Water").setAutoCancel(true);


        notificationManager.notify(notificationId, builder.build());


}

来源:https://stackoverflow.com/questions/58917122/how-to-create-multi-notification-based-on-a-list-of-localtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!