D
D
DragoN DragoN2021-10-08 07:23:35
Java
DragoN DragoN, 2021-10-08 07:23:35

How to send notifications at a given time?

Hello!

I am faced with the task of sending notifications at a given time by the user. For example, a user can create several tasks for a certain time, and the application sends a notification when this time comes. All actions are performed strictly in the application without the use of Firebase Cloud Messages

. I created a BroadcastReceiver with a BOOT_COMPLETED filter in which the Service is started. In the Service itself, I created the logic of running through the database, determining the time, and if the time is reached, it displays a notification

. In general, I understand how it should be, but nothing comes out, so I ask you to deal with my questions:
1. How to make the application work in the background and give out a notification even after a long time, even when the application is not running?
2. Where should the BroadcastReceiver be registered?
3. When I start the intent in the BroadcastReceiver, when working in the background, it gives an error: Not allowed to start service Intent
4. When I work in the application and execute the BroadcastReceiver, the application restarts the activity
5. According to the article , I could not find the permission specified android.permission .SCHEDULE_EXACT_ALARM
6. I don't have canScheduleExactAlarms() in AlarmManager , although it is specified that the method is

BroadcastReceiver, _notification_receiver.java

public void onReceive( Context context, Intent intent )
{
Intent INTENT = new Intent( context, _notification_service.class );
context.startService( INTENT );
}


PendingIntent pendingIntent = PendingIntent.getBroadcast( this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT );

AlarmManager alarmManager = ( AlarmManager ) getSystemService( Context.ALARM_SERVICE );
if (pendingIntent != null && alarmManager != null ) alarmManager.cancel( pendingIntent );
alarmManager.setRepeating( alarmType, time, replace, pendingIntent );

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg Zakharov, 2021-10-11
@blazenn12

1) need more information on the case. What versions minSdkVersion, compileSdkVersion, the response time should be accurate or not, what kind of work it should do, the base is local or not.
2) depending on its type. Some in the manifest, some through code.
3) everything is correct. It is possible to esteem about restrictions
4) without the code it is difficult to advise something.
5.6) if you look, it says "Added in API level 31" below.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question