使用AlarmManger在android中调度多个重复通知的问题

使用AlarmManger在android中调度多个重复通知的问题,android,broadcastreceiver,android-notifications,repeatingalarm,Android,Broadcastreceiver,Android Notifications,Repeatingalarm,首先,这不是重复,因为我已经实现了那些根本不起作用的解决方案 我正在尝试以每天的间隔创建多个重复通知警报。但是 因此,首先在我的MainActivity.java中,我有一段代码来安排OnDestroy()中的报警,稍后我将对其进行更改: @Override protected void onDestroy() { //1st notification Calendar calendarnoti = Calendar.getInstance();

首先,这不是重复,因为我已经实现了那些根本不起作用的解决方案

我正在尝试以每天的间隔创建多个重复通知警报。但是

因此,首先在我的MainActivity.java中,我有一段代码来安排OnDestroy()中的报警,稍后我将对其进行更改:

  @Override
    protected void onDestroy() {
        //1st notification
        Calendar calendarnoti = Calendar.getInstance();
        calendarnoti.set(Calendar.HOUR_OF_DAY, 19);
        calendarnoti.set(Calendar.MINUTE, 29);
        calendarnoti.set(Calendar.SECOND, 55);
        Intent intent = new Intent(MainActivity.this, Notification_receiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarmManager = (AlarmManager) MainActivity.this.getSystemService(ALARM_SERVICE);
        alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendarnoti.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

        //2nd notification
        Calendar calendarnoti1 = Calendar.getInstance();
        calendarnoti1.set(Calendar.HOUR_OF_DAY, 8);
        calendarnoti1.set(Calendar.MINUTE, 59);
        calendarnoti1.set(Calendar.SECOND, 55);
        Intent intent1 = new Intent(MainActivity.this, Notification_receiver.class);
        PendingIntent pendingIntent1 = PendingIntent.getBroadcast(MainActivity.this, 99, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarmManager1 = (AlarmManager) MainActivity.this.getSystemService(ALARM_SERVICE);
        alarmManager1.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendarnoti1.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent1);

        super.onDestroy();
    }
这是我的广播接收器代码:

age com.techystudios.homework.NotifyBroadcastReceiver;

    import android.app.Notification;
    import android.app.NotificationChannel;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.media.RingtoneManager;
    import android.net.Uri;
    import android.os.Build;

    import androidx.core.app.NotificationCompat;


    import com.techystudios.homework.MainActivity;
    import com.techystudios.homework.R;
    import com.techystudios.homework.data.MyDBHandler;
    import com.techystudios.homework.model.Points;

    import java.util.Calendar;

    import static android.content.Context.MODE_PRIVATE;


    public class Notification_receiver extends BroadcastReceiver {


        int sYear;
        int sMonth;
        int sDate;

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




            //1st notification
            MyDBHandler db = new MyDBHandler(context);
            Calendar currentcal = Calendar.getInstance();
            int Year = currentcal.get(Calendar.YEAR);
            int Month = currentcal.get(Calendar.MONTH) + 1;
            int dayofmonth = currentcal.get(Calendar.DAY_OF_MONTH);
            db.close();

            String notdone = Integer.toString(db.getnotDoneWorks(Year, Month, dayofmonth));


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

            Intent notificationIntent = new Intent(context, MainActivity.class);
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

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

            Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);


            if (notdone.equals("0")) {

                NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "techystudioschannelUnIquEID")
                        .setSmallIcon(R.drawable.ic_notification)
                        .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
                        .setContentTitle("Todo Homework")
                        .setContentText("You don't have any homework dues today.")
                        .setStyle(new NotificationCompat.BigTextStyle().bigText("You don't have any homework dues today."))
                        .setSound(alarmSound)
                        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                        .setContentIntent(pendingIntent)
                        .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    String channelId = "techystudioschannelUnIquEID";
                    NotificationChannel channel = new NotificationChannel(
                            channelId,
                            "Remind me of remaining homeworks before the day ends",
                            NotificationManager.IMPORTANCE_HIGH);
                    notificationManager.createNotificationChannel(channel);
                    builder.setChannelId(channelId);
                }
                notificationManager.notify(0, builder.build());

            } else {

                NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "techystudioschannelUnIquEID")
                        .setSmallIcon(R.drawable.ic_notification)
                        .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
                        .setContentTitle("Todo Homework")
                        .setContentText("Hurry up! The day is about to end and you have " + notdone + " homeworks to finish before the day ends.")
                        .setStyle(new NotificationCompat.BigTextStyle().bigText("Hurry up! The day is about to end and you have " + notdone + " homeworks to finish before the day ends."))
                        .setSound(alarmSound)
                        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                        .setContentIntent(pendingIntent)
                        .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    String channelId = "techystudioschannelUnIquEID";
                    NotificationChannel channel = new NotificationChannel(
                            channelId,
                            "Remind me of remaining homeworks before the day ends",
                            NotificationManager.IMPORTANCE_HIGH);
                    notificationManager.createNotificationChannel(channel);
                    builder.setChannelId(channelId);
                }
                notificationManager.notify(0, builder.build());

            }


            Calendar calendar = Calendar.getInstance();
            SharedPreferences getshrd = context.getSharedPreferences("dtValues", MODE_PRIVATE);
            sYear =  getshrd.getInt("sYear", 0);
            sMonth = getshrd.getInt("sMear", 0);
            sDate = getshrd.getInt("sDate", 0);
            Points points = new Points(context, sYear, sMonth, sDate, calendar);

            MyDBHandler db1 = new MyDBHandler(context);
            String todoWork = Integer.toString(db1.getCount());
            db1.close();

    //        NotificationManager notificationManager1 = (NotificationManager) context
    //                .getSystemService(Context.NOTIFICATION_SERVICE);

    //        Intent notificationIntent1 = new Intent(context, MainActivity.class);
    //        notificationIntent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

            PendingIntent pendingIntent1 = PendingIntent.getActivity(context, 99,
                    notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

            Uri alarmSound1 = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

            if (!todoWork.equals("0")) {
                NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "techystudioschannelUnIquEID1")
                        .setSmallIcon(R.drawable.ic_notification)
                        .setContentTitle("Todo Homework")
                        .setContentText("Good Morning! You have total " + todoWork + " homeworks to complete.")
                        .setStyle(new NotificationCompat.BigTextStyle().bigText("Good Morning! You have total " + todoWork + " homeworks to complete."))
                        .setSound(alarmSound1)
                        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                        .setContentIntent(pendingIntent1)
                        .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    String channelId = "techystudioschannelUnIquEID1";
                    NotificationChannel channel = new NotificationChannel(
                            channelId,
                            "Remind me for completing my homeworks",
                            NotificationManager.IMPORTANCE_HIGH);
                    notificationManager.createNotificationChannel(channel);
                    builder.setChannelId(channelId);
                }
                notificationManager.notify(0, builder.build());
            } else {
                NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "techystudioschannelUnIquEID1")
                        .setSmallIcon(R.drawable.ic_notification)
                        .setContentTitle("Todo Homework")
                        .setContentText("Good Morning! You have no homeworks to complete. Come back to add or chill and relax.")
                        .setStyle(new NotificationCompat.BigTextStyle().bigText("Good Morning! You have no homeworks to complete. Come back to add or chill and relax."))
                        .setSound(alarmSound1)
                        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                        .setContentIntent(pendingIntent1)
                        .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    String channelId = "techystudioschannelUnIquEID1";
                    NotificationChannel channel = new NotificationChannel(
                            channelId,
                            "Remind me for completing my homeworks",
                            NotificationManager.IMPORTANCE_HIGH);
                    notificationManager.createNotificationChannel(channel);
                    builder.setChannelId(channelId);
                }
                notificationManager.notify(0, builder.build());
            }

    }



}
好了,现在我做了什么,结果是什么:

