1

guys, am currently building an application with compose, and i need to fire a notification at a given time to users, so whenever users click on a button on my list item, i want to get the time of that item and set an alarm pending intent for it, and fire it when the times reaches, So am working with android alarm manager, but whenever i close my application the alarm doesn't broadcast my notification, but if i leave it open, it fires the alarm notification successfully, i dont know what i am doing wrong cause from what i know my application isnt responsible for firing the alarm, cause the android system does this for us,

so here is my code that i have tried with Alarm Manager

Funtion to Schedule Notification

`

    @RequiresApi(api = Build.VERSION_CODES.M)
    fun scheduleNotification(calendar: Calendar, context: Context, taskInfo:FixtureAlarm) {


    val alarmManager = context.getSystemService(ALARM_SERVICE) as AlarmManager


    // adding intent and pending intent to go to AlarmReceiver Class in future
    val intent = Intent(context, FixtureNotificationReceiver::class.java)
    intent.putExtra("fixture", taskInfo)
    val pendingIntent = PendingIntent.getBroadcast(context, taskInfo.id, intent,
        PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)

    // when using setAlarmClock() it displays a notification until alarm rings and when pressed it       takes us to mainActivity
    val mainActivityIntent = Intent(context, MainActivity::class.java)
    val basicPendingIntent = PendingIntent.getActivity(context, taskInfo.id, mainActivityIntent,     PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
    // creating clockInfo instance
    val clockInfo = AlarmManager.AlarmClockInfo(Calendar.getInstance().also { it.add(Calendar.SECOND,10) }.timeInMillis, basicPendingIntent)
    // setting the alarm
    alarmManager.setExact(AlarmManager.RTC_WAKEUP,Calendar.getInstance().also { it.add(Calendar.SECOND,10) }.timeInMillis, pendingIntent)
    Toast.makeText(context, "Scheduled ", Toast.LENGTH_LONG).show()
    }

`

My BroadCast Receiver

    class FixtureNotificationReceiver() : BroadcastReceiver() {

    private var notificationManager: NotificationManagerCompat? = null

    override fun onReceive(p0: Context?, p1: Intent?) {

        val taskInfo = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            p1?.getParcelableExtra("fixture",FixtureAlarm::class.java)
        } else {
            p1?.getParcelableExtra("fixture") as? FixtureAlarm
        }
        Log.d("SVTRECIEVED","ALARTM RECIEVED ${taskInfo?.homeTeam.toString()}")
        // tapResultIntent gets executed when user taps the notification
        if(taskInfo!=null) {
            val tapResultIntent = Intent(p0, MainActivity::class.java)
            tapResultIntent.putExtra("fixture", taskInfo)
            tapResultIntent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP
            val pendingIntent: PendingIntent =
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    getActivity(p0, 0, tapResultIntent, FLAG_UPDATE_CURRENT or FLAG_IMMUTABLE)
                } else {
                    getActivity(p0, 0, tapResultIntent, PendingIntent.FLAG_UPDATE_CURRENT)
                }


            val notification = p0?.let {
                NotificationCompat.Builder(it, FIXTURESCHANNEL_ID)
                    .setContentTitle("Fixture Reminder")
                    .setContentText("${taskInfo?.awayTeam} VS ${taskInfo.homeTeam} is about to start, open SportVectru and get live updates ")
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setAutoCancel(true)
                    .setPriority(NotificationCompat.PRIORITY_HIGH)
                    .setContentIntent(pendingIntent)
                    .setDefaults(NotificationCompat.DEFAULT_ALL)
                    .setDefaults(NotificationCompat.DEFAULT_SOUND)
                    .setDefaults(NotificationCompat.DEFAULT_VIBRATE)
                    .setPriority(NotificationCompat.PRIORITY_HIGH).build()}
                val notificationtone = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM)
                val r = RingtoneManager.getRingtone(p0, notificationtone)
                r.play()



            notificationManager = p0?.let { NotificationManagerCompat.from(it) }

            notification?.let { taskInfo.let { it1 -> if (ActivityCompat.checkSelfPermission(
                    p0,
                    Manifest.permission.POST_NOTIFICATIONS
                ) != PackageManager.PERMISSION_GRANTED
            ) {
                Toast.makeText(p0, "Permission for showing notification is disabled", Toast.LENGTH_SHORT).show()
                return
            }

                notificationManager?.notify(it1.id, it)

                }
            }
        }
    }
    }

Added it to my Manifest `

    <receiver android:name=".dormain.notifications.pendingNotification.FixtureNotificationReceiver"
        android:enabled="true"
     />

`

I even granted Permission

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

But it still doesnt work, notification is never shown when application is closed.

I have also disabled battery Optimization for my application, doesnt work.

Tule Simon
  • 11
  • 1

0 Answers0