0

I used lots of method from alarmManager like setRepeating, setInexactRepeating(),setExactAndAllowWhileIdle(),

also this too

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { manager.setAlarmClock(AlarmManager.AlarmClockInfo(c.timeInMillis , null),intentAlarm) }

everything is working well when app is running or in recent, but when i close the app it didn't do anything

I also give manifest permission

<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK" />

I look into this stackoverflow question and it didn't work for me and also from this github project

Please help me i stuck for 3 days and didn't get solution that trigger my alarm when app is closed

here is my full code of implemantation

fun setAlarm(c: Calendar, context: Context, tick:Long, dayChoosen:ArrayList<String>) {
        val manager = context.getSystemService(ALARM_SERVICE) as AlarmManager




        //Notification Broadcast intent
        val intentAlarm = Intent(context, MyReceiver::class.java).let {
            it.putExtra("daysChoosenArrayList", dayChoosen)
            it.putExtra("id", dayChoosen)
            PendingIntent.getBroadcast(context, tick.toInt(), it, PendingIntent.FLAG_ONE_SHOT)
        }


        //alarm fire next day if this condition is not statisfied
    if (c.before(Calendar.getInstance())) {
        c.add(Calendar.DATE, 1)
    }
        //set alarm
    manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, c.timeInMillis, 24*60*60*1000,intentAlarm)


    }

and broadcast receiver

class MyReceiver: BroadcastReceiver() {
override fun onReceive(p0: Context?, p1: Intent?) {
    notification(p0!!,"nono")

}

}

notifcation()

fun notification(context: Context, text:String ){
        // pendingIntent is an intent for future use i.e after
        // the notification is clicked, this intent will come into action
        val intent = Intent(context, MainActivity::class.java)

        // FLAG_UPDATE_CURRENT specifies that if a previous
        // PendingIntent already exists, then the current one
        // will update it with the latest intent
        // 0 is the request code, using it later with the
        // same method again will get back the same pending
        // intent for future reference
        // intent passed here is to our afterNotification class
        val pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)

        

        // checking if android version is greater than oreo(API 26) or not
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            notificationChannel = NotificationChannel(channelId, description, NotificationManager.IMPORTANCE_HIGH)
            notificationChannel.enableLights(true)
            notificationChannel.lightColor = Color.GREEN
            notificationChannel.enableVibration(false)
            notificationManager.createNotificationChannel(notificationChannel)

            builder = Notification.Builder(context, channelId)
                .setContentText(text)

                .setSmallIcon(R.drawable.ic_launcher_background)
                .setLargeIcon(BitmapFactory.decodeResource(context.resources, R.drawable.ic_launcher_background))
                .setContentIntent(pendingIntent)
        } else {

            builder = Notification.Builder(context)
                .setContentText("heheh")
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setLargeIcon(BitmapFactory.decodeResource(context.resources, R.drawable.ic_launcher_background))
                .setContentIntent(pendingIntent)
        }
        notificationManager.notify(1234, builder.build())
    }

0 Answers0