age com.techystudios.homework.NotifyBroadcastReceiver;

    import android.app.Notification;
    import android.app.NotificationChannel;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.media.RingtoneManager;
    import android.net.Uri;
    import android.os.Build;

    import androidx.core.app.NotificationCompat;


    import com.techystudios.homework.MainActivity;
    import com.techystudios.homework.R;
    import com.techystudios.homework.data.MyDBHandler;
    import com.techystudios.homework.model.Points;

    import java.util.Calendar;

    import static android.content.Context.MODE_PRIVATE;


    public class Notification_receiver extends BroadcastReceiver {


        int sYear;
        int sMonth;
        int sDate;

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




            //1st notification
            MyDBHandler db = new MyDBHandler(context);
            Calendar currentcal = Calendar.getInstance();
            int Year = currentcal.get(Calendar.YEAR);
            int Month = currentcal.get(Calendar.MONTH) + 1;
            int dayofmonth = currentcal.get(Calendar.DAY_OF_MONTH);
            db.close();

            String notdone = Integer.toString(db.getnotDoneWorks(Year, Month, dayofmonth));


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

            Intent notificationIntent = new Intent(context, MainActivity.class);
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

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

            Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);


            if (notdone.equals("0")) {

                NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "techystudioschannelUnIquEID")
                        .setSmallIcon(R.drawable.ic_notification)
                        .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
                        .setContentTitle("Todo Homework")
                        .setContentText("You don't have any homework dues today.")
                        .setStyle(new NotificationCompat.BigTextStyle().bigText("You don't have any homework dues today."))
                        .setSound(alarmSound)
                        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                        .setContentIntent(pendingIntent)
                        .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    String channelId = "techystudioschannelUnIquEID";
                    NotificationChannel channel = new NotificationChannel(
                            channelId,
                            "Remind me of remaining homeworks before the day ends",
                            NotificationManager.IMPORTANCE_HIGH);
                    notificationManager.createNotificationChannel(channel);
                    builder.setChannelId(channelId);
                }
                notificationManager.notify(0, builder.build());

            } else {

                NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "techystudioschannelUnIquEID")
                        .setSmallIcon(R.drawable.ic_notification)
                        .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
                        .setContentTitle("Todo Homework")
                        .setContentText("Hurry up! The day is about to end and you have " + notdone + " homeworks to finish before the day ends.")
                        .setStyle(new NotificationCompat.BigTextStyle().bigText("Hurry up! The day is about to end and you have " + notdone + " homeworks to finish before the day ends."))
                        .setSound(alarmSound)
                        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                        .setContentIntent(pendingIntent)
                        .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    String channelId = "techystudioschannelUnIquEID";
                    NotificationChannel channel = new NotificationChannel(
                            channelId,
                            "Remind me of remaining homeworks before the day ends",
                            NotificationManager.IMPORTANCE_HIGH);
                    notificationManager.createNotificationChannel(channel);
                    builder.setChannelId(channelId);
                }
                notificationManager.notify(0, builder.build());

            }


            Calendar calendar = Calendar.getInstance();
            SharedPreferences getshrd = context.getSharedPreferences("dtValues", MODE_PRIVATE);
            sYear =  getshrd.getInt("sYear", 0);
            sMonth = getshrd.getInt("sMear", 0);
            sDate = getshrd.getInt("sDate", 0);
            Points points = new Points(context, sYear, sMonth, sDate, calendar);

            MyDBHandler db1 = new MyDBHandler(context);
            String todoWork = Integer.toString(db1.getCount());
            db1.close();

    //        NotificationManager notificationManager1 = (NotificationManager) context
    //                .getSystemService(Context.NOTIFICATION_SERVICE);

    //        Intent notificationIntent1 = new Intent(context, MainActivity.class);
    //        notificationIntent1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

            PendingIntent pendingIntent1 = PendingIntent.getActivity(context, 99,
                    notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

            Uri alarmSound1 = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

            if (!todoWork.equals("0")) {
                NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "techystudioschannelUnIquEID1")
                        .setSmallIcon(R.drawable.ic_notification)
                        .setContentTitle("Todo Homework")
                        .setContentText("Good Morning! You have total " + todoWork + " homeworks to complete.")
                        .setStyle(new NotificationCompat.BigTextStyle().bigText("Good Morning! You have total " + todoWork + " homeworks to complete."))
                        .setSound(alarmSound1)
                        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                        .setContentIntent(pendingIntent1)
                        .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    String channelId = "techystudioschannelUnIquEID1";
                    NotificationChannel channel = new NotificationChannel(
                            channelId,
                            "Remind me for completing my homeworks",
                            NotificationManager.IMPORTANCE_HIGH);
                    notificationManager.createNotificationChannel(channel);
                    builder.setChannelId(channelId);
                }
                notificationManager.notify(0, builder.build());
            } else {
                NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "techystudioschannelUnIquEID1")
                        .setSmallIcon(R.drawable.ic_notification)
                        .setContentTitle("Todo Homework")
                        .setContentText("Good Morning! You have no homeworks to complete. Come back to add or chill and relax.")
                        .setStyle(new NotificationCompat.BigTextStyle().bigText("Good Morning! You have no homeworks to complete. Come back to add or chill and relax."))
                        .setSound(alarmSound1)
                        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                        .setContentIntent(pendingIntent1)
                        .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    String channelId = "techystudioschannelUnIquEID1";
                    NotificationChannel channel = new NotificationChannel(
                            channelId,
                            "Remind me for completing my homeworks",
                            NotificationManager.IMPORTANCE_HIGH);
                    notificationManager.createNotificationChannel(channel);
                    builder.setChannelId(channelId);
                }
                notificationManager.notify(0, builder.build());
            }

    }



}
  • 我尝试使用相同的通知通道,然后使用不同的通知通道,但结果是它触发了其中一个通知,然后使用不同的通知通道,但结果相同

  • 再次尝试使用不同的请求代码,结果与上面相同

  • 然后我尝试使用不同的通知ID,但两个通知都是在第一个通知的预定时间触发的

  • 然后,我还尝试为NotificationManager和所有这些东西保留相同的对象,而不是创建一个新对象,然后与第三个指针的结果相同。您可以看到注释掉的第二个通知的新对象

  • 我还尝试了2个BroadcastReceivers,但没有同时触发任何通知

  • 最后,我尝试使用“intent”对象从MainActivity发送intent额外消息,并在receiver中放置了if/else块,但在计划的时间不会触发任何通知