M
M
MishaBabets2015-05-29 11:34:34
Android
MishaBabets, 2015-05-29 11:34:34

Notifications in sleep mode?

Good afternoon. I am new to programming. I am writing a program that, at a user-specified time, makes a request to the site database, receives a JSON response, parses it and displays it in notifications. Initially, I did it through Service + TimerTask, but the problem was that when the device is in sleep mode, notifications do not come. I tried waking up the device like this before sending a request to the database:

PowerManager powerManager = (PowerManager) getApplicationContext().getSystemService(POWER_SERVICE);
        PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakelockTag");
        wakeLock.acquire()

and this scheme did not work, notifications still did not come in sleep mode. But they came when the phone was on charge, or connected to a computer. How to wake up the processor without charging, I never found it. I decided to try it through the AlarmManager with the RTC_WAKEUP key:
Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.add(Calendar.SECOND, timeUpdate);
        time = calendar.getTimeInMillis();

        am = (AlarmManager) getApplicationContext().getSystemService(ALARM_SERVICE);
        intentAlarm = new Intent(getApplicationContext(), TimeNotification.class);
am.cancel(pendingIntent);
    am.setRepeating(AlarmManager.RTC_WAKEUP, time, timeUpdate * 1000, pendingIntent);

This also did not help, besides, notifications began to come strangely, sometimes they come, sometimes they don’t, and if they don’t, they accumulate. Those. there is an update period of 1 minute, if the phone is turned off for 5 minutes, then immediately after turning on the first notification comes, and as soon as I reset it, the next one comes and so on 5 times. Although, the number in the notifications is the same:
Notification.Builder builder = new Notification.Builder(mContext.getApplicationContext());

                    Intent notificationIntent = new Intent(mContext.getApplicationContext(), ShowNotification.class);
                   
                    PendingIntent contentIntent = PendingIntent.getActivity(mContext.getApplicationContext(),
                            0, notificationIntent,
                            PendingIntent.FLAG_CANCEL_CURRENT);

                    builder.setContentIntent(contentIntent)
                            .setSmallIcon(R.drawable.notification) 
                            .setTicker(mContext.getString(R.string.notifTicker))
                            .setWhen(System.currentTimeMillis())
                            .setAutoCancel(true)
                            .setSound(uri)
                            .setOnlyAlertOnce(true)
                            .setPriority(1)
                            .setDefaults(Notification.DEFAULT_VIBRATE)
                            .setContentTitle(mContext.getString(R.string.notifTitle))
                            .setContentText(mContext.getString(R.string.notifText)); 

                    Notification notification = builder.build();


                    NotificationManager notificationManager = (NotificationManager) mContext.getApplicationContext()
                            .getSystemService(Context.NOTIFICATION_SERVICE);
                    notificationManager.notify(<b>101</b>, notification);
                }

When it worked through TimerTask, this was not the case, notifications came one at a time.
Then I realized that in fact I don’t need the Service, and you can only run the AlarmManager, and there was an idea that the service itself does not work in sleep mode. But without the service, it also did not work.
Dear colleagues, there are ideas on how to implement processor awakening. Perhaps there is some kind of system queue in which you can put your notification, and it will be executed when the CPU wakes up, or somehow track the CPU wake-up, or some other thoughts on how to correctly implement this type of task?
PS: If there is a short update period, then sometimes notifications come by themselves, i.e. the period is 1 min, and it can come in 10 or 30, which means that some processes somehow wake up the CPU, + somehow wake up the CPU notifications like SMS or Viber.
PPS: Naturally indicated in the manifest
<uses-permission android:name="android.permission.WAKE_LOCK" />

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Copperfield, 2015-05-29
@Copperfield

Try WakefulBroadcastReceiver

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question