无法在不同时间收到多个通知

时间:2016-12-02 03:02:57

标签: android notifications alarmmanager android-notifications notificationmanager

我试图在用户点击按钮时在特定时间设置通知。

像这样:

// 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不同,并假设如果卡1的按钮为100秒,那么它对于card2的按钮是65,如果我先按下card2按钮,然后按下card2按钮,则会根据card2的时间设置通知,即100,并且卡片的定时通知永远不会显示!

我想要的是显示每个通知是否在100秒或65之后设置。

请帮助我。

1 个答案:

答案 0 :(得分:0)

好吧,改变代码:

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

为我工作!