Answer the question
In order to leave comments, you need to log in
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()
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);
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);
}
<uses-permission android:name="android.permission.WAKE_LOCK" />
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question