Я хочу запускать уведомление каждый час.
Я использую setRepeating в сервисе AlarmManager, проблема в том, что когда я закрываю свое приложение, менеджер не передает в BroadcastReceiver.
Мой BroadcastReceiver:
public class MyReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
Notification notification = new NotificationCompat.Builder(context, "notify_001")
.setContentTitle("URL Database")
.setSmallIcon(R.drawable.ic_launcher)
.setStyle(new NotificationCompat.BigTextStyle())
.setContentText("You didn't use the URLDatabase app for a while.\nYour urls feel lonely.")
.setContentIntent(pendingIntent).build();
NotificationManager manager = (NotificationManager) context.getSystemService(Service.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
NotificationChannel channel = new NotificationChannel("notify_001",
"URLDatabaseNotification",
NotificationManager.IMPORTANCE_DEFAULT);
manager.createNotificationChannel(channel);
}
manager.notify(0, notification);
Log.i("URLDatabase", "Received");
}
}
Мой код для AlarmManager (я использую это в onCreate своей деятельности):
calendar = Calendar.getInstance();
intent = new Intent(this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
am = (AlarmManager) this.getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000*60*60, pendingIntent);
Нужно ли создавать пользовательскую службу, которая будет работать вфон