【问题标题】:Notifications repeated while using the app使用应用程序时重复通知
【发布时间】:2019-11-04 13:55:29
【问题描述】:

我创建了在共享首选项中设置的特定时间显示的通知。 通知在设定的时间正常工作,但是当我大约每分钟使用该应用程序时也会显示它们。 如果我在一段时间后从导航栏中删除它们,则会重新发送通知。 如何仅在我设置的时间发送通知? 我在 Android 8 Api 26 中

这是我在MainActivity的onCreate中初始化的类,用来设置Alarm Manager

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】:

    解决了!问题是要多次初始化服务,通过在共享首选项中设置一个设置初始化或不设置的参数来解决,以便它只初始化一次。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-12
      • 1970-01-01
      • 2018-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-17
      相关资源
      最近更新 更多