0

I have an Android app where i schedule some notifications at specific times every day with alarmManager and receiver like this

fun addNot() {
    val alarmMgr = context!!.getSystemService(Context.ALARM_SERVICE) as AlarmManager?
    val intent = Intent(this.context!!, MyAlarmReceiverTest::class.java)
    val pendingIntent = PendingIntent.getBroadcast(this.context!!, 0, intent, 0) 
    val current = Calendar.getInstance()
    val time =  createDate("13:58:00")
    if(time < current) {
        time.add(Calendar.DATE,1) 
    }

    alarmMgr!!.setRepeating(AlarmManager.RTC_WAKEUP, time.timeInMillis ,AlarmManager.INTERVAL_DAY,pendingIntent)
}

fun createDate(str: String):Calendar {

    val separated = str.split(":").toTypedArray() 
    val datetimeToAlarm = Calendar.getInstance()  
    datetimeToAlarm.set(Calendar.HOUR_OF_DAY,separated[0].toInt()) 
    datetimeToAlarm.set(Calendar.MINUTE,separated[1].toInt()) 
    datetimeToAlarm.set(Calendar.SECOND,separated[2].toInt()) 
    return datetimeToAlarm

}

With this receiver

 class MyAlarmReceiverTest : BroadcastReceiver() {
   override fun onReceive(context: Context?, intent: Intent?) {

    val notification = Notification.Builder(MyApplication.appContext)
        .setContentTitle("title goes here")
        .setContentText("this is content")
        .setSmallIcon(R.mipmap.app_logo_final)
        .setLargeIcon(
            BitmapFactory.decodeResource(
                MyApplication.appContext.getResources(),
                R.mipmap.app_logo_final
            )
        )
        .setContentIntent(null)
        .build()


     notification.sound =  Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + MyApplication.appContext.getPackageName() + "/raw/a")

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

    notificationManager?.notify(45545454, notification)
 }
}

And the manifest

        <receiver
        android:name=".MyAlarmReceiverTest"
        android:enabled="true"
        android:stopWithTask="false">
        <intent-filter>
            <action android:name="com.simplemobiletools.applauncher.sendbroadcast" />
            <action android:name="android.intent.action.BOOT_COMPLETED" />

            <category android:name="android.intent.category.DEFAULT" />

            <action android:name="android.intent.action.QUICKBOOT_POWERON" />
        </intent-filter>
    </receiver>

Questions :

1- the above notification sometimes triggers and sometimes not , why ?

2- should i use job or workmanager for this ?

App support is from api 21 if this matters

sheko
  • 516
  • 4
  • 15
  • Any help please , i can't get around this for some weeks – sheko May 29 '21 at 12:30
  • Alarms do not fire when the device is idle in Doze mode. Any scheduled alarms will be deferred until the device exits Doze, try to follow [this](https://stackoverflow.com/questions/33055129/how-to-show-a-notification-everyday-at-a-certain-time-even-when-the-app-is-close/55910596#55910596) answer also check [this](https://developer.android.com/training/scheduling/alarms) from google – Jimale Abdi May 31 '21 at 18:42

0 Answers0