Невозможно получить несколько уведомлений в разное время
Я пытаюсь установить уведомление в определенное время, когда пользователь нажимает кнопку.
Как это:
// make this duration fifteen minutes before startTime
long futureInMillis = SystemClock.elapsedRealtime() + Long.parseLong(holder.handlerGapTV.getText().toString());
AlarmManager alarmManager = (AlarmManager) holder.itemView.getContext().getSystemService(Context.ALARM_SERVICE);
ArrayList<PendingIntent> intentArray = new ArrayList<PendingIntent>();
for (int i=0; i<10; ++i) {
Intent notificationIntent = new Intent(holder.itemView.getContext(), NotificationARBroadcastReceiver.class);
notificationIntent.putExtra(NotificationARBroadcastReceiver.NOTIFICATION, holder.getNotification(i));
PendingIntent pendingIntent = PendingIntent.getBroadcast(holder.itemView.getContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent);
intentArray.add(pendingIntent);
}
Long.parseLong(holder.handlerGapTV.getText().toString())
был рассчитан как:
try {
Date date3 = todayTimeDateFormat.parse(todayTimeDate);
Date date4 = todayTimeDateFormat.parse(newTime);
holder.handlerGap = date4.getTime() - date3.getTime();
holder.handlerGapTV.setText(String.valueOf(holder.handlerGap));
holder.handlerGapTV.setVisibility(View.INVISIBLE);
Log.d("handlerGapTV.getText()", holder.handlerGapTV.getText().toString());
} catch (ParseException e) {
e.printStackTrace();
}
вот getNotification()
:
private Notification getNotification(int repeat) {
mBuilder =
new NotificationCompat.Builder(itemView.getContext())
.setSmallIcon(R.mipmap.app_icon_1)
.setContentTitle("Notif title")
.setContentText("Notif text");
Intent resultIntent = new Intent(itemView.getContext(), AcceptedRequest.class);
// Because clicking the notification opens a new ("special") activity, there's
// no need to create an artificial back stack.
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
itemView.getContext(),
repeat,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
return mBuilder.build();
}
и вот NotificationARBroadcastReceiver.java
:
public class NotificationARBroadcastReceiver extends BroadcastReceiver {
public static String NOTIFICATION = "notification";
@Override
public void onReceive(Context context, Intent intent) {
Random random = new Random();
int m = random.nextInt(9999 - 1000) + 1000;
NotificationManager mNotifyMgr =
(NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
Notification notification = intent.getParcelableExtra(NOTIFICATION);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
mNotifyMgr.notify(m, notification);
}
}
Проблема в том, что Long.parseLong(holder.handlerGapTV.getText().toString())
отличается для CardView
на котором кнопка и предположим, что если она 100
секунд для кнопки card1, то это 65
для кнопки card2 и, если я сначала нажму кнопку card2 и кнопку card2 после нее, уведомление будет установлено в соответствии со временем card2, т.е. 100
и уведомление о времени card1 никогда не отображается!
Что я хочу, чтобы показать все уведомления, если оно установлено после 100
секунды или 65
,
Пожалуйста, помогите мне с этим.
1 ответ
Ну, изменив код из этого:
long futureInMillis = SystemClock.elapsedRealtime() + Long.parseLong(holder.handlerGapTV.getText().toString());
AlarmManager alarmManager = (AlarmManager) holder.itemView.getContext().getSystemService(Context.ALARM_SERVICE);
ArrayList<PendingIntent> intentArray = new ArrayList<PendingIntent>();
for (int i=0; i<10; ++i) {
Intent notificationIntent = new Intent(holder.itemView.getContext(), NotificationARBroadcastReceiver.class);
notificationIntent.putExtra(NotificationARBroadcastReceiver.NOTIFICATION, holder.getNotification(i));
PendingIntent pendingIntent = PendingIntent.getBroadcast(holder.itemView.getContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent);
intentArray.add(pendingIntent);
}
к этому:
int m = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);
// make this duration fifteen minutes before startTime
long futureInMillis = SystemClock.elapsedRealtime() + Long.parseLong(holder.handlerGapTV.getText().toString());
ArrayList<PendingIntent> intentArray = new ArrayList<PendingIntent>();
Intent notificationIntent = new Intent(holder.itemView.getContext(), NotificationARBroadcastReceiver.class);
notificationIntent.putExtra(NotificationARBroadcastReceiver.NOTIFICATION, holder.getNotification());
PendingIntent pendingIntent = PendingIntent.getBroadcast(holder.itemView.getContext(), m, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) holder.itemView.getContext().getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent);
работал на меня!