I'm trying to send notifications using AlarmManager and they are being sent very late. Sometimes they do get sent around the time they're supposed to be sent, but other times they're being sent 2 hours or even 6 hours later than the original time that was set.
This is the BroadcastReceiver() for the notification:
#RequiresApi(Build.VERSION_CODES.O)
class NotificationAlarmReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val i = Intent(context, SplashActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
val pendingIntent = PendingIntent.getActivity(
context,
0,
i,
PendingIntent.FLAG_IMMUTABLE
)
val builder = NotificationCompat.Builder(context, Constants.NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification_icon)
.setColor(ContextCompat.getColor(context, R.color.colorPrimary))
.setContentTitle(context.getString(R.string.notification))
.setContentText(NOTIFICATION_CONTEXT_TEXT)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setStyle(NotificationCompat.BigTextStyle().bigText(NOTIFICATION_CONTEXT_TEXT))
.setContentIntent(pendingIntent)
.setAutoCancel(true)
with(NotificationManagerCompat.from(context)) {
notify(Constants.NOTIFICATION_NOTIFICATION_ID, builder.build())
}
}
companion object {
private val NOTIFICATION_CONTEXT_TEXT: String by lazy { "Lorem ipsum" }
}
}
And this is the code for sending the notification:
notificationCalendar = Calendar.getInstance()
notificationCalendar[Calendar.HOUR_OF_DAY] = 8
notificationCalendar[Calendar.MINUTE] = 0
notificationCalendar[Calendar.SECOND] = 0
notificationCalendar[Calendar.MILLISECOND] = 0
if ((Calendar.getInstance().timeInMillis - notificationCalendar.timeInMillis) > 0) {
notificationCalendar.add(Calendar.DAY_OF_MONTH, 1)
}
notificationAlarmMgr = context.getSystemService(Context.ALARM_SERVICE) as? AlarmManager
notificationAlarmIntent = Intent(
context,
NotificationAlarmReceiver::class.java
).let { intent ->
PendingIntent.getBroadcast(
context,
NOTIFICATION_ID,
intent,
PendingIntent.FLAG_IMMUTABLE
)
}
notificationAlarmMgr?.setInexactRepeating(
AlarmManager.RTC_WAKEUP,
notificationCalendar.timeInMillis,
AlarmManager.INTERVAL_DAY,
notificationAlarmIntent
)
Why are my notifications being sent so late? What can I do to ensure they get sent at least within a minute of the desired time?
Related
I'm trying stopping running coroutineWorker from notification button. I tried 3 methods and 2 of them calls "Result.failure()" & working fine. However another one doesn't.
Below CoroutineWorker shows foregroundInfo and Starts ringtone.
class RingWork(context: Context, params: WorkerParameters) : CoroutineWorker(context, params) {
companion object {
val ALARM_CHANNEL_ID = "alarm_channel6"
}
lateinit var ringtoneSound: Ringtone
val context = applicationContext
#RequiresApi(Build.VERSION_CODES.Q)
override suspend fun doWork(): Result {
return try {
val alarmId = inputData.getInt("alarmId", 0)
val notificationMgr =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
//CHANNEL
val alarmChannel = NotificationChannel(
ALARM_CHANNEL_ID, "alarm" ,NotificationManager.IMPORTANCE_HIGH
)
alarmChannel.setSound(null, null)
alarmChannel.enableVibration(false)
alarmChannel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC
notificationMgr.createNotificationChannel(alarmChannel)
val fullScreenIntent = Intent(context, LockscreenActivity::class.java).putExtra("alarmId", alarmId)
//This calls "failure" properly
val fullScreenPendingIntent = PendingIntent.getActivity(context, 0, fullScreenIntent,
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)
//This calls "failure" properly
val stop1PendingIntent =
WorkManager.getInstance(context).createCancelPendingIntent(getId())
val s2Intent = Intent(context, StopAlarmReceiver::class.java).putExtra("alarmId", alarmId)
//This is not.
val stop2PendingIntent = PendingIntent.getBroadcast(context, 1, s2Intent,
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT)
val builder = NotificationCompat.Builder(context, ALARM_CHANNEL_ID)
.setSmallIcon(R.drawable.ic_stat_name)
.setContentTitle("title")
.setFullScreenIntent(fullScreenPendingIntent, true)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setCategory(NotificationCompat.CATEGORY_ALARM)
.setAutoCancel(true)
.setSound(null)
.setVibrate(null)
.addAction(R.drawable.ic_stat_name, "Stop1", stop1PendingIntent)
.addAction(R.drawable.ic_stat_name, "Stop2", stop2PendingIntent)
setForeground(
ForegroundInfo(1999999, builder.build(), FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK)
)
ringtoneSound =
RingtoneManager.getRingtone(context, RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM))
ringtoneSound.play()
delay(30000L)
ringtoneSound.stop()
Result.success()
} catch (e: Exception) {
Result.failure()
} finally {
cleanup()
}
}
fun cleanup(){
ringtoneSound.stop()
}
}
In LockScreenActivity, there is a button to stop ringtone.
binding.stoppingbutton.setOnClickListener {
val workMgr = WorkManager.getInstance(applicationContext)
workMgr.cancelUniqueWork("RingWork-$alarmId")
finish()
}
This calls "result.failure" and "finally" then ringtone will stop, notification will disapear. working fine.
However, if I press "Stop2" button on the notification.
class StopAlarmReceiver: BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val alarmId = intent.getIntExtra("alarmId", 0)
val workMgr = WorkManager.getInstance(context)
workMgr.cancelUniqueWork("RingWork-$alarmId")
}
}
It cancels worker, but it won't call "result.failure" and "finally", so ringtone won't stop. Notification also won't disappear.
fullScreenPendingIntent and stop2PendingIntent are doing the same thing, but why it won't behave same?
You can edit your PendingIntent like this and it will trigger onReceive:
val stop2PendingIntent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
PendingIntent.getBroadcast(context, downloadFile.productId, cancelIntent, PendingIntent.FLAG_IMMUTABLE)
else
PendingIntent.getBroadcast(context, downloadFile.productId, cancelIntent, 0)
Hi I am implementing a alarm system in my app. I have an alarm manager that triggers a notification everyday in a certain time. It doesn't work as it intended the notification is not displayed at all. Please can anyone help me with these thank you.
my Alarm set code:
val calender = Calendar.getInstance()
calender[Calendar.HOUR_OF_DAY] = hour
calender[Calendar.MINUTE] = minute
calender[Calendar.SECOND] = 0
calender[Calendar.MILLISECOND] = 0
val pendingIntent = PendingIntent.getBroadcast(
requireContext(),
0,
intent,
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
)
alarmManager.setInexactRepeating (
AlarmManager.RTC_WAKEUP,
calender.timeInMillis,
AlarmManager.INTERVAL_DAY,
pendingIntent
)
Broadcast receiver:
class AlarmReceiver : BroadcastReceiver() {
#RequiresApi(Build.VERSION_CODES.O)
override fun onReceive(context: Context?, p1: Intent?) {
val notificationManager =
context?.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val notificationCompatBuilder =
NotificationCompat.Builder(context, Constants.NOTIFICATION_CHANNEL_ID)
notificationCompatBuilder
.setContentTitle("Prescript")
.setContentText("Medicine time.")
.setSmallIcon(R.mipmap.ic_launcher).priority = NotificationCompat.PRIORITY_DEFAULT
notificationManager.notify(666, notificationCompatBuilder.build())
Log.d("TAG", "onReceive: $notificationCompatBuilder")
// Toast.makeText(context, "Alarm....", Toast.LENGTH_LONG).show();
}
}
I'm trying to set unit test for my android app. I have to test a notifications services that looks like that :
This class provide function to set notification in a context
class DeadlineNotification {
companion object {
private lateinit var alarmManager: AlarmManager
private lateinit var pendingIntent: PendingIntent
/*
Create a notification channel for reminder notifications
Creating an existing notification channel with its original values performs no operation,
so it's safe to call this code when starting an app.
*/
fun createNotificationChannel(context: Context){
val channelName :CharSequence = "reminders channel"
val description = "channel for reminders notifications"
val channel = NotificationChannel("remindersChannel", channelName, NotificationManager.IMPORTANCE_DEFAULT)
val notificationManager = context.getSystemService(NotificationManager::class.java)
channel.description=description
notificationManager.createNotificationChannel(channel)
}
/*
Set a notification that will be triggered in a given time in ms.
you can pass a title/description and Id in parameter
*/
fun setNotification(timeMS: Long, title: String, description: String, id: Int, context: Context){
alarmManager = context.getSystemService(AppCompatActivity.ALARM_SERVICE) as AlarmManager //this get an service instance of AlarmManager
val intent = Intent(context, ReminderBroadcastReceiver::class.java) //this create an intent of broadcast receiver
//Adding extra parameter that will be used in the broadcase receiver to create the notification
intent.putExtra("title", title)
intent.putExtra("description", description)
intent.putExtra("id", id)
//set the receiver as pending intent
pendingIntent = PendingIntent.getBroadcast(context, id, intent, PendingIntent.FLAG_IMMUTABLE)
//set an alarm that will wake up the pending intent (receiver)
alarmManager.setExact(AlarmManager.RTC_WAKEUP, timeMS, pendingIntent)
}
fun getAlarmManager():AlarmManager = this.alarmManager
}
}
setNotification will set an Alarm that will launch an intent of ReminderBroadcastReceiver :
//this class is a BroadcastReceiver that will send a notification when it is triggered
class ReminderBroadcastReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
var channelId = "remindersChannel"
//retrieving some parameters for the notification
var title = intent!!.getStringExtra("title")
var content = intent!!.getStringExtra("description")
var notifId = intent!!.getIntExtra("id", 0)
val intent2 = Intent(context, MainActivity::class.java)
intent2!!.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
//this is the intent where the user will be send when clicking on the notification
val pendingIntent = PendingIntent.getActivity(context, 0, intent2, PendingIntent.FLAG_IMMUTABLE)
//Builder of the notification
val notifBuilder = NotificationCompat.Builder(context!!, channelId)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentTitle(title + notifId.toString())
.setContentText(content)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.setCategory(NotificationCompat.CATEGORY_REMINDER)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
//send the notification
val notificationManager = NotificationManagerCompat.from(context)
notificationManager.notify(notifId, notifBuilder.build())
}
}
This will create a NotificationManager and send the notification.
The code above is working as expected.
Now I'm trying to write unit test for it, I tried this :
#RunWith(AndroidJUnit4::class)
class DeadlineNotificationTest {
#Test
fun `notification is triggered at currentTime and redirect to MainActivity`() {
val activity: MainActivity = Robolectric.setupActivity(MainActivity::class.java) //main Activity
DeadlineNotification.setNotification(System.currentTimeMillis(), "title", "empty description", 1, activity) //this create an alarm
//here we're looking for the notificationManager that have been
val notificationService: NotificationManager = activity.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val shadowNotificationManager =
shadowOf(notificationService)
assertEquals(1, shadowNotificationManager.size())
val notification: Notification = shadowNotificationManager.allNotifications[0]
val contentIntent: PendingIntent = notification.contentIntent
val nextIntent = shadowOf(contentIntent).savedIntent
val nextClassName = nextIntent.component!!.className
assertEquals(nextClassName, MainActivity::class.java.getName())
}
}
But the first assertion fails, so I assume the shadowNotificationManager cannot see the notification.
Do you have any Idea how I should proceed to have a working test?
I've been looking for this for a couple of hours but I haven't found anything helpful. Right now, I have an activity (called other_recurringreminder) that sets a time (fomartted calendar, HH:mm; string), repetition frequency (int), repetition unit (like minutes, hours, days; string), whether it's on or off (bool), and a name (string)
In other_recurringreminder.kt, I have this function:
fun sendnotification()
{
val channelID = "channel1"
val notificationID = 1
val sharedPreferences: SharedPreferences = getSharedPreferences("sharedPrefs", Context.MODE_PRIVATE)
val builder = NotificationCompat.Builder(this, "channelID")
.setSmallIcon(R.drawable.centr)
.setColor(resources.getColor(R.color.purple))
.setContentTitle(sharedPreferences.getString("Alarm1Name_USERPREFS", "Reminder 1"))
.setContentText(getString(R.string.notif_recurringreminders))
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setAutoCancel(true)
with(NotificationManagerCompat.from(this)) {
notify(notificationID, builder.build())
}
}
Which sends the notification when called.
How can I make it so that my app sends this notification at a time from SharedPreferences, with a title from SP, and repeats every x units from SP?
Should this function be in another kotlin file?
Can this work even after a restart, when my app hasn't yet been opened?If I want to schedule more than one notif with different values, do I need to duplicate anything?
Sorry if it's a dumb question, I'm fairly new to kotlin. and thanks!
you can use alarm manager for it.
first create a class with broadcast receiver like
class AlarmReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent?) {
Log.d("this", "notify")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val intent = Intent(context, AlarmActivity2::class.java)
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
val pendingIntent = PendingIntent.getActivity(context, 0, intent, 0)
val builder = NotificationCompat.Builder(context, "111")
.setSmallIcon(R.drawable.blue_stringart)
.setContentTitle("Alarm is running")
.setAutoCancel(true)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setDefaults(NotificationCompat.DEFAULT_SOUND)
.setDefaults(NotificationCompat.DEFAULT_VIBRATE)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.addAction(R.drawable.ic_baseline_stop_24,"Stop",pendingIntent)
.setContentIntent(pendingIntent)
val notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM)
val r = RingtoneManager.getRingtone(context, notification)
r.play()
val notificationManagerCompat = NotificationManagerCompat.from(context)
notificationManagerCompat.notify(123, builder.build())
}
}
}
after that go to your activity class make 2 method and call in oncreate
private fun setAlarm1() {
var calender: Calendar
calender = Calendar.getInstance()
calender.set(Calendar.HOUR_OF_DAY, PUT_YOUR_ALARM HOUR)
calender.set(Calendar.MINUTE, PUT_YOUR_ALARM MINUTE)
calender.set(Calendar.SECOND, 0)
alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
val thuReq: Long = Calendar.getInstance().timeInMillis + 1
var reqReqCode = thuReq.toInt()
if (calender.timeInMillis < System.currentTimeMillis()) {
calender.add(Calendar.DAY_OF_YEAR, 1)
}
val alarmTimeMilsec = calender.timeInMillis
val intent = Intent(this, AlarmReceiver::class.java)
intent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK)
val pendingIntent = PendingIntent.getBroadcast(this, reqReqCode, intent, 0)
alarmManager.setRepeating(
AlarmManager.RTC_WAKEUP,
calender.timeInMillis,
HERE_PUT_YOUR_HOUR * 60 * 60 * 1000,
pendingIntent
)
private fun createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val name = "Alarmclock Channel"
val description = " Reminder Alarm manager"
val importance = NotificationManager.IMPORTANCE_HIGH
val notificationChannel = NotificationChannel(CHANNELID, name, importance)
notificationChannel.description = description
val notificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(notificationChannel)
}
}
Note - Must to do(go to your app setting and give notification permission on) 1.alarmManager.setRepeating here you can use your alarm type as your wish. 2.requestcode must be unique for each alarm. 3. you must take a alarm time and keep in calender.timeInMillis which is you expecting alarm time.
still problem comments below
Hello guys i'm making app for tracking insulin injections for my mom. I want to make app that will create notification automatically every 7 p.m. Can you advice me how to do it? I tried to google it but i'm only found videos with notifications by clicking button but it will not work for me because i want my app to create notifications even when app is closed so i need something like Services that will always by alive even app is closed
You firstly need to instantiate a Broadcast Receiver class and override their onReceive function.
Don't forget to add this to your manifest.
<receiver android:name=".AlertReceiver" />
Override the function as talked.
class AlertReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
Glide.with(context).asBitmap().load(R.drawable.ic_diagnosis_24dp).into(object : CustomTarget<Bitmap>() {
override fun onLoadCleared(placeholder: Drawable?) {}
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
val notificationHelper = NotificationHelper(context,resource,intent.extras!!.getInt("requestCode"))
val nb = notificationHelper.channelNotification
notificationHelper.manager?.notify(1, nb.build())
}
})
}
}
You can prepare a Notification Helper just in case you want to deal with things in a better format.
class NotificationHelper(base: Context?, healthReportIcon: Bitmap, requestCode: Int) : ContextWrapper(base) {
private val healthIcon = healthReportIcon
private var mManager: NotificationManager? = null
private lateinit var beforeTime: String
private var intentActivity: Class<*>
#TargetApi(Build.VERSION_CODES.O)
private fun createChannel() {
val channel = NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_HIGH)
manager!!.createNotificationChannel(channel)
}
val manager: NotificationManager? get() {
if (mManager == null) {
mManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
}
return mManager
}
val channelNotification: NotificationCompat.Builder get() = NotificationCompat.Builder(applicationContext, channelID)
.setContentTitle("Reminder!")
.setContentText("Update medical records before $beforeTime")
.setSmallIcon(R.drawable.ic_round_local_hospital_24)
.setColor(ContextCompat.getColor(this,R.color.blue_diff))
.setLargeIcon(healthIcon)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setPriority(NotificationCompat.PRIORITY_MAX)
.setTimeoutAfter(1800000)
.setAutoCancel(true)
.setContentIntent(PendingIntent.getActivity(baseContext, 0, Intent(baseContext, intentActivity), 0))
companion object {
const val channelID = "phoneId"
const val channelName = "phoneChannel"
}
init {
when (requestCode) {
0 -> {
beforeTime = "12:30 AM"
}
1 -> {
beforeTime = "6:30 AM"
}
2 -> {
beforeTime = "12:30 PM"
}
3 -> {
beforeTime = "6:30 PM"
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createChannel()
}
intentActivity = if(FirebaseAuth.getInstance().currentUser!=null) {
HomeActivity::class.java
} else{
Splash::class.java
}
}
}
Finally, set or cancel your alarms in your codebase.
private fun startAlarms() {
val cal0 = Calendar.getInstance()
cal0[Calendar.HOUR_OF_DAY]=24
cal0[Calendar.MINUTE]=0
cal0[Calendar.SECOND]=0
val cal1 = Calendar.getInstance()
cal1[Calendar.HOUR_OF_DAY]=6
cal1[Calendar.MINUTE]=0
cal1[Calendar.SECOND]=0
val cal2 = Calendar.getInstance()
cal2[Calendar.HOUR_OF_DAY]=12
cal2[Calendar.MINUTE]=0
cal2[Calendar.SECOND]=0
val cal3 = Calendar.getInstance()
cal3[Calendar.HOUR_OF_DAY]=18
cal3[Calendar.MINUTE]=0
cal3[Calendar.SECOND]=0
if (cal0.before(Calendar.getInstance())) {
cal0.add(Calendar.DATE, 1)
}
if (cal1.before(Calendar.getInstance())) {
cal1.add(Calendar.DATE, 1)
}
if (cal2.before(Calendar.getInstance())) {
cal2.add(Calendar.DATE, 1)
}
if (cal3.before(Calendar.getInstance())) {
cal3.add(Calendar.DATE, 1)
}
val alarmManager: AlarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
val intent = Intent(this, AlertReceiver::class.java)
intent.putExtra("requestCode",0)
alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal0.timeInMillis, PendingIntent.getBroadcast(this, 0,intent , PendingIntent.FLAG_UPDATE_CURRENT))
intent.putExtra("requestCode",1)
alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal1.timeInMillis, PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT))
intent.putExtra("requestCode",2)
alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal2.timeInMillis, PendingIntent.getBroadcast(this, 2,intent, PendingIntent.FLAG_UPDATE_CURRENT))
intent.putExtra("requestCode",3)
alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal3.timeInMillis, PendingIntent.getBroadcast(this, 3, intent, PendingIntent.FLAG_UPDATE_CURRENT))
}
private fun cancelAlarms() {
val alarmManager: AlarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
val intent = Intent(this, AlertReceiver::class.java)
alarmManager.cancel(PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT))
alarmManager.cancel(PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_CANCEL_CURRENT))
alarmManager.cancel(PendingIntent.getBroadcast(this, 2, intent, PendingIntent.FLAG_CANCEL_CURRENT))
alarmManager.cancel(PendingIntent.getBroadcast(this, 3, intent, PendingIntent.FLAG_CANCEL_CURRENT))
PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT).cancel()
PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_CANCEL_CURRENT).cancel()
PendingIntent.getBroadcast(this, 2, intent, PendingIntent.FLAG_CANCEL_CURRENT).cancel()
PendingIntent.getBroadcast(this, 3, intent, PendingIntent.FLAG_CANCEL_CURRENT).cancel()
}
Hope this helped you out. Happy Coding! :)