繁体   English   中英

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

[英]Issues with scheduling multiple repeating-notifications in android with AlarmManger

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

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

所以,首先在我的 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();
    }

这是我的 BroadcastReceiver 代码:

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

    }



}

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

  1. 我尝试使用相同的通知通道,然后使用不同的通知通道,但结果是它触发了任何一个通知,然后使用不同的通知通道但结果相同。

  2. 再次尝试使用不同的请求代码,结果与上述相同。

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

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

  5. 我也尝试了 2 个 BroadcastReceivers,但没有同时触发任何通知。

  6. 最后,我尝试使用“意图”object 从 MainActivity 发送意图附加内容,并在接收器中放置一个 if/else 块,但在预定时间再次不会触发任何通知。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM