使用该应用程序时会重复发出通知

问题描述 投票:0回答:1

我创建了在共享首选项中设置的特定时间显示的通知。通知在设定的时间正确运行,但是当我大约每分钟使用一次应用时,通知也会显示出来。如果过一会儿我将它们从导航栏中删除,则通知会重新发送。如何仅在设置时间发送通知?我使用的是Android 8 Api 26

这是我在MainActivity的onCreate中初始化的类,用于设置警报管理器

public class NotificationApp {


    private Context CONTEXT;

    public NotificationApp(Context context) {
        this.CONTEXT = context;

    }

    public void SetNotification() {

        SharedPreferencesApp sharedPreferencesApp = new SharedPreferencesApp(CONTEXT);

        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, sharedPreferencesApp.getHourPreferences());
        calendar.set(Calendar.MINUTE, sharedPreferencesApp.getMinutesPreferences() - 1);
        calendar.set(Calendar.SECOND, 1);


        Intent intentNotificationApp = new Intent(CONTEXT, NotificationApp.Notification_reciver.class);
        intentNotificationApp.setAction("MY_NOTIFICATION_MESSAGE");
        PendingIntent pendingIntent = PendingIntent.getBroadcast(CONTEXT, 0, intentNotificationApp, PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager alarmManager = (AlarmManager) CONTEXT.getSystemService(Context.ALARM_SERVICE);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);

    }

在MainActivity的onCreate()中>new NotificationApp(getActivityContext()).SetNotification();

然后是扩展BroadcastReceiver的类


        public Notification_reciver() {

        }

        @SuppressLint("UnsafeProtectedBroadcastReceiver")
        @Override
        public void onReceive(Context context, Intent intent) {
            if (Objects.equals(intent.getAction(), "MY_NOTIFICATION_MESSAGE")) {
                NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

                Intent intentGetNotificationExpired = new Intent(context, ArticoliScaduti.class);
                intentGetNotificationExpired.putExtra("today", true);
                intentGetNotificationExpired.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

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

                NotificationCompat.Builder notificationBuilderExpired = new NotificationCompat.Builder(context, "dispensa_channel")
                        .setSmallIcon(R.drawable.icon)
                        .setContentTitle("title text")
                        .setContentText("body text")
                        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                        .setContentIntent(pendingIntent)
                        .setAutoCancel(true)
                        .addAction(R.drawable.ic_launcher_background, "some_text", pendingIntent);

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    NotificationChannel channel = new NotificationChannel("notificationChannelOne", "channel", NotificationManager.IMPORTANCE_DEFAULT);
                    channel.setDescription("name_channel");

                    ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)).createNotificationChannel(channel);
                    notificationBuilderExpired.setChannelId(channel.getId());
                }

                notificationManager.notify(0, notificationBuilderExpired.build());
            }
        }
    }


}

我创建了在共享首选项中设置的特定时间显示的通知。通知在设定的时间正确运行,但是当我在每个...

android notifications broadcastreceiver android-8.0-oreo
1个回答
0
投票

已解决!问题是初始化服务不止一次,可以通过设置一个参数来解决该问题,该参数设置是否在共享首选项中设置初始化,以便仅将其初始化一次。

© www.soinside.com 2019 - 2024. All rights reserved.