Skip to content Skip to sidebar Skip to footer

Alarmmanager With Notification In Android Does Not Show Any Notifications

I try to create daily notification for my application. In order to test the application I set the interval for the notifications to 15 minutes. Unfortunately, no notification is sh

Solution 1:

Firstly, let's start by enabling our BroadcastReceiver in Manifest by changing our code to this:

<receiverandroid:name= ".DailyNotificationReceiver"android:enabled="true"android:exported="true"><intent-filter><actionandroid:name="android.intent.action.BOOT_COMPLETED"/></intent-filter></receiver>

Secondly, I am a bit unsure of the purpose of the ActivityNotification Activity. It would be more than enough to create a simple class like Alarms.

From there you can define all the methods you need to set up an alarm. Something like this:

classInternetDaysLeftAlarm@Injectconstructor(
    @ApplicationContextval context: Context
) {
    /**
     * Function is used to schedule one-time Alarm that will trigger on specific time.
     *
     * @param hourOfDay -> parameter defines what o'clock should the alarm be triggered at. (24-hour)
     *      default value is:
     * @see INTERNET_DAYS_LEFT_ALARM_DEFAULT_TRIGGER_HOUR
     */funscheduleAlarm(hourOfDay: Int = INTERNET_DAYS_LEFT_ALARM_DEFAULT_TRIGGER_HOUR) {
        val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
        val intent = Intent(context, InternetDaysLeftReceiver::class.java)
        intent.action = INTENT_ACTION_INTERNET_DAYS_LEFT_ALARM
        val pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0)

        val msUntilTriggerHour: Long = TimeUnit.MINUTES.toMillis(minutesUntilOClock(hourOfDay))

        // Calculating and adding jitter in order to ease load on serverval jitter: Long = TimeUnit.MINUTES.toMillis(Random.nextInt(0, 420).toLong())

        val alarmTimeAtUTC: Long = System.currentTimeMillis() + msUntilTriggerHour + jitter

        // Enabling BootReceiverval bootReceiver = ComponentName(context, BootReceiver::class.java)
        context.packageManager.setComponentEnabledSetting(
            bootReceiver,
            PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP
        )

        /**
         * Schedules the Alarm based on Android Version.
         *
         * As per AlarmManager documentation to guarantee Alarm execution at specified time we use following methods:
         *
         * @see AlarmManager.setExactAndAllowWhileIdle -> Android.M [API 23] and above.
         * @see AlarmManager.setAlarmClock -> Everything below Android M.
         */if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, alarmTimeAtUTC, pendingIntent)
        } else {
            alarmManager.setAlarmClock(AlarmManager.AlarmClockInfo(alarmTimeAtUTC, pendingIntent), pendingIntent)
        }
    }

Notifications can be handle by:

  • Creating a separate Notifications class

or

  • Handling all the notification things like creating channel and sending them inside BroadcastReceiver when you receive an Alarm.

Post a Comment for "Alarmmanager With Notification In Android Does Not Show Any Notifications"