2

**This problem occurs when application is not running so please answer to the point

my question is simple.

i am using Firebase push notifications to get notifications. When my application is running my whole code is working fine values are being saved to database correctly. The problem arises when the application is not running and notifications comes and no data is saved to database in FirebaseMessagingService.

My FirebaseMessagingService code is

  public class MyFirebaseMessagingService extends FirebaseMessagingService {
    private static int NOTIFY_ME_ID = 0;

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        Message message= new Message(remoteMessage.getNotification().getTitle(),remoteMessage.getNotification().getBody(),remoteMessage.getData().get(Constants.Keys.IMPORTANT));

        DatabaseHandler databaseHandler= new DatabaseHandler(getApplicationContext());
        databaseHandler.addMessage(message);
}
}

And Database code is below

public class DatabaseHandler extends SQLiteOpenHelper {
    // All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME = "notifierxdatabase";

    // Contacts table name
    private static final String TABLE_MESSAGE = "message";

    // Contacts Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_TITLE = "title";
    private static final String KEY_BODY = "body";
    private static final String KEY_TYPE = "type";

    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_MESSAGE + "("
                + KEY_ID + " INTEGER PRIMARY KEY," + KEY_TITLE + " TEXT,"
                + KEY_BODY + " TEXT," + KEY_TYPE +" TEXT"+ ")";
        db.execSQL(CREATE_CONTACTS_TABLE);
    }

    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_MESSAGE);

        // Create tables again
        onCreate(db);
    }

    /**
     * All CRUD(Create, Read, Update, Delete) Operations
     */

    // Adding new contact
    public void addMessage(Message message) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_TITLE,message.getTitle());
        values.put(KEY_BODY,message.getBody());
        values.put(KEY_TYPE,message.getType());
        // Inserting Row
        db.insert(TABLE_MESSAGE, null, values);
        db.close(); // Closing database connection
    }

    // Getting single contact
    public Message getMessage(int id) {
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(TABLE_MESSAGE, new String[] { KEY_ID,
                        KEY_TITLE, KEY_BODY,KEY_TYPE }, KEY_ID + "=?",
                new String[] { String.valueOf(id) }, null, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();

        Message message = new Message(Integer.parseInt(cursor.getString(0)),
                cursor.getString(1), cursor.getString(2),cursor.getString(3));
        // return contact
        return message;
    }

    // Getting All Contacts
    public List<Message> getAllMessages() {
        List<Message> messageList = new ArrayList<Message>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_MESSAGE;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {

                Message message= new Message();
                message.setId(Integer.parseInt(cursor.getString(0)));
                message.setTitle(cursor.getString(1));
                message.setBody(cursor.getString(2));
                message.setType(cursor.getString(3));
                messageList.add(message);
            } while (cursor.moveToNext());
        }

        // return contact list
        return messageList;
    }

    // Updating single contact
    public int updateMessage(Message message) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_TYPE, message.getTitle());
        values.put(KEY_BODY, message.getBody());
        values.put(KEY_TYPE, message.getType());

        // updating row
        return db.update(TABLE_MESSAGE, values, KEY_ID + " = ?",
                new String[] { String.valueOf(message.getId()) });
    }

    // Deleting single contact
    public void deleteMessage(Message message) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_MESSAGE, KEY_ID + " = ?",
                new String[] { String.valueOf(message.getId()) });
        db.close();
    }


    // Getting contacts Count
    public int getMessagesCount() {
        String countQuery = "SELECT  * FROM " + TABLE_MESSAGE;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        cursor.close();

        // return count
        return cursor.getCount();
    }

}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Zaeem Sattar
  • 990
  • 2
  • 12
  • 30

0 Answers0