Предотвратить старые уведомления от стрельбы - PullRequest
0 голосов
/ 17 апреля 2020

Я создаю приложение, которое я планирую уведомления на день, скажем, утром, вечером и ночью - проблема в том, что если я открыл приложение ночью старые уведомления о утренних и вечерних шоу. я использую ниже код

    val alarmMgr = MyApplication.appContext.getSystemService(Context.ALARM_SERVICE) as AlarmManager?
    val intent = Intent(MyApplication.appContext, MyAlarmReceiver::class.java)
    intent.putExtra("Title",title)
    intent.putExtra("Body",body)
    intent.putExtra("Id",id)
    intent.putExtra("Sound",sound)
    val pendingIntent = PendingIntent.getBroadcast(MyApplication.appContext, id, intent, 0)
    alarmMgr!!.setRepeating(AlarmManager.RTC_WAKEUP,time,AlarmManager.INTERVAL_DAY,pendingIntent) 

с этой частью внутри onReceive тревоги

private fun sendNotification( intent: Intent) {

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

            val notificationID = intent.extras!!.getInt("Id")!!
            val title = intent.extras!!.getString("Title")
            val body = intent.extras!!.getString("Body")
            val sound = intent.extras!!.getString("Sound")

            val channelID = "com.ebookfrenzy.notifydemo.news"

            val resultIntent = Intent(MyApplication.appContext, MainActivity::class.java)

            resultIntent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK

            val pendingIntent = PendingIntent.getActivity(
                MyApplication.appContext,
                0,
                resultIntent,
                PendingIntent.FLAG_UPDATE_CURRENT)

            val notification = Notification.Builder(MyApplication.appContext,
                channelID)
                .setContentTitle(title)
                .setContentText(body)
                .setSmallIcon(android.R.drawable.ic_dialog_info)
                .setChannelId(channelID)
                .setContentIntent(pendingIntent)
                .build()

             sound?.let {
                 notification.sound =  Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + MyApplication.appContext.getPackageName() + "/raw/$it")
             }

        var notificationManager = MyApplication.appContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

        notificationManager?.notify(notificationID, notification)


    } else {


    }
 }
...