I've been porting to android for about 3 months, I'm still having some difficulties, I have an app that does an online service using google's firebase, and validates a date in string according to the validation, it creates a notification for the user. At first everything works fine ... but if I clean the app from the memory of the phone, and wait a few minutes it stops working, I can not see what the error is, because if I draw from the memory of the emulator, the errors no longer appear in the android studio lobby for me. I noticed on my personal cell phone, that the service does not stop running until it hits ... but when I clean the app from the memory, if it looks at the processes, it appears as "RESET" and after restarting the reboot, it stops work, do this 2x and give the service to run as well.
This is the code of the service ... that starts with the boot of the phone, or when the user requests the app to use the service, I put validations to not start it if it is already running (I think everything is right) )
public class ShowNotificationService extends Service {
public static AlarmManager alarmManager;
public static PendingIntent pendingIntent;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Calendar cal = Calendar.getInstance();
java.util.Date hora = cal.getTime();
System.out.println("####################"+hora);
boolean alarmeAtivo = (PendingIntent.getBroadcast(this, 22222222, new Intent(this, CriarNotificacao.class), PendingIntent.FLAG_NO_CREATE) == null);
if(alarmeAtivo){
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> TRUUUUUUUUUUUUUUUUE");
AgendarNotificacao(hora, 22222222, "NOVAS", "hora: "+cal.getTime());
}else{
System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> FAAAAAAAAAAAAALSE");
}
}
public void AgendarNotificacao(java.util.Date data, int id, String titulo, String conteudo) {
System.out.println("------------------------------------------------------AGENDAR NOTIFICACAO");
// Obtém um novo calendário e define a data para a data da notificação
Calendar calendar = Calendar.getInstance();
calendar.setTime(data);
// Obtém um alarm manager
alarmManager = (AlarmManager) getApplicationContext().getSystemService(getBaseContext().ALARM_SERVICE);
// Prepare the intent which should be launched at the date
Intent intent = new Intent(this, CriarNotificacao.class);
intent.putExtra("id", String.valueOf(id));
intent.putExtra("titulo", titulo);
intent.putExtra("conteudo", conteudo);
// Obtém o pending intent
pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// Regista o alerta no sistema.
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),1800000, pendingIntent);
}
}
Here is the part that validates the online date in firebase ...
public class CriarNotificacao extends BroadcastReceiver {
SimpleDateFormat dateformat = new SimpleDateFormat("EE MMM dd HH:mm:ss z yyyy",Locale.ENGLISH);
//DateFormat f2 = DateFormat.getDateInstance(DateFormat.FULL, brasil);
Date dateAtual = new Date();
Date dataNotificacao= new Date();
@Override
public void onReceive(final Context context, final Intent paramIntent) {
Firebase firebase = LibraryClass.getFirebase().child("notificacao/");
firebase.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot tasksSnapshot) {
try {
dataNotificacao = dateformat.parse(tasksSnapshot.child("tempo").getValue().toString());
} catch (ParseException e) {
Toast.makeText(context,"Problema: "+e+" "+dataNotificacao,Toast.LENGTH_LONG).show();
System.out.println("ERROOOOOOOOOOOOOOOOOOOOO"+e+" "+dataNotificacao);
}
dataNotificacao = new Date(dataNotificacao.getTime() + 30 * 60 * 1000);
int diferenca = dateAtual.compareTo(dataNotificacao);
if (diferenca<0){
System.out.println("-------------------------------------CRIAR NOTIFICACAO");
Bundle extras = paramIntent.getExtras();
int id = Integer.parseInt(extras.getString("id"));
String titulo = extras.getString("titulo");
String conteudo = extras.getString("conteudo");
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setContentTitle(titulo)
.setContentText(tasksSnapshot.child("msg").getValue().toString())
.setAutoCancel(true);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
mBuilder.setSmallIcon(R.drawable.ic_merc);
Intent resultIntent = new Intent(context, MainActivity.class);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_CANCEL_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(500);
mNotificationManager.notify(id, mBuilder.build());
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
Toast.makeText(context,"Falhou: " + firebaseError.getMessage(),Toast.LENGTH_LONG).show();
}
});
}
}