Я занимаюсь разработкой приложения, в котором мне нужно каждые 3 минуты запускать фоновый сервис для извлечения некоторых данных в формате JSON с сервера.
Сейчас, в целях тестирования, я пытаюсь подключиться к сервису. показывать уведомление каждые 30 секунд. Но, как ни странно, он вызывает службу и показывает уведомление только 3-4 раза и перестает показывать дальнейшие уведомления после этого.
Я использую AlarmManager
для выполнения этой задачи.
MainActivity
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import com.google.android.material.tabs.TabLayout;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
import com.matrix.msnimble.service.CaseReceiver;
import com.matrix.msnimble.ui.main.SectionsPagerAdapter;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
ViewPager viewPager = findViewById(R.id.view_pager);
viewPager.setAdapter(sectionsPagerAdapter);
TabLayout tabs = findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);
Intent caseReceiver = new Intent(getApplicationContext(), CaseReceiver.class);
PendingIntent recurringCaseReceiver = PendingIntent.getBroadcast(getApplicationContext(), 0, caseReceiver, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarms = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarms.setInexactRepeating (AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 30000, recurringCaseReceiver);
}
}
BroadcastReceiver
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class CaseReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent pushPullService = new Intent(context, PushPullService.class);
context.startService(pushPullService);
}
}
Сервис
import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.util.Log;
import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import com.matrix.msnimble.MainActivity;
import com.matrix.msnimble.R;
public class PushPullService extends IntentService {
public PushPullService() {
super(PushPullService.class.getName());
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
createNotificationChannel();
showNotification();
}
private void showNotification(){
Intent intent = new Intent(this.getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this.getApplicationContext(), 0, intent, 0);
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.mipmap.icon );
Notification builder = new NotificationCompat.Builder(this.getApplicationContext(), "casenotification")
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle("New Case(s)")
.setContentText("You have received 5 fresh cases")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setDefaults(Notification.DEFAULT_ALL)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setLargeIcon(largeIcon)
.setAutoCancel(true).build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this.getApplicationContext());
try {
int max = 10000;
int min = 1;
int range = max - min + 1;
notificationManager.notify((int)(Math.random() * range) + min, builder);
}catch
(Exception e){Log.d("--->",e.getMessage());}
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "casenotification";
String description = "com.matrix.msnimble";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel("casenotification", name, importance);
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC );
channel.setDescription(description);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
}
AndroidManifest
<service
android:name=".service.PushPullService"
android:enabled="true"
android:exported="true">
</service>
<receiver android:name=".service.CaseReceiver" ></receiver>
Снимок экрана уведомления на устройстве