I'm writing code to display notifications to the user at specific times (just like the Google Calendar app).
I hence created :
a BroadcastReceiver that listens to BOOT_COMPLETED, upon reception it sets an alarm in one minute in order to not overload the device when it is still loading stuff;
a BroadcastReceiver that listens to alarms: the first set one minute after BOOT_COMPLETED, and the next at the next appointment (like in Google Calendar)
So, typically:
BOOT_COMPLETED => launch alarm with a one minute delay
One minute later => the Receiver sets another alarm for the next appointment
Several minutes/hours/days later, the alarm goes off => the Receiver displays a status bar notification
Which means that the status bar notifications are launched from the BroadcastReceiver.
I have read in the doc that they should be launched from Activities or Services : https://developer.android.com/guide/topics/ui/notifiers/notifications.html#Basics
I'm asking for best practice here. Should I create a Service that will be launched by the BroadcastReceiver, and which only purpose will be to launch the status bar notification? My code is working, I just want to create clean code as suggested by Google.
You can add a Notification from a BroadcastReceiver, AFAIK. That should be fairly fast. If StrictMode complains about it, then it might be worth worrying about -- otherwise, you should be OK.
Related
I searched a lot, but couldn't find nothing clear. Every solution seems outdated. In my app, I need this simple task: the user can enable a daily reminder and select a time in the Time Picker.
Everyday in the selected hour the user should receive a simple notification. I only need to read the local database (Room, SQLite) and then show a notification.
I'm using AlarmManager, because the user should receive the notification in the exact time he selected.
Everthing is working fine when the app is open or minimized. When I close the app in the recently used apps (swipe up) I don't receive the notification.
I have a class that creates the alarm with this calling:
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), pendingIntent);
The pendingIntent opens a NotificationReceiver extends BroadcastReceiver class. In the onReceive method I read the db and show the notification. I also tried starting a service in the onReceive method (IntentService or Service) but they work exactly as just using the broadcast receiver.
I put the receiver in the AndroidManifest. Everything is working except when I close the app or restart the system (I registred the BOOT_COMPLETED in the manifest and I have another broadcast receiver that configure the alarm again. If I open the app and keep it open before the notification time, then I receive it. But if the app is still closed, I don't receive it. So I thik the BOOT_COMPLETED is working fine).
The other solutions doesn't seem to work. What can I do to have it working on Android 11 (Pixel 3 device)?
One example of what I need is the Google Keep app, but mine is simpler. I don't see any notification dot telling that the Google Keep is running in background. Still, I get the reminders on time. How the Google Keep works? It's a service always running (and killing the battery)? It uses AlarmManager and BroadcastReceiver? WorkManager? Even if a close the app or restart the phone, I always get the notifications. How can I achieve it on my app?
I am using AlarmManager to trigger a regular action of my app that should run whenever the device is on. Following the documentation I am receiving an intent of type android.intent.action.BOOT_COMPLETED to start the alarm whenever the device starts.
I am aware that the user needs to start the app before it can receive this intent as explained here and in many question on StackOverflow.
But when the user starts the app for the first time want to immediately set the alarm and not wait until the device reboots for the first time.
How can I trigger my alarm setup cleanly when the app is launched for the first time in addition to on BOOT_COMPLETED?
I guess could call the alarm setup in my launcher activity but this seems to bloat the Activity code and I don't want to execute it if the alarms have been setup at BOOT_COMPLETED already.
In my app I want to schedule an Alarm associated with Activity A. When the alarm fires, what should happen depends on the circumstances:
1. if the app is not opened at that time, then display a notification in the notification bar and also start the activity A the next time the app is started
3. if the app is active, start Activity A
Activity A is not a background activity - it requires user interaction.
Ideally, the same behavior would work with more than 1 alarm - i.e. if 2 alarms fired while the app was not in use, two activities would be queued to start once the user starts the app.
I read the Android Docs on AlarmManager, PendingIntents and Notification - I know how to schedule an alarm which fires a notification, but I don't know at all how I would go about
- the "queueing" of activities after an alarm fires
- the "conditional behavior" when an alarm fires (based on whether app is running or not).
It seems like plenty of apps would need this kind of behavior. I hope someone can point me in the right direction.
Found a solution in the "Android Programming: The Big Nerd Ranch Guide" book (Hardy & Phillips). The solution is to use Intent Service that handles the AlarmManager intents, and sends an ordered broadcast. The broadcast is either received by an Activity, or - outside of the app lifetime - by another service, which then sends a notification.
We want to send messages to app users either through notifications, dialog box or image opening up on their screen every 24 hours telling them that our app is running on their phone.
We were looking to use Notification builder but it has limitation that it only works for api 11 and above and half of all app installations today are for earlier api versions. We are trying to find out which would be the best way to go with this.
I'm not sure what "Notification builder" is, but you can certainly use Notification and NotificationManager in any API you want.
So, putting it all together, I would use AlarmManager to fire off an alarm every 24 hours. Set up this alarm when your application runs, and in a BroadcastReceiver which is configured to receive BOOT_COMPLETED. The BOOT_COMPLETED notification allows you to quietly restart the alarm if the device reboots.
The alarm triggers another BroadcastReceiver which puts the notification up. If the user selects the notification, then your application is launched. Mostly, the presence of the notification will be all the reminder your user will need.
My notes say that NotificationManager can pop a View up onto the screen, which could be a dialog. However, I think a simple icon in the status bar would be best, since you're just reminding the user that the application is present.
Oh, p.s., if your application is a service that's running in the background 24/7, then you should also remember to restart it in the BOOT_COMPLETED broadcast.
I have application that needs to show notification in status bar.
I have to set time and date in my app, and on that time and date notification needs to be shown in status bar as somekind of reminder.
I've tried this tutorial, but scheduled event isn't triggered at all.
Is there a way to do this?
I suggest that you use AlarmManager in connection with a SQL database to handle notifications. You could use AlarmManager to schedule alarms which are fetched from your database. You should use a BroadcastReceiver to add alarms when the phone boots in order to handle the situation where the phone is rebooted by the user or system failure.
This howto explains how to use the AlarmManager.