I have an android app that is linked to the firebase notification push and to sqlite, the push that comes to me while the app in foreground is saved correctly in the sqlite, now the push that I get in the background is not saved. Perform a service that I also included in the manifest.
BaseDatosService:
public class BaseDatosService extends Service {
ConexionSQLiteHelper conexionSQLiteHelper;
String titulo;
String descripcion;
Context context;
public BaseDatosService() {
}
public BaseDatosService(Context context, String titulo, String descripcion) {
this.titulo = titulo;
this.descripcion = descripcion;
this.context = context;
}
@Override
public void onCreate() {
super.onCreate();
Log.d("APP", ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Servicio creado...");
conexionSQLiteHelper = new ConexionSQLiteHelper(this);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId){
Log.d("APP", ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Servicio iniciado...");
String titulo = intent.getStringExtra("titulo");
String descripcion = intent.getStringExtra("descripcion");
Log.d("APP", ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> TITULO : " + titulo);
if(MainActivity.class == null) {
conexionSQLiteHelper.guardarNoti(titulo, descripcion);
}
return Service.START_NOT_STICKY;
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
ConexionSQLiteHelper.java
public class ConexionSQLiteHelper extends SQLiteOpenHelper {
SQLiteDatabase db;
ArrayList<Notificacion> notificaciones = new ArrayList<>();
AdapterList adapterList;
public ConexionSQLiteHelper(Context context) {
super(context, "notificacion3", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
//Log.d("APP", "------------------------ Filas: " + db.toString());
db.execSQL(Utilidades.CREAR_TABLA_NOTIFICACION);
}
public void guardarNoti(String titulo, String descripcion){
db = super.getReadableDatabase();
ContentValues values = new ContentValues();
//SimpleDateFormat sdf = new SimpleDateFormat("HH:mm dd/MM/yyyy");
try {
//Date horaActual = sdf.parse("13/07/2018");
//Log.d("APP", ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Filas3: " );
values.put(CAMPO_TITULO, titulo);
values.put(CAMPO_DESCRIPCION, descripcion);
// values.put(CAMPO_FECHA, horaActual.toString());
}catch (Exception error){
error.printStackTrace();
}
db.insert(TABLA_NOTIFICACION, null, values);
db.close();
}
public void removeItem(int id){
db = this.getWritableDatabase();
//db.delete(Utilidades.TABLA_NOTIFICACION,Utilidades.CAMPO_ID + "=" + id,null);
db.execSQL("delete from " + Utilidades.TABLA_NOTIFICACION + " where " + Utilidades.CAMPO_ID +" = " + id);
// Log.d("APP", "------------------------ Filas: " + id);
db.close();
}
public ArrayList<Notificacion> llamarTodo(){
db = this.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from " + TABLA_NOTIFICACION, null);
if(cursor.moveToFirst()){
// Log.d("APP", ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> Filas8: " );
for(int i = 0; i < cursor.getString(0).length();i++) {
Notificacion contenedor = new Notificacion(cursor.getString(0), cursor.getString(1), cursor.getString(2),cursor.getString(3));
notificaciones.add(contenedor);
}
}
db.close();
return notificaciones;
}
@Override
public void onUpgrade(SQLiteDatabase db, int viejaVersion, int nuevaVersion)
{
db.execSQL("DROP TABLE IF EXISTS notificacion3");
db.execSQL(Utilidades.CREAR_TABLA_NOTIFICACION);
}
public ArrayList llenar_lv(){
ArrayList<Notificacion> list = new ArrayList<>();
SQLiteDatabase database = this.getWritableDatabase();
String q = "SELECT * from notificacion3";
Cursor registros = database.rawQuery(q,null);
if(registros.moveToFirst()){
do{
list.add(new Notificacion(registros.getString(0), registros.getString(1), registros.getString(2), registros.getString(3)));
}while(registros.moveToNext());
}
return list;
}
